Arduino Week 2 LabECE 1020Prof. Ahmadi
Objectives
1. Control the rotation of standard servo motor A standard servo motor is limited in its rotation
between 0 and 180 degrees
2. Control the speed of a continuous rotation servo motor based on the light intensity
A continuous rotation servo motor can rotate freely without restriction
Introduction 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 it’s
torque rating
PWM Generation
There are 2 different ways in which you can generate a PWM signal1. Use the built-in PWM pins on the Arduino where
you only need to set the duty cycle2. 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 servo’s expected pulse timing
Part 1 – Controlling Servo Rotation (Manual PWM)
int servoPin = 4; //variable to store the servo pin numberint pulse = 700; //variable to store the pulse duration
void setup(){
pinMode(servoPin, OUTPUT); //set the servo pin as an outputSerial.begin(9600); //set serial data transfer rate
}
void loop(){
digitalWrite(servoPin, HIGH); //send 5V to the servodelayMicroseconds(pulse); //for pulse microsecondsdigitalWrite(servoPin, LOW); //send 0V to the servodelay(20); //for 20 milliseconds
}
You can change the duration of the pulse to vary the servo’s rotation angle.
Part 1 Schematic (Manual PWM)
Standard servo
Part 1 Board Layout
Ground
5V
Pin 4
Servoconnector
Servomotor
How to read a light sensor[What 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.
How to read a light sensor[How 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]
Reading the Light Sensorint sensorPin = A0; //variable to set the sensor input pinint 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 “Ctrl+Shift+M” after uploading your program to open the
serial communication dialog.
Light Sensor Schematic
Board Layout
Light sensor
5VPin A0
Ground
Light sensor connector
Resistor
Now let’s combine the light sensor with a servo motor to build a light-sensitive servo that rotates at speeds proportional to the light intensity.
Part 2 – Controlling Servo Rotation Speed as a Function of Light Intensity (1)
int sensorPin = A0; //variable to set the sensor input pinint outPin = 5; //variable to store the output pinint sensorValue = 0; //variable to store the value coming from the sensorint 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…
Part 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(";"); }
Part 2 Schematic
Continuous rotation servo
Part 2 Board Layout
Light sensor
Servo connector
5VPin A0
GroundPin 5
Light sensor connector
Servo motor
Resistor
Command 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 pin’s 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
http://arduino.cc/en/Reference/HomePage