Servos with Huzzah32

In this tutorial we will explore how to control servos with a Huzzah32 (EPS32). The overall process is similar to using servos with an Arduino UNO. There are a few difference in the code setup() and some power considerations that we must address.

This demo begins by getting a single single servo connected and up and running. In a second video I then describe how to power multiple servos and give a demo of adding 3 servos to the system.

Let’s get started with a single servo and a Huzzah32.

Video:: Single Servo with Huzzah32

Get the code on Github. Download link (experimental).

Time stamps are linked in the video description on vimeo.
Show time stamps

00:00 intro
01:10 wiring and connections
06:20 library and code
13:15 see it in action

Single Servo Code

// this code in also on github (with history and credits):
// https://github.com/hex705/netArt/tree/master/electronicsReviews/servosWithHuzzah32

#include <ESP32Servo.h>

Servo myservoWhite; // create servo object to control a servo

int pos = 0;          // variable to store the servo position
int servoPinWhite = 14;   

void setup() {
	// Allow allocation of all timers - not sure why
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);

	myservoWhite.setPeriodHertz(50);   // standard 50 hz servo
	myservoWhite.attach(servoPinWhite, 500, 3000); // attaches the servo 

	// NOTE -- I have changed the default min/max of 1000us and 2000us
	// different servos may require different min/max settings
	// for an accurate 0 to 180 sweep
}

void loop() {

  for (pos = 0; pos <= 180; pos += 5) { 
      myservoWhite.write(pos);    
      delay(10);                    
  }
  for (pos = 180; pos >= 0; pos -= 5) { 
      myservoWhite.write(pos);    
      delay(10);                 
  }
}

Multiple Servos with Huzzah32

Get the code on Github. Download link (experimental).

Time stamps are linked in the video description on vimeo.
Show time stamps

00:00 intro
00:20 Connections
00:48 Code for 3 servos
02:20 Try it! :: Connect the servos.
04:03 FAIL! :: What’s going on ? 
06:26 Solution :: External power
10:55 Alt :: Battery power

Multi Servo Code:

// this code in also on github (with history and credits):
// https://github.com/hex705/netArt/tree/master/electronicsReviews/servosWithHuzzah32


#include <ESP32Servo.h>  // https://github.com/madhephaestus/ESP32Servo

// servo objects
Servo myservoGreen; 
Servo myservoBlue;
Servo myservoWhite;

int pos = 0;    // variable to store the servo position

// servo pins
int servoPinGreen = 15;
int servoPinBlue  = 32;
int servoPinWhite = 14;

void setup() {
	// Allow allocation of all timers
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);
 
  // set frequency of pulse -- standard 50 hz servo
	myservoGreen.setPeriodHertz(50);  
	myservoBlue.setPeriodHertz(50); 
	myservoWhite.setPeriodHertz(50); 

  // attache the servos to their respective pins 
  // using default min/max of 1000us and 2000us
	myservoGreen.attach(servoPinGreen, 500, 3000); // pin,min,max
	myservoBlue.attach (servoPinBlue,  500, 3000);
	myservoWhite.attach(servoPinWhite, 500, 3000);
  
	// different servos may require different min/max settings
	// for an accurate 0 to 180 sweep
}     
  
void loop() {
  // goes from 0 degrees to 180 degrees
  for (pos = 0; pos <= 180; pos += 5) { 
      myservoGreen.write(pos); 
      myservoBlue.write(pos); 
      myservoWhite.write(pos);    
      delay(10) 
  }

  for (pos = 180; pos >= 0; pos -= 5) { 
      myservoGreen.write(pos); 
      myservoBlue.write(pos); 
      myservoWhite.write(pos);    
      delay(10);               
  }
}