Arduino Week 2 Lab

Download Report

Transcript Arduino Week 2 Lab

Arduino Week 2 Lab

ECE 1020 Prof. Ahmadi

Objectives

1.

2.

 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 1.

Use the built-in PWM pins on the Arduino where you only need to set the duty cycle  2.

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; int pulse = 700; void setup() { pinMode(servoPin, OUTPUT); Serial.begin(9600); } void loop() { digitalWrite(servoPin, HIGH); delayMicroseconds(pulse); digitalWrite(servoPin, LOW); delay(20); } //variable to store the servo pin number //variable to store the pulse duration //set the servo pin as an output //set serial data transfer rate //send 5V to the servo //for pulse microseconds //send 0V to the servo //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

5V Ground Servo motor Pin 4 Servo connector

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; int sensorValue = 0; { void setup() Serial.begin(9600); } void loop() //variable to set the sensor input pin //set serial data transfer rate { } 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 Light sensor connector Pin A0 Resistor 5V 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; int outPin = 5; int sensorValue = 0; int m = 0; //variable to set the sensor input pin //variable to store the output pin //variable to store the value coming from the sensor //variable to store the motor signal (i.e. the voltage) //voltage controls the speed of a continuous servo } { void setup() Serial.begin(9600); pinMode(outPin, OUTPUT); //set serial data transfer rate //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 = "); Serial.print(m); Serial.println(";"); //print the motor signal to the computer screen

Part 2 Schematic

Continuous rotation servo

Part 2 Board Layout

Light sensor connector Light sensor Pin A0 5V 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