Lab 4: Pot Motion Control
Now that we have a way to read an angle (not precisely but
close), we will add the servomotor with the angle dial and pointer.
We will then use the pot to control the position 10 times per
second, and thereby control the motion of the servomotor.
Parts required:
1 Arduino
1 USB cable
1 Arduino proto shield
and jumper wires
1 Potentiometer
1 100 Ω resistor
Check off when complete:
■ FIGURE 19:
Servo and
pot dials.
1 Potentiometer dial angle image
1 Servomotor
1 Servomotor angle dial image
1 Servomotor angle pointer image
❏ Place the following program into the Arduino IDE:
■ FIGURE 17:
Pot motion
control
schematic.
Estimated time for this lab: 30 minutes
❏ The images for the pot dial, servomotor dial, and pointer are
shown in Figure 19. You can reuse the servomotor dial and
pointer from Chapter 5 if you have it. Construct the dial and
pointer as discussed in Chapter 5.
❏ Assemble the servomotor dial and pointer, then plug it into the
Arduino proto shield breadboard as in Figures 1, 16, and 17.
// A101_ch7_pot_motion_control 5/7/14 Joe Pardue
#include <Servo.h>
Servo myservo; // create servo object to control a
// servo
int sensorPin = A0; // analog input pin
int zero = 0; // calibration reading for 0 degree
int oneeighty = 0; // calibration reading for 180
■ FIGURE 18:
Pot motion control test.
void setup() {
// attaches the servo on pin 9 to the servo
myservo.attach(9);
Serial.begin(57600);
Serial.println(“Pot motion control rev 1.0”);
}
void loop() {
int val;
if(Serial.available())
{
char c = Serial.read();
if(c == ‘a’) // get the zero degree calibration value
{
// read the value from the sensor:
zero = analogRead(sensorPin);
Serial.print(“You set 0 degree to: “);
Serial.println(zero);
}
if(c == ‘b’) // get the 180 degree calibration value
{
// read the value from the sensor:
oneeighty = analogRead(sensorPin);
Serial.print(“You set 180 degree to: “);
Serial.println(oneeighty);
}
} delay(100);
Continued on page 77
July 2014 73