You are on page 1of 28

Chapter 13

Sensor Interfacing
Topics
■ Analog Interfacing using ADC
■ Sampling : Nyquist rate
■ Quantization: Step size resolution

■ ADC Major Characteristics


■ ADC programming in AVR
■ ADC Registers
■ Program Examples

■ Signal Conditioning for Sensor Interfacing

2
Sensors
■ Sensors are devices that measure or detect an event or
change in the environment.
■ Generally speaking, sensors output analog signals.

■ Even though we are developing digital systems, however,


in many applications, it is inevitable to deal with analog
signals (e.g. generated by sensors).
■ In order to process analog signal, we need first to
convert it to digital, using ADC.
3
Analog interfacing using ADC

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.

■ In order to sample a signal adequately, the sampling


frequency must be adequate. But how fast should it be?
5
Nyquist Rate

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.

3. Vref : Input reference voltage


✔ Dictates the step size with ADC resolution
4. Dout : Digital data output
✔ Parallel or serial ADC outputs

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.

■ Clock driving ADC


■ The clock must be between (50 – 200) KHz.
■ The closer the clock to 200KHz, the faster the conversion
time.
■ The ADC clock can be generated using a pre-scalar.
■ Available pre-scalars are (2, 4, 8, 16, 32, 64, 128).
■ Example: Assume 1MHz crystal, find the suitable pre-scalar
to achieve the highest possible ADC operating frequency.

f = 106 / 200x103 = 5 🡺 Next highest is 8

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

3. ADC: Holds the result of conversion

17
ADMUX

■ MUX0-MUX3: input select


■ Only ADC0 to ADC5 available

■ REFS1-REFS0: Vref selection

■ ADLAR (ADC Left Adjust Result)


■ 0: right adjust the result
■ 1: left adjust the result

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

■ If ADLAR is set (i.e. 1), then the result will be left-aligned as


shown below:
ADCH ADCL
b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 0 0 0 0 0 0

■ If we are interested in 8-bit resolution, then left align can be used


and the result is read from ADCH only (the two LSB stored in
ADCL are ignored).
19
AD Control & Status Register (ADCSRA)
■ The figure shows the register bit map:

ADIE

Pre-scalar

Start Conversion 000


complete fclk / 2
conversion 001
010 fclk / 4
Interrupt 011 fclk / 8
Enable ADC Enable
100 fclk / 16
101 fclk / 32
110 fclk / 64
111 fclk / 128

20
How to use ADC in AVR
START

1. Select Channel and Vref (ADMUX)


2. Set Pre-scalar (ADCSRA)
3. Enable ADC (ADCSRA)

Start Conversion (ADCSRA)

No
Is Conversion
Complete?
(ADCSRA)

Yes

Read result (ADC)

21
ADC Example 1

Vref

22
ADC Example 1
int main()
{
DDRD = 0xFF; // Configure Port D as output

ADMUX = 0x22; // WHY?(Channel 2, ADLAR, Aref)

ADCSRA = 0x86;// WHY?(Prescalar 64, Enable ADC)

while (1) {
ADCSRA |= (1 << ADSC); // WHY?

while ( (ADCSRA & 0x10) == 0) ; // WHY?


ADCSRA |= (1<<ADIF); // Clear end of conversion flag

PORTD = ADCH; // WHY?


}

return 0;
} 23
ADC Example 2

24
ADC Example 2
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>

int main (void)


{
DDRB = 0xFF; //make Port B an output
DDRD = 0xFF; //make Port D an output

ADCSRA= 0x87; //make ADC enable and select ck/128


ADMUX= 0xC0; //1.1V Vref, ADC0, right-justified

while(1)
{
ADCSRA |= (1<<ADSC); //start conversion

while((ADCSRA&(1<<ADIF))==0); //wait for conversion to finish

ADCSRA |= (1<<ADIF); // Clear end of conversion flag

PORTD = ADCL; //give the low byte to PORTD


PORTB = ADCH; //give the high byte to PORTB

_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>

int main (void)


{
DDRB = 0xFF; //make Port B an output
DDRD = 0xFF; //make Port D an output
ADCSRA= 0x8F; // make ADC and Interrupt enable and select ck/128
ADMUX= 0xC0; // 1.1V Vref, ADC0, right-justified
Sei (); // global interrupt enable
ADCSRA |= (1<<ADSC); // start conversion
while(1) ; // Wait foreever
return (0);
}

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

Lecture note on resistance to voltage conversion:


https://soundlab.cs.princeton.edu/learning/tutorials/sensors/node17.html 27
RECOMMENDED PROBLEMS
PROBLEMS HIGHLIGHTED IN RED HAVE HIGHER PRIORITY

CHAPTER-13 [ADC Interfacing]

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

You might also like