Web
Analytics
 
 

ESP32 vs. Arduino: Servo Control!

Hey guys! We've had so much fun interacting with all of the makers out there enjoying our new Voyager and Explorer Robotics Kits. With the high-speed DC motors and the high-torque robotic arm, it's got a ton of features! Today, I wanted to break things down for anyone out there who may be interested in programming ESP32 but might be a little nervous about its differences with the Arduino. While in some cases, the ESP32 can be a little more complicated, today I wanted to show you the differences between basic servo movement with the Arduino, and basic Servo movement with the ESP32! It's easier than you might think! As always, we have this tutorial available on our YouTube Channel as well. First things first, let's take a look at just how easy it is to get an Arduino moving a servo.



Part One: Simple Servo Movement using Arduino

To demonstrate just how easy this is, I thought I would use our Robotic Laser Cannon kit as an example. This PCB has two servo connectors and a joystick. We're going to start off by getting one of our servos to move.

If we were to build this same circuit on a breadboard, it would look something like this. With the brown (or black) wire of our servo connected to GND (ground), the red wire connected to 5v (five volts), and the yellow wire (our data wire, sometimes white on certain servos ) connected to pin D3.



Let's take a look at what it would take to program our servo with a simple sketch that would move our servo back and forth.


#include <Servo.h>
Servo servoOne;

void setup() {
servoOne.attach(3);
}

void loop() {
servoOne.write(0);
delay(500);  
servoOne.write(0); 
delay(500);   
}

Pretty simple, right? Our first step is calling the Arduino Servo library, which is built into the Arduino IDE, and doesn't require you to download anything extra. the next step is declaring our Servo object but typing

Servo servoOne;

Then in our void setup, we need to attach our servo object (servoOne) to a PWM pin on the Arduino. We have the option of using pins 3,5,6,9,10, or 11. In this case, we'll choose pin D3.

servoOne.attach(3);

Once we have all of that setup, all we need to do is tell our servos what to do! In this case, in our void loop, we have our servo rotating from 0 degrees to 180 degrees, every half second.



Part Two: Let's add in some control!

So more often than not, we don't simply want our servos to simply move back and forth. In most cases, we want some level of accuracy and control. To show just how easy is it to take this project to the next level, let's go ahead and add in the code we need to use the onboard joystick we have on our Robotic Laser Cannon Kit PCB. If you were to build this project on a PCB, it would look something like this.




To get our servo to respond to our joystick data, we'll need a few more variables and some conditional statements. Check out our second example code, this should do the trick!

#include <Servo.h>
Servo servoOne;
int posX = 90;
int joyX;

void setup() {
  servoOne.attach(3);
  servoOne.write(posX);
}

void loop() {
joyX = analogRead(A1);
if(joyX > 800)
{
  posX++;
  servoOne.write(posX);
  delay(20); 
}
else if(joyX < 200)
{
  posX--;
  servoOne.write(posX);
  delay(20);  
}
}

The first big difference in our example code here is two new variables.

int posX = 90;
int joyX;

Our position variable (posX) is given a value of 90, and it represents a start position for our servo of 90 degrees. It's this variable that we're going to increase and decrease with our joystick to move our servo down in the void loop. Our second new variable is for our joystick data. We're going to be reading the data from our joystick in the void loop and we'll need a variable to hold that information for us!


Next is our void setup. We've only added one line of code to our original example.

 servoOne.write(posX);

Since we have created a position variable, and we set it to 90 degrees, then placing this command in the void setup will make our servo automatically start off at it's halfway point every time the Arduino is turned on, or the reset button is pressed. This isn't really necessary but can be a helpful little addition.


The biggest change to our original example code is n the definitely in the void loop. Our first line of code is what is grabbing the data from our joystick.

joyX = analogRead(A1);

Joysticks are fascinating, and surprisingly simple devices. To power up and read a joystick, all you need to do is send in 5 volts, connect GND, and then read the change in voltages for the X and Y axis. A joystick is made up of two devices known as potentiometers. A potentiometer is a device that acts as a resistor, resisting the flow of electricity, but one that can change its resistive properties based on position! So 5 volts goes in, and as you move the joystick left and right, that output voltage changes. The "analogRead" command, let's us read that change in voltage as a string of numbers. In this case, we would read 1023 if the joystick was pushed in one extreme, and 0 if it was pushed in the opposite extreme. This leads us to our first conditional statement.


if(joyX > 800){  
posX++;  
servoOne.write(posX);  
delay(20); 
}

Now, we've already established that moving the joystick in one direction back and forth would give us a range of 0 - 1023, and so our is condition is based on if that value happens to go above 800. Since 1023 is the value of the joystick in one extreme, and right around 500 would be the neutral position right in the middle, then if you read a value of 800, the joystick must be being pushed towards one extreme.


So if that's the case, we want to start moving our servo. We do this by first adding to the value of our position variable with the line "posX++". In most programming languages "++" is shorthand for "add one" so, if the joystick is being pressed in a certain direction (in our case to the right), then our program will add one to the value of posX. The next line of code tells our servo to move to whatever the value of posX happens to be, so as the number posX gets bigger, the servo will move right! In the beginning, that would be 90, but if you keep holding down your joystick, you'll keep adding to the value of posX, and your servo wil