Introduction: Micro Servo Lab
In this lab we will work on controlling a micro servo's position with a potentiometer. Based on the position of the micro servo's "arms" we will light up corresponding rows of LEDs. For this lab you will need:
- 1 micro servo (the one provided is a 9 gram micro servo)
- 1 potentiometer
- 10 LEDS (using two different colors)
- 10 220 Ohm resistors
Step 1: Connect a Micro Servo
The micro servo has three wires for power, ground, and a signal pulse. The micro servo will accept a PWM pulse to determine what position it should be in (0 - 180 degrees). Technically you can use any of the PWM pins on the Arduino Uno, but we generally start with Pin 9 or 10*.
Setup:
- Connect the breadboard to the power rail (+5V) and the ground rail (GND)
- Connect the servo to the power rail, ground rail, and Pin 9.
**This is because the Servo library utilizes Timer2 on the Arduino which will block us from using PWM signals, analogWrite(), on these two pins for any purpose other then controlling a servo. While we can still use these pins for digital i/o, we generally will use these exclusively for servo control**
Step 2: Test Micro Servo
The code here is the sample code provided by the Servo Library. It will simply have the servo sweep back and forth from 0 to 180 degrees
/* Sweep by BARRAGAN <http://barraganstudio.com> This example code is in the public domain. modified 8 Nov 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Sweep */ #include "Servo.h" Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
Step 3: Connect a Potentiometer
We will now work on manually controlling the position of the servo with a potentiometer. Connect the potentiometer as follows:
- Left Side - Ground rail
- Right Side - Power rail
- Top/Middle connection - Pin A0 (analog 0 pin)
Step 4: Potentiometer Starter Code
Below is some starter code for controlling the servo with a potentiometer. Finish the code so that when you move the potentiometer, the servo will move in unison.
/* Sweep by BARRAGAN This example code is in the public domain. modified 8 Nov 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Sweep */ #include "Servo.h" Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position int potPin = 0;//Select the pin for connecting the potentiometer int potVal = 0;//Current potentiometer value void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode(potPin, INPUT); } void loop() { potVal = analogRead(potPin); myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position }
Step 5: Connect First LED
After we have the servo controlled via the potentiometer, we're going to add some feedback via some LEDs. We will be creating two rows of LEDs. One will represent the "left" arm of the servo and the other will represent the "right" arm of the servo. As the servo changes positions, one arm will rise and the other will fall. The LEDs will light up to display:
- full - arm is raised
- half - arms are equal.
- off - arm is lowered
The diagram will display the rows of LEDs on opposite ends of the breadboard. This was done for ease of visibility, your LED's should be lined up/even with one another.
Connect first LED:
- Connect the short lead of the LED to the ground rail
- Connect the longer lead of the LED to a 220 Ohm resistor. Connect the resistor to pin 13 on the Arduino.
Step 6: Connect Remaining Row of LEDs
After the first LED has been added, connect the remaining LEDs:
- Shorter lead - connect to the ground rail
- Longer lead - connect a 220 Ohm resistor to the LEDs and the following Arduino Pins: 12, 11, 10, 9, 8
Step 7: Add First LED, Second Row
The second row of LEDs will be added in the same fashion as the first:
- Connect the short lead of the LED to the ground rail
- Connect the longer lead of the LED to a 220 Ohm resistor. Connect the resistor to pin 7 on the Arduino.
Step 8: Connect Final LEDs
Connect the remaining LEDs:
Shorter lead - connect to the ground rail Longer lead - connect a 220 Ohm resistor to the LEDs and the following Arduino Pins: 6, 5, 4, 3
Step 9: Control LED Display
Your last step is to update your code to control your LEDs. It will need to handle the following:
- The top row will match the "right arm" of the servo. As the arm sweeps up/down the LEDs must turn on/off.
- The bottom row will match the "left arm" of the servo. As the arm sweeps up/down the LEDs must turn on/off.