Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Advanced Embedded

Systems
ADC (analog-to-digital converter)
and
Sensor Interfacing

Lecture 0902

The AVR Microcontroller and Embedded Systems using Assembly and C


by
M. A. Mazidi, S. Naimi and S. Naimi

1
2 2
ADC Modes

3 3
ADC Modes

If ADATE bit is 1,
We can use other modes as well by setting
ADTS2:0 in SFIOR register

ADTS2 ADTS1 ADTS0 Trigger Source


0 0 0 Free Running mode
0 0 1 Analog Comparator
0 1 0 External Interrupt Request 0
0 1 1 Timer/Counter0 Compare Match
1 0 0 Timer/Counter0 Overflow
1 0 1 Timer/Counter1 Compare Match B
1 1 0 Timer/Counter1 Overflow
1 1 1 Timer/Counter1 Capture Event

4 4
Differential Mode ADC

5 5
Differential Mode ADC

6 6
ADC on Arduino
 analogRead(pin)
 pin: the name of the analog input pin to read
from. A0 to A7 on UNO
 Returns: Digital Equivalent of Analog value

 int val = analogRead(A0);

7 7
Example of Different
Analog Sensors

8
8
Analog Input Sensors
Vcc , GND , Signal

LM 35 MQ Series Liquid Mpx5700


Gas Sensor Level Gas Pressure
Sensor Sensor

Soil MicroPhone Flame Magnetic/Current


Moisture Sound Sensor Sensor
Sensor Sensor
9
Resistive Sensors

Flex Sensor Force Sensor


(Bend/Deflection) (Pressure)

Light Dependent
Resistor
10
Temperature Sensor (LM35)

 Convert temperature to voltage


 10mV for each degree

11
Temperature vs. Vout for AVR
(with Vref = 2.56)  
 
S

12
Get data from LM35 sensor interfaced to channel 0 (ADC0) of ADC and
displays the result on Port D.
#include <avr/io.h> #include <avr/io.h>

int main (void) int main (void)


{ {
DDRB = 0xFF; DDRB = 0xFF;
DDRD = 0xFF;

ADCSRA= 0x87; ADCSRA= 0x87;


ADMUX= 0xC0; ADMUX= 0xE0;

while(1) while(1)
{ {
ADCSRA = ADCSRA | 0x40; ADCSRA = ADCSRA | 0x40;

while((ADCSRA & 0x10)==0); while((ADCSRA & 0x10)==0);

ADCSRA = ADCSRA | 0x10; ADCSRA = ADCSRA | 0x10;

PORTD = ADCL;
PORTB = ADCH; PORTB = ADCH;

} }
} }
13
DAC
(digital-to-analog
converter)

14
14
DAC (digital-to-analog converter)
Vout

4
3
2

VREF 1
Step size = 0 input
Num of steps 00 01 10 11

VOUT = num × step size

Binary number n
(input) DAC VOUT

Vref

15
Connecting a DAC to the Microcontroller

Micro. +5V +5V


DAC0808
5K
Vcc
Vref(+)
Vref(-)
PD0 D0 5K 5k

To scope
OUT –
VOUT = 0 to 10V
+ 1k
Comp
0.1uF
100 pF
PD7 D7
VEE -12V

GND

16
Generating a saw-tooth wave using DAC
#include <avr/io.h> LDI R16, 0xFF
OUT DDRD, R16
int main (void)
{
unsigned char i = 0; //define a counter AGAIN:
DDRD = 0xFF; //make Port D an output

while (1) //do forever INC R16


{
PORTD = i; //copy i into PORTD to be converted OUT PORTD, R16
i++; //increment the counter
}
} NOP
NOP
Volt
VREFH RJMP AGAIN

Time

17

You might also like