Intro: How to Control 3 Servo Motors Using Push Button Switches and an Arduino Uno
In this project I set myself the task of controlling the movement of 3 servo motors by pressing push button switches. My intention is to use these buttons to control the motion of a three-servo robot arm.
What I want to happen:
1. I want to control a servo using a pair of buttons, one to turn it clockwise, one to turn it counter-clockwise
2. If you press and hold down a button, a servo motor will move clockwise until it reaches its maximum limit.
3. If you push and hold down the other button, a servo motor will move counter-clockwise until it reaches its maximum limit.
3. If you tap a button and release it, a servo motor will move a set amount in a particular direction.
4. When you first switch on the Arduino, the servo will reset its position to the middle of its arc of movement
Things I had to be careful of:
1. Trying to force the servo motor to keep moving in one direction, after it has reached its maximum limit, could damage it.
2. Attaching too many servos to the Arduino's 5v power-line could damage the components on the Ardiuno, so I need to power the servo motors externally.
3. Getting tied up with all these cables! I need to be able to extend their length to increase flexibility of positioning and to tidy the cables out of the way of the buttons.
Step 1: Preparing the Components
You will need:
Six 10k Ohm resistors
There is a very nice instructable here to help you understand how a resistor's colour can help you identify its rating:
Six push button switches.
I used the smaller ones here, as I found I kept breaking the legs on the larger ones when I pushed them into the breadboard.
Three standard servo motors
One 4 AA battery pack
One Arduino Uno R3
One USB cable to connect the Arduino to a PC and power it.
The Arduino IDE software to upload the program that will control the servo.
Some male/male jumper cables to make the connections.
Some male/female jumper cables to extend the cable length (more on this later)
Breakaway header pins which will be used to connect your motors to the breadboard. (If you use my extended cable option you won't need these)
Step 2: A Bit of Information About Push Button Switches
To understand why the buttons are wired up the way they are, please take a look at the website below, it tells you everything you need to know and more:
The two images I include in this step are taken from the website below and are really helpful for understanding how to wire up push button switches.
On my breadboard the push button pins are orientated like this:
So when the button is pressed pins 3 and 1 connect to each other, completing the circuit.
Step 3: Inserting the Resistors and Push Buttons Into the Breadboard
Carefully insert the push button switches as shown in the diagram. I inserted them with pin 3 and pin 1 to the top of the breadboard , but you can do it with pin 2 and 4 at the top if you prefer.
Just for reference again, the switches are inserted in this orientation:
It doesn't matter what way round you insert the resistors as long as the legs go into the correct holes. I trimmed the legs down a little bit to give them a nice low profile on the breadboard.
The resistors will connect Pin 3 of the push button switch to the Arduino 5v line, and the black cables connect Pin 1 of the push button switch to ground, completing the circuit. For the ground cables, I didn't use the male/male jumper cables, but instead used some thin, insulated copper wire I had lying around, I did this so that the cables didn't get too messy, but if you don't have any spare wire, the male/male jumper cables will work fine.
I used 4 small pieces of insulated copper wire to connect the left and right sides of the breadboard's power rails on the top and the bottom, and the black cable on the right is used to connect the ground from the Arduino, to the ground of the battery pack used to power the servos. Don't miss this step. We must make sure all ground planes (the Arduino's and the servos) are connected together.
Step 4: Adding the Servo Motors and the Arduino to the Circuit
The next step is to install the servo motors.
Here I took 2 male/male jumper cables and inserted them into the female end of 2 male/female jumper cables. This doubled the length of the cable, I did this because I was planning to use the servos in a robot and wanted to make sure the cables didn't restrict the robot arm as it moved around.
After this I inserted them into the servo motor's female connectors (for ground and power )
Next, I connected the power and ground cable from the servo motor into the bottom power rails (see the diagram for a more detailed view.) The servo's go on the bottom power rails as they will be powered by an external battery pack.
After that, I extended the length of 1 more cable, inserted one end into the servo motor' s signal pin and the other end into the Arduino at Digital In pin 8.
I repeated this process with the other two servos until I got the set up shown in the diagram above. After hooking up the servo motors, I used two extended cables to connect the Arduino's 5v power line, and ground line to the breadboard.
Step 5: Connecting the Signal Pin From the Push Buttons to the Arduino
This step is where the cables can get a bit messy, but the diagram above shows you where they plug in, just start on the left and work across the breadboard being careful not to dislodge any cables as you go along. Its important to get the cables hooked up correctly at this stage or your button presses will do unexpected things!
It isn't necessary to use extended cables for this part, but I did, as it let me hide the cables out of the way of the buttons. The bends I have put in the cables are only to keep the diagram easy to understand.
After this, the only thing that remains to be done with the physical setup is to give everything a good check and insert the battery pack cables in the bottom power rails. I have an on/off switch on my battery pack so I insert its cables right away, if you don't have an on/off switch, you may want to leave the battery pack out of the circuit until you are ready to use it. Servos are always working when they are getting power, and they can eat up a batteries power pretty quickly if you aren't careful.
OK, that's the physical setup done, now we have one final step before we can use our servo motors and buttons - programming the Arduino!
Step 6: Programming the Arduino - Declaring the Variables and Servos
OK, so all the physical steps have been completed, now it is time to tell the Arduino what to do with all the hardware we have plugged in to it.
When you first look at the program it is a bit scary, but when we break it down into its important parts, it is easy to see what is going on. To summarise what we have physically installed. we have 3 pairs of push buttons, each pair is responsible for controlling one servo motor, so our code is basically going to be repeating itself 3 times.
Let's take a look at the code which handles declaring servo objects first:
Servo myservo; // create servo object to control a servo
The code first brings in the Servo.h library so it can handle servos. Then we declare the names of the three servo objects we will use to manage our servo motors, in our case the names of the servo objects are myservo, myservo2 and myservo3.
Next we have to declare some variables we will use later in the code to store the position of the servo motor:
int pos = 85; // variable to store the servo's starting position
int pos2 = 85;
int pos3 = 85;
Here we are declaring the variables which will handle the position of the servo and we are also setting their value on startup to 85 ( halfway between 5 and 175, which will centre the position of the servo)
Next we declare 3 variables, maxDeg, minDeg and movement.
These will be used to limit the movement range of the servo and hopefully stop it from getting damaged. Look in the data sheet for your servo and you should find what the limits are for your hardware, then you can change the values to suit your particular motor.
const int maxDeg = 165; // limits the maximum range of the servo's movement to 165
const int minDeg = 5; // limits the minimum range of the servo's movement to 5
const int movement = 2; // the amount to move the servo when the button is pushed. Value can be changed.
With that done, we now need to declare two variables, leftPin and rightPin, which will tell the Arduino where to find the signal inputs from the push button switches:
const int leftPin = 2; // tells the Arduino the location of the signal cable from the switch
const int rightPin = 3;
Then we do the same thing for the other 2 pairs of push button switches:
const int leftPin2 = 4;
const int rightPin2 = 5;
const int leftPin3 = 6;
const int rightPin3 = 7;
Now we tell the Arduino where the signal outputs to the servos are located:
const int outputPin = 8; // tells the Arduino the location of the signal cable to the servo
const int outputPin2 = 9;
const int outputPin3 = 10;
Finally we declare variables which will handle the states of the push pin switches, when they are pressed or unpressed. We start off by declaring them all as unpressed or 0:
int leftPressed = 0; // variables we will use to keep information about the switch states
int rightPressed = 0;
int leftPressed2 = 0;
int rightPressed2 = 0;
int leftPressed3 = 0;
int rightPressed3 = 0;
With this done, we are now ready to set up the pins on the Arduino, which is covered in the next step.
Step 7: Programming the Arduino - Setting Up the Digital Pins on the Arduino
The Arduino is a very flexible little micro-controller and its digital pins can act as inputs or outputs, so we need to tell it what state we want its pins to be in, working as inputs or outputs.
This code attaches the servo motor's signal cable location, stored in the variable outputPin, to the servo object
// This code tells the Arduino to set the states of the pins to input mode
Now that's all set up we can get into the heart of the program, which controls how the servos move when the buttons are pressed, which is covered in the next step.
Step 8: Programming the Arduino - the Main Loop Explained
To make it easier on the eye I have only shown the code for the first set of push buttons, the actual program which I will include here has all the code for all the buttons.
This code assigns a value to the variables by reading the state of the pins and changes the value of the variable accordingly.
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
We then do the same thing for the other two pairs of buttons
Now we tell the Ardiuno what to do when the push buttons are pressed. if the left button is pressed the code checks to make sure that the servo is at less than its maximum movement. It then updates the variable pos by adding the movement variable's value, and sends this to the servo which moves by that amount
if(pos < maxDeg)
pos += movement;
myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’
If the right push button is pressed, the code checks that the servo value is more than the minimum movement, it then updates the variable pos by subtracting the movement variable's value, and sends this to the servo which moves by that amount
if(pos > minDeg)
pos -= movement;
myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’
The logic for the code is exactly the same for the other two sets of push buttons, if you have a look at the next step you can download the code and have a look for yourself.
Step 9: The Full Program
Here is the full program. Upload it to your Arduino and your project is ready to work. Well done!