|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
Servo Motor Control with an ArduinoOverviewIn this lab, you'll control a servomotor's position using the value returned from an analog sensor. Servos are the easiest way to start making motion with a microcontroller. Even though they don't turn 360 degrees, you can use them to create all sorts of periodic or reciprocating motions. Check out some of the mechanisms at Rob Ive's site for ideas on how to make levers, cams, and other simple machines for making motion. Table of Contents (hide) 1. PartsFor this lab you'll need:
2. Prepare the breadboardConnect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections: (Diagram made with Fritzing - download)
3. Connect an analog input sensor and a servoPick any analog input and connect it to Analog pin 0 as you did in the Analog Input and Output Lab. Then connect an RC servomotor to digital pin 2. The yellow wire of the servo goes to the pin, and the red and black wires go to +5V and ground, respectively. Not all servos have the same wiring colors. For example, the Hextronik servos that come with Adafruit's ARDX kit use red for +5V,brown for ground, and mustrard yellow for control.
When you attach the servo, you'll need a row of three male headers to attach it to a breadboard. You may find that the pins don't stay in the servo's connector holes. Put the pins in the servo's connector, then push them down on a table gently. They will slide up inside their plastic sheaths, and fit better in your servo's sonnector.
4. Program the MicrocontrollerFirst, find out the range of your sensor by using analogRead() to read the sensor and printing out the results.
void setup() { Serial.begin(9600); // initialize serial communications } void loop() { int analogValue = analogRead(A0); // read the analog input Serial.println(analogValue); // print it } Now, map the result of the analog reading to a range from 0 to 179, which is the range of the sensor in degrees. Store the mapped value in a local variable called servoAngle.
void setup() { Serial.begin(9600); // initialize serial communications } void loop() { int analogValue = analogRead(A0); // read the analog input Serial.println(analogValue); // print it // if your sensor's range is less than 0 to 1023, you'll need to // modify the map() function to use the values you discovered: int servoAngle = map(analogValue, 0, 1023, 0, 179); } Finally, add the servo library at the beginning of your code, then make a variable to hold an instance of the library, and a variable for the servo's output pin. In the setup(), initialize your servo using servo.attach(). Then in your main loop, use servoAngle to set the servo's position.
#include <Servo.h> // include the servo library Servo servoMotor; // creates an instance of the servo object to control a servo int servoPin = 2; // Control pin for servo motor void setup() { Serial.begin(9600); // initialize serial communications servoMotor.attach(servoPin); // attaches the servo on pin 2 to the servo object } void loop() { int analogValue = analogRead(A0); // read the analog input Serial.println(analogValue); // print it // if your sensor's range is less than 0 to 1023, you'll need to // modify the map() function to use the values you discovered: int servoAngle = map(analogValue, 0, 1023, 0, 179); // move the servo using the angle from the sensor: servoMotor.write(servoAngle); } 5. Get CreativeServo motors give you the power to do all kinds of things. They can be used to push a remote contro button, in a pinch: (Project: Tom Igoe)You can play drums (Project: Nick Yulman) You can make a rat's tail move (project: Gabriela Gutiérrez) If you've got 800 or so of them and a lot of time, you can build a wooden mirror (project: Daniel Rozin). Come up with a project of your own that needs a little movement, and see if you can solve the problem with a servomotor. |