Arduino Powered Servo Control - I Made It at TechShop
by madebyjoe in Circuits > Arduino
5300 Views, 21 Favorites, 0 Comments
Arduino Powered Servo Control - I Made It at TechShop
Servos are incredibly useful in making "things" move. But in order to make that thing move you need to control it somehow. Luckily, arduino makes this incredibly easy to do and can be done with something as a potentiometer. The potentiometer turns and the servo can follow this rotation to it maximim bounds. I made this at TechShop and you can too.
http://techshop.ws
http://techshop.ws
Materials
Arduino Uno
Servo (5volt, really any small size will work)
10k ohm potentiometer
breadboard
wire
usb cable
Servo (5volt, really any small size will work)
10k ohm potentiometer
breadboard
wire
usb cable
Circuit
This circuit is just about as easy as easy gets with an arduino powering it.
The servo has three wires, Vcc, ground, and data. Connect Vcc to power, ground to ground, and data to pin 3. The data pin is important because it needs to be a pin with PWM or pulse with modulation control. Not all arduino pins have this capability. On the uno they are marked by a " ~ ".
Connect the potentiometer to power and ground. Connect the third leg to analog pin 5. This creates a variable voltage divider for the arduino to read.
Circuit complete.
The servo has three wires, Vcc, ground, and data. Connect Vcc to power, ground to ground, and data to pin 3. The data pin is important because it needs to be a pin with PWM or pulse with modulation control. Not all arduino pins have this capability. On the uno they are marked by a " ~ ".
Connect the potentiometer to power and ground. Connect the third leg to analog pin 5. This creates a variable voltage divider for the arduino to read.
Circuit complete.
Code
This is the code for the servo to follow the potentiometer
#include <Servo.h>
#define POT_PIN 5
#define SERVO_PIN 3
Servo myservo; // create servo object to control a servo
void setup()
{
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
}
void loop()
{
int val;
val = analogRead(POT_PIN); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value 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
}
Run that and Viola! You can now control a servo with a potentiometer.
#include <Servo.h>
#define POT_PIN 5
#define SERVO_PIN 3
Servo myservo; // create servo object to control a servo
void setup()
{
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
}
void loop()
{
int val;
val = analogRead(POT_PIN); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value 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
}
Run that and Viola! You can now control a servo with a potentiometer.