Title: Arduino Week 2 Lab
1Arduino Week 2 Lab
2Objectives
- Control the rotation of standard servo motor
- A standard servo motor is limited in its rotation
between 0 and 180 degrees - Control the speed of a continuous rotation servo
motor based on the light intensity - A continuous rotation servo motor can rotate
freely without restriction
3Introduction to Servos
- A servo is a small, electrically-driven motor
that provides rotary actuation - Servos are controlled by using Pulse-Width
Modulation (PWM) - The duration of a voltage pulse determines how
far the shaft will turn (i.e. the angle) - Servos will not hold their position indefinitely,
so the position pulse must be sent repeatedly - The holding force of a servo is determined by
its torque rating
4PWM Generation
- There are 2 different ways in which you can
generate a PWM signal - Use the built-in PWM pins on the Arduino where
you only need to set the duty cycle - Manually generate one by alternating voltage HIGH
and LOW with specified delays - We will use the manual method since the built-in
PWM frequency does not match the servos expected
pulse timing
5Part 1 Controlling Servo Rotation (Manual PWM)
- int servoPin 4 //variable to store the
servo pin number - int pulse 700 //variable to store the pulse
duration - void setup()
-
- pinMode(servoPin, OUTPUT) //set the servo pin
as an output - Serial.begin(9600) //set serial data transfer
rate -
- void loop()
-
- digitalWrite(servoPin, HIGH) //send 5V to the
servo - delayMicroseconds(pulse) //for pulse
microseconds - digitalWrite(servoPin, LOW) //send 0V to the
servo - delay(20) //for 20 milliseconds
You can change the duration of the pulse to vary
the servos rotation angle.
6Part 1 Schematic (Manual PWM)
Standard servo
7Part 1 Board Layout
Servo motor
Pin 4
5V
Servo connector
Ground
8How to read a light sensorWhat is a light
sensor?
- The light sensor we are using is the same one we
used when working with the Lego Botball robotics
kit. - Light sensor is a photoresistor, also known as a
light dependent resistor. - A photoresistor is a sensor whose resistance
varies with light intensity. Most decrease in
resistance as the light intensity increases.
9How to read a light sensorHow to connect it?
- The sensor is connected in series with a resistor
- Both of which are between the 5V terminal of the
Arduino and the Ground terminal - They form a Voltage Ladder
- The data we want comes from the voltage at the
point of connection between the sensor and
resistor This is what will change in response to
light
10Reading the Light Sensor
- int sensorPin A0 //variable to set the sensor
input pin - int sensorValue 0
- void setup()
-
- Serial.begin(9600) //set serial data transfer
rate -
- void loop()
-
- sensorValue analogRead(sensorPin) //read
the value from the light sensor - Serial.print ("Sensor value ") //print the
sensor value to the computer screen - Serial.print(sensorValue)
- Serial.println("") //"printLN" creates a
new line -
On your keyboard, press CtrlShiftM after
uploading your program to open the serial
communication dialog.
11Light Sensor Schematic
12Board Layout
5V
Pin A0
Light sensor
Light sensor connector
Resistor
Ground
13Now lets combine the light sensor with a servo
motor to build a light-sensitive servo that
rotates at speeds proportional to the light
intensity.
14Part 2 Controlling Servo Rotation Speed as a
Function of Light Intensity (1)
- int sensorPin A0 //variable to set the sensor
input pin - int outPin 5 //variable to store the output
pin - int sensorValue 0 //variable to store the
value coming from the sensor - int m 0 //variable to store the motor signal
(i.e. the voltage) - //voltage controls the speed of a continuous
servo - void setup()
-
- Serial.begin(9600) //set serial data transfer
rate - pinMode(outPin, OUTPUT) //set the output pin
as an output -
Continued on next slide
15Part 2 Controlling Servo Rotation Speed as a
Function of Light Intensity (2)
- void loop()
-
- sensorValue analogRead(sensorPin) //read the
value from the light sensor - Serial.print ("Sensor value ") //print the
sensor value to the computer screen - Serial.print(sensorValue)
- Serial.print("")
-
- m map(sensorValue, 0, 1023, 0,
255) //convert sensorValue to motor signal - //PWM output is from 0-255
- analogWrite(outPin, m) //send the motor
signal to the servo -
- Serial.print ("m ") //print the motor
signal to the computer screen - Serial.print(m)
- Serial.println("")
16Part 2 Schematic
Continuous rotation servo
17Part 2 Board Layout
Light sensor connector
Light sensor
5V
Pin A0
Servo connector
Resistor
Servo motor
Ground
Pin 5
18Command Reference
- pinMode(pin,mode) configures the pin to behave
either as an input or an output - Serial.begin(speed) sets the data transfer rate
for serial communication - digitalWrite(pin,value) sets the specified
pins voltage to HIGH (5V) or LOW (0V) - delay(ms) pauses the program for the specified
amount of time in milliseconds - analogRead(pin) reads the value from the
specified pin maps voltage from 0V to 5V into
integer values between 0 and 1023 - map(value, fromLow, fromHigh, toLow, toHigh)
maps the values in the from range to the to range - Serial.print() writes data to the computer in
ASCII text format