Top Banner
EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross
24

EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Apr 01, 2015

Download

Documents

Kristofer Corke
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

EMS1EP Lecture 9Analog to Digital Conversion (ADC)

Dr. Robert Ross

Page 2: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Overview (what you should learn today)

• Revision of Analog and Digital• ADC + Quantisation• Examples of Analog Inputs• Worked examples• Minor Project• Major Project

Page 3: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Analog/Digital

Microcontroller(LArduino)

ADCAnalog

to Digital

Converter

DACDigital

to Analog

Converter

Transducer(Sensor) Actuator

Physical Variable

(Sound, light, pressure, temp, ect)

Control Physical Variable

(Sound, light, pressure, temp, ect)

Analog Voltages

Digital Voltages

Page 4: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Analog to Digital Converters (ADC)

• Measure an analog value and provide a digital representation of this value

• Like a digital multimeter for your microcontroller

• Inside the Arduino microcontroller• Several different techniques used (covered in

second year microcontrollers)

Page 5: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

ADC quantisation

• Converting a continuous value into a digital number• Arduino has a 10bit ADC – (210 = 1024 voltage levels)• Value returned in the range of 0-1023 (corresponding

to 0-5V)• 5/1023 = 4.89mV/step • Each time you increase the voltage by 4.89mV the

digital value should go up 1 step

Page 6: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

ADC on the LArduino

• LArduino has 4 analog inputs (marked Analog 0-3)

• Analog inputs can be connected to all of these inputs at the same time

• Can only read one analog input at a time

Page 7: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Using the ADC

• Analog pins don’t need to be setup in the setup function• To read an analog value use the analogRead() command.• Syntax:

int analogRead(<ADC Pin>);Returns the analog result<ADC Pin>: specifies which ADC pin you wish to read (i.e. Pins 0-4)

Typical use:int ADC_Result;ADC_Result = analogRead(2);

Page 8: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Examples of Analog Inputs

Pressure

Acceleration

Temperature

Voltage (Variable resistor forms voltage divider)

Light

Page 9: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Worked Example: Potentiometer

• Interface a potentiometer to an analog input on the Arduino

• Every 500ms read the value of the potentiometer and send it via the serial port to the PC

• If the value is more than 500 turn ON an LED – else turn off LED

Page 10: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Worked Example: Potentiometer

• Circuit Diagram

LArduino Board

5V

Analog Pin2

Voltage (Variable resistor forms voltage divider)

Pin5

5V

Page 11: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Worked Example: Potentiometerint pot1 = 2;int led1 = 5;int ADCValue;

void setup(){ Serial.begin(<baud rate>); //Setup serial port

pinMode(led1, OUTPUT); //Setup LEDdelay(500);

}

Page 12: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Worked Example: Potentiometervoid loop(){

ADCValue = analogRead(pot1); //Read analog valueSerial.print(“ADC Result: “);Serial.println(ADCValue);

if(ADCValue > 500){ //Turn LED ondigitalWrite(led1,LOW);

}else{ //Turn LED off

digitalWrite(led1,HIGH);}delay(500); //500ms delay

}

Page 13: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Measuring multiple ADCs

• Only one ADC can be measured at a time• If more than one need to be measured step

through and measure one after anotherADCValue1 = analogRead(ADC1);ADCValue2 = analogRead(ADC2);ADCValue3 = analogRead(ADC3);

Page 14: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Worked example: Light Sensor

• Create a ‘garden light sensor’ • A photodiode is used to sample how much light there

is and gives an analog voltage output• If the voltage is below a preset threshold then turn

on another LED – otherwise turn off the LED

LArduino Board

Analog Pin1

5V

Pin6

5V

100K

Page 15: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Worked Example: Light Sensorint pd1 = 1;int led1 = 6;

void setup(){

pinMode(led1, OUTPUT); //Setup LED}

void loop(){ //You fill in this section

}

Page 16: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Minor Project: Level Crossing System

• Construction of a train level crossing system• Description – a level crossing system using 1

servo as the boom gate, activated by a debounced switch and with flashing warning lights and a audible buzzer alarm

• Deliverables:– Working System for demonstration day– Schematics (completed in Altium)– Code

Page 17: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Minor Project: Level Crossing System

• When the switch is pressed (and at some point released):– The boom gate should come down (servo turn 90o)– Two LEDs should alternately flash at 1Hz– A buzzer should be controlled to beep in 0.5 second bursts (use PWM

with 50% duty cycle)• The switch is a debounced switch. When the switch is pressed

a second time the boom gate should come up, the LEDs should stop flashing and the buzzer should remain silent

• See handout for more details• Minor project is to be completed in the lab (may require a

small amount of extra outside lab time)• Demonstrations in lab 8

Page 18: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

What the minor project should look like

Page 19: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Major Project

• Build a robot• Infrared photodiodes on the bottom (allow it

to sense light/dark objects• Battery powered• Control algorithms• Applications

– Line tracking/following– Staying inside a paddock– Racing

Page 20: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

What the major project should look like

Page 21: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Continuous Rotation Servos• By default servo motors only have about 180O of

motion that they can travel over• Servos can be modified for continuous rotation (they

can go all the way around)• PWM signal controls speed not position of

continuous rotation servos• Involves:

– Removing feedback potentiometer (variable resistor)– Soldering in resistors– Cutting notch out of gears

• Instruction video will be provided in the labs

Page 22: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Control Algorithms• Three light sensors on the bottom of the robot used

to detect the lines• For line following want to keep it close to the middle

sensor• Possible readings:

– No Sensors (but last seen on S1)– S1 only– S1+S2– S2 only – S2+S3– S3 only– No Sensors (but last seen on S3) S1 S2 S3

Page 23: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Control Algorithms• Suggested algorithm is a proportional one

– The further away the robot is from the line (S2 only) the larger change in its relative speed between the wheels it should make

– Should also give the robot a little forward bias so it keeps moving forward and doesn’t get stuck in tight bends

• Can switch between just driving forward and control algorithm • Will need to experiment with different speeds and

find a mix between speed and stability• Execution tests will have races with the fastest robots

receiving bonus marks (but they must not leave the track)

Page 24: EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.

Summary(What you learnt in this session)

• Many sensors that our microcontroller needs to read are analog

• An ADC enables the microcontroller to read an analog value and convert it to a digital number

• Introduction to the minor and major project tasks