190 likes | 355 Views
Arduino Week 2 Lab. ECE 1020 Prof. Ahmadi. Objectives. 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
E N D
Arduino Week 2 Lab ECE 1020 Prof. Ahmadi
Objectives • 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
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 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 servo’s expected pulse timing
Part 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 servo’s rotation angle.
Part 1 Schematic (Manual PWM) Standard servo
Part 1 Board Layout Servo motor Pin 4 5V Servo connector Ground
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 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 “Ctrl+Shift+M” after uploading your program to open the serial communication dialog.
Board Layout 5V Pin A0 Light sensor Light sensor connector Resistor Ground
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 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…
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 connector Light sensor 5V Pin A0 Servo connector Resistor Servo motor Pin 5 Ground
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