Lab 8: Output: Servo Motors
- Due Oct 25, 2017 by 10:30am
- Points 10
- Submitting a file upload
- File Types pdf
Objective
Using the servo motor to explore rotational and linear motions.
Activities
Part 1 - The Servo Motor
1. Connect jumper wires to the servo motor. Also add a colorful marker to one of the servo horns so you can easily see when it is turning.
2. Connect the servo motor to your Arduino.
Try this code to control the servo using the number keys on your keyboard. Observe the position of the servo as well as the values (pulse width in ms) printed on the serial monitor. You can send the numbers 1-9 over serial to control the position of the motor.
/*
* Servo Control by Serial
* entering 1-9 on the serial monitor will move the servo
* motor to corresponding positions between 0 and 180 degrees
*
* brief history of edits:
*
* modified for TUI Fall 2017
*
* modified for TUI October 2007
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt
* http://todbot.com/
*
* adapted from "http://itp.nyu.edu/physcomp/Labs/Servo"
*/
#include <Servo.h>
int servoPin = 7; // pin for the servo motor
Servo myservo; // create servo object to control a servo
int pos; // the position we want to move the servo to
int val; // variable used to store data from serial port
void setup() {
myservo.attach(servoPin); // attaches the servo on pin servoPin to the servo object
Serial.begin(9600);
Serial.println("Servo control program ready");
}
void loop() {
while (Serial.available()) {
val = Serial.read();
if (val >= '1' && val <= '9' ) {
Serial.println();Serial.println();
// interpret the user's input to get a number from 1 to 9
val = val - '0'; // convert val from character variable to number variable
// (see http://forum.arduino.cc/index.php?topic=103511.0 for a forum discussing more about this)
Serial.print("val = ");Serial.println(val);
// calculate the position that we should move the servo to
pos = map(val, 1, 9, 0, 180); // (see https://www.arduino.cc/en/Reference/Map)
Serial.print("pos = ");Serial.println(pos);
// tell the servo to go to the position
myservo.write(pos);
// wait a few milliseconds so we don't overload the servo with too many commands all at once
delay(15);
}
}
}
3. Next, connect your potentiometer and control the servo motor with the pot using the code below. Again, observe the position of the servo as well as the values printed on the serial monitor.
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal 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(7); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // 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
}
Part 2 - Make a Crawler
You can turn rotational movement into linear movement with clever mechanical levers. Experiment with different levers that can make your crawler move forward. Here is one example of a crawler.
We will cover other simple mechanics next week!
You might also find this sketch helpful. It just makes the servo sweep back and forth repeatedly. You could try tweaking the code to make it move back and forth in a slightly different pattern if that helps your crawler.
/* Servo Sweep
just makes the servo sweep back and forth repeatedly
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>
int servoPin = 7; // pin for the servo motor
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(servoPin); // attaches the servo on pin servoPin 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
}
}
Homework
Make a crawler. Include photos and descriptions of the crawler itself. Once you get your crawler to move forward, perhaps you would want to generate movement from your program and use your potentiometer to control the speed of the movement. You may also team up with a friend and use two servos instead of one.
No wheels allowed! This should crawl, not roll.
For more information on powering the arduino and servo motor without plugging it into your laptop, see this forum post: https://forum.arduino.cc/index.php?topic=474522.0 Links to an external site.
and ask around at the Invention Lab!