Introduction: In this session you will look at a piece of equipment called a Servo Motor. This is a type motor used to create really precise movement. Unlike DC (Direct Current) motors, which can only be specified to move forward or backward, Servo motors can specify a position to the degree. Goals Servo motor basics. Access the servo library. Control the Servo with a potentiometer. Page 1 - Start Arduino Course: 04 Servos
The Sensors To use a Servo motor, we need two things, we need to be able to power it and to control it. This means they ll be three connections to our Servo motor: Power, Ground and Control. To control Servos, we actually use the same principle for controlling the brightness of our LEDs Pulse Width Modulation. This works by changing the duty cycle of the square wave, and keeping the frequency the same throughout. Pulse Width Modulation (PWM) 5v 25% Duty Cycle 50% Duty Cycle 5v 0v 0v 5v 75% Duty Cycle 0v 04 - Servos Challenge What does the Arduino Pin have to allow if we want to use a servo with it? What symbol does the Arduino use to identify which pins can do this? Page 2 - Start Arduino Course: 04 Servos
Circuit 4.0 - Servo Hardware For this circuit, we are going to make a Servo motor rotate backwards and forwards. We will be defining the amount of degrees to rotate and how long to pause for between each rotation. For this, we will need: 1x Arduino Uno 1x USB cable 1x Servo motor 6x Jumper Wires Page 3 - Start Arduino Course: 04 Servos
Circuit 4.0 - Servo Code Make a new sketch in the Arduino IDE and name this Servo. To use the servo, there is a lot of complex algorithms and conversions to make sure the servo can make these precise movements, so we re going to use the Servo Library which comes included in Arduinos IDE. To use external library, you have to let the Arduino know you want it included in your sketch. To do this we use the <include> function. //include the Servo library in the sketch #include <Servo.h> //create servo object to control a servo Servo servo1; //variable to store the servo position int pos = 0; void setup() //attaches the servo on pin 9 to the servo object servo1.attach(9); } void loop() //goes from 0 degrees to 180 degrees in steps of 1 degree for (pos = 0; pos <= 180; pos +=1) //tell servo to go to position in variable pos servo1.write(pos); //waits 15 milliseconds for the servo to reach the position delay(15); } //goes from 180 degrees to 0 degrees for (pos = 180; pos >= 0; pos -= 1) //tell the servo to go to position in variable pos servo1.write(pos); //waits 15 milliseconds for the servo to reach the position delay(15); } } Page 4 - Start Arduino Course: 04 Servos
Circuit 4.0 - Servo Code Once you have copied the code, press (upload) and watch the results! (compile) and if no errors appear, press At this stage your servo should be rotating backwards and forwards. My code won t compile! Is everything spelt correctly? Are all your lines ending in a semi-colon? Do you have the correct capital letters? Did you close the curly brackets? Let s go through the code understand what every part is doing. Libraries #include <Servo.h> This is how we include the Arduino Servo Library. This makes it possible to use pre-defined functions in our projects that make using the Servo motors and Arduino easier. Servo Object Servo servo1; This is how we make our Servo Object. This allows us to use the library functions. We will now refer to our servo as servo1. Global Variables int pos = 0; These are our global variables for storing the Servo position and Servo pin. This means we can easily keep track of the position and the digital pin we are using for controlling the Servo. These are created outside void setup and void loop so they are accessible in both. void setup() void setup() This is our void setup(). We use curly brackets to define what happens in the setup of our program. This function is called once. Page 5 - Start Arduino Course: 04 Servos
servo1.attach(9); Our Servo needs to be attached to the digital pin we are using to control it. We set this using the number stored in variable servo_pin. This called in void setup() as it only needs to called once. void loop() void loop() This is our void loop(). This is where our main loop for our program happens. We set everything inside void loop() by using curly brackets. Everything within these brackets will loop infinitely. for loop (forwards) //goes from 0 degrees to 180 degrees in steps of 1 degree for (pos = 0; pos <= 180; pos +=1) //tell servo to go to position in variable pos servo1.write(pos); //waits 15ms for the servo to reach the position delay(15); This is our for loop for moving our servo forwards. We use normal brackets to set how many times to loop through everything within our curly brackets. In this instance, we increment the variable pos by 1 until it reaches 180. Everytime we increment pos, we use Servo library function write(). Variable pos is then used to set the Servo Position. We use delay() with a value of 15 to pause the loop for 15 milliseconds. This gives the Servo time to reach it s position. for loop (backwards) //goes from 180 degrees to 0 degrees for (pos = 180; pos >= 0; pos -= 1) //tell the servo to go to position in variable pos servo1.write(pos); //waits 15ms for the servo to reach the position delay(15); This for loop is for moving our Servo position backwards. This works exactly the same as the previous for loop, but instead of moving from 0 to 180, it sets the Servo Position from 180 to 0. This loop happens after the first, so variable pos can first reach 180, then will decrease to 0. Page 6 - Start Arduino Course: 04 Servos
Circuit 4.0 - Servo Challenge How would you change the speed of your Servo? What happens when we remove the delay in our for loops? Why? What other digital pins could I use to control a Servo with? How do we know this? What could you make with your Servo? Page 7 - Start Arduino Course: 04 Servos
Circuit 4.1 - Servo + Potentiometer Hardware Can you try and control the Servo with a potentiometer? For this, we will need: 1x Arduino Uno 1x USB cable 1x Servo motor 8x Jumper Wires 1x Potentiometer Try and work the code out for yourself Remember back to how we read data from a potentiometer, it uses Analog Readings. That means you will need to scale your Analog Input data to the Servo position data. For this you can use the Arduino function map(). This function is an easy way of scaling a value to another range. int outcome - map(int input, int min_input, int max_input, int min_output, int max_output); What is the input? What is the minimum and maximum value the input can have? What is the minimum and maximum value the outcome should have? Page 8 - Start Arduino Course: 04 Servos