PAGE 1 | GREEN ENERGY HANDBOOK © 2 0 0 8 - 2018 M I C H A E L E T T E R S H A N K
https://www.robotscience.co.za
GREEN ENERGY: WORKSHOP 8.3 SERVO MOTOR CONTROL WITH A VARIABLE RESISTOR
This illustration clearly shows how the value of the HIGH pulses control position of a hobby servo.
The next step is to wire up a voltage divider circuit [in this instance a variable resistor] and use the Arduino analogue function to read
the variable resistor sweeper position [VRP] and then use some clever code to map the VRP to the servo so you can adjust the
position of the servo through adjusting the setting of the variable resistor. Wire everything up like this:
Now that you have a hobby servo and variable resistor [or potentiometer] wired up to your microcontroller you can upload the code
on the next page and test the system. If nothing happens check and make sure that the pins in the code are same as the actual pins.
PAGE 2 | GREEN ENERGY HANDBOOK © 2 0 0 8 - 2018 M I C H A E L E T T E R S H A N K
https://www.robotscience.co.za
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // map the analogue input values to the servo output values (between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
Now replace the variable resistor [voltage divider] with a light dependent resistor [LDR] and pull up resistor and observe what
happens when you shine a bright light on the LED. Try swapping the LDR and the pull up resistor to a pull down resistor.