Professional Documents
Culture Documents
Sensor Interfacing
Topics
■ Analog Interfacing using ADC
■ Sampling : Nyquist rate
■ Quantization: Step size resolution
2
Sensors
■ Sensors are devices that measure or detect an event or
change in the environment.
■ Generally speaking, sensors output analog signals.
4
Sampling Frequency
■ Sampling frequency: Rate at which the signal is sampled.
Notice how increasing sampling frequency produces a better
digital representation of the analog signal.
Sampling Signal
frequency frequency
6
Nyquist Rate
7
Quantization
8
Effect of Increasing Quantization Bits
9
ADC
ADC major characteristics
n
1. Resolution : n bit resolution Vin ADC Output
(binary number)
✔ n = 8, 10, 12, 16 .. etc.
✔ Higher resolution provides smaller step Vref
size (step size: the smallest change that
can be distinguished by ADC).
2. Conversion time : time it takes the ADC to
convert the analog input to a digital number.
✔ Dictated by clock source and method used
for data conversion.
10
ADC
11
ADC
12
ADC
More ADC characteristics
5. Parallel versus serial ADC
✔ Serial ADC has fewer pins and
smaller packages.
✔ More CPU time is needed to get the
converted data
6. Multiple Analog input channels.
✔ Multiplexing of analog input is widely
used.
✔ ADC chips with 2, 4, 8 or 16
channels in a single chip.
13
ADC in ATmega8
+5 V
1. Vital Pins:
1. Power
■ VCC
■ Ground
2. Crystal +5V
■ XTAL1 +5V
■ XTAL2
3. Reset
2. I/O pins
• PORTB, PORTC, and PORTD
4. Internal ADC pins
• AREF, AVCC, ADCn
• Analog In : ACD0 to ADC5
14
Using ADC in AVR
■ The AVR ATmega8 microcontroller has a built-in ADC with
up to 6 channels. 10-bit quantizer is used.
15
ADC in AVR
Bandgap
Reference
16
Programming ADC
■ The following Registers are related to ADC:
1. ADCSRA:
■ Enable ADC
■ Start conversion
■ Conversion complete flag
■ Pre-scalar selection
■ ADC interrupt Enable
2. ADMUX:
■ Selects channel
■ Selects reference voltage Vref
■ Result alignment
17
ADMUX
18
ADLAR Flag
■ ATmega8 uses 10-bit quantizer. The result of conversion is split
between two 8-bit registers: ADCH & ADCL.
■ If ADLAR is cleared (i.e. 0), then the results will be right-aligned
as shown below:
ADCH ADCL
0 0 0 0 0 0 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0
ADIE
Pre-scalar
20
How to use ADC in AVR
START
No
Is Conversion
Complete?
(ADCSRA)
Yes
21
ADC Example 1
Vref
22
ADC Example 1
int main()
{
DDRD = 0xFF; // Configure Port D as output
while (1) {
ADCSRA |= (1 << ADSC); // WHY?
return 0;
} 23
ADC Example 2
24
ADC Example 2
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
while(1)
{
ADCSRA |= (1<<ADSC); //start conversion
_delay_ms(100);
}
}
25
ADC Programming using Interrupts
■ To avoid tying down the microcontroller, we can use
interrupts:
■ Set to High A/D interrupt enable (ADIE) in ADCSRA register
■ Properly define ISR(ADC_vect)
■ ADC
#includeExample
<avr/io.h> 2 using interrupts :
#include <avr/interrupt.h>
ISR (ADC_vect)
{
PORTD = ADCL; //give the low byte to PORTD
PORTB = ADCH; //give the high byte to PORTB
ADCSRA |= (1<<ADSC); // start conversion
}
26
Sensors and signal conditioning
■ Sensor: Converts a physical signal (e.g. light,
temperature, humidity, etc.) to an electrical signal
(e.g. resistance, voltage, current, capacitance, etc.)
■ Example LM35 :
■ convert temp. to voltage
■ 10mV for each degree
■ Linear characteristic
■ The output of some sensors (e.g. PT100) is in form of
resistance, or capacitance (Some humidity sensors)
■ Signal conditioning: Some sensor outputs need to be
converted to proper voltage values
■ PT100 : Resistor to voltage conversion
■ LM34: Voltage amplifier
Problems
S13.1 (1, 2, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16) S13.2 (25, 26,27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43) S13.3 (44,
45, 46).
28