Arduino UNO Tutorial 2 - Servos

Arduino UNO Tutorial 2 - Servos

Radio Control Servos are great pieces of kit. They are essential in pretty much every robot build, from controlling arms and legs to driving wheels and tracks. Servos normally rotate up 180 degrees with the 90 degree mid-point being the center position, and can be positioned at any point in-between. By replacing the positional feedback potentiometer inside a servo can be made to fully rotate in either direction and be made to drive wheels for your robot. (this modification is for a later tutorial)

So, lets get used to driving a servo with the Arduino Uno

Load the following arduino sketch program. We start off by including the <Servo.h> file. This file contains all the coding required for controlling servos to make our life really easy. Next we need to create our servo object. Our servo is to be called servoMain (but it can be called anything you like). Next comes the setup() function and inside we attach our servo object to pin 10.

We are now ready to send commands to our servo. In the main loop() function we issue a number of commands to rotate the servo to various positions, with a 1 second delay between each command.

 

/*
Arduino Servo Test sketch
*/
#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
   servoMain.attach(10); // servo on digital pin 10
}

void loop()
{
   servoMain.write(45);  // Turn Servo Left to 45 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(0);   // Turn Servo Left to 0 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(90);  // Turn Servo back to center position (90 degrees)
   delay(1000);          // Wait 1 second
   servoMain.write(135); // Turn Servo Right to 135 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(180); // Turn Servo Right to 180 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(90);  // Turn Servo back to center position (90 degrees)
   delay(1000);          // Wait 1 second   
}

On your servo you will have 3 wires. Normally black, red (center) and either orange or white.

Servo

Using some single strand hook up wire, connect the black wire to one of the Arduino 0V pins. Connect the red wire (center wire) to the Arduino 5V pin. And finally connect the orange/white wire to pin 10.

Try it out, and play around with the settings

Tutorial 1 - Flashing LED

Tutorial 3 - Timing

Shopping Cart
0 items
 
DELIVERY/PAYMENT

Free Delivery Orders over £50

UK - £2.40 orders below
                    £50.00
EU Airmail from £3.95
Worldwide from £7.95
More Details...

 
Specials
 
Brands
 
 Check out our videos