Department of Electrical Engineering
Embedded Systems
Analog Input
Lecture 6 Dr F Bensaali
Content:
• Introduction to analog data
• Analog-to-digital conversion
• Analog inputs on the mbed
• Reading and logging data from the analog
inputs
2
Introduction to analog data
Human world signals are usually based upon continuous
analog signals that vary in time and space
o e.g. temperature variation in the room over a day
Microcontrollers are often required to process analog signals
(e.g. from microphone or temperature sensor) and must be
able to convert them first to digital data
They must also be able to convert digital signals to analog
(e.g. driving loudspeaker or DC motor)
µC
3
Analog-to-digital conversion
An analog-to-digital convertor (ADC) is an electronic circuit
whose digital output is proportional to its analog input
The ADC measures the input voltage and gives a binary
output number proportional to its size
Analog signals can be repeatedly converted into digital
representations with a resolution and at a rate determined by
the ADC
a0
a1
a2
ADC .
.
.
an
4
Analog-to-digital conversion (Cont’d)
Usually we want to work with more than one signal
o More than one ADC could be used- Costly and consumes
semiconductor space
An analog multiplexer Multiplexer
Selects one input
Voltage
Reference
can be put in front of channel
CLK
the ADC Digital
µC
output
NXP1768
Analog input
ADC n
microcontroller
includes an ADC and Input Start
multiplexer on chip Select Conversion CPU
Control
Conversion
Complete
5
Range and Resolution
Many ADCs obey the following equation:
𝑽𝑽𝒊𝒊
𝑫𝑫 = × 𝟐𝟐𝒏𝒏
𝑽𝑽𝒓𝒓
o D: the digital output value (integer value)
o Vi: the input voltage
o Vr: the reference voltage
o n: the number of bits
ADC has minimum and maximum permissible input values
o The difference between min and max values is called the range
o Often the minimum value is 0 V
o The input range is directly linked to the value of the voltage
reference – In many ADCs it is equal to reference voltage
o Input values that exceed the min and max permissible values are
likely digitized as the min and max values respectively – ‘limiting’
or ‘clipping’ action takes place 6
Range and Resolution (Cont’d)
Example 3-bit ADC
The input voltage is
gradually increased starting
from 0 V (Output 000)
If the analog input slowly
increases, there comes a
point when the digital output
changes to 001
At some points the output
will reach 111 (the max
value 𝟐𝟐𝟑𝟑 − 𝟏𝟏)
The input may increase further but cannot force any increase in
output value
7
Quantization error
By converting an analog signal
to digital there is a risk of
approximation
Any one digit output value has
to represent a small range of
analog input voltages
If the output value of 011 is
correct for the input voltage at
the middle of the step then the
greatest error occurs at either
end of the step
011
This is called Quantization Error
Greatest Correct Greatest
error value error
8
Quantization error (Cont’d)
The more steps the lower is the quantization error
More steps are obtained by increasing the number of bits in the
ADC
o This increases the complexity, cost of the ADC and the
conversion time
Example
To convert an analog signal that has a range 𝟎𝟎 − 𝟑𝟑. 𝟑𝟑 V to an 8-
bit digital signal then:
o There are 𝟐𝟐𝟖𝟖 = 𝟐𝟐𝟐𝟐𝟐𝟐 output values
o The step width is: 𝟑𝟑. 𝟑𝟑⁄𝟐𝟐𝟐𝟐𝟐𝟐 = 𝟏𝟏𝟏𝟏. 𝟖𝟖𝟖𝟖 𝒎𝒎𝒎𝒎
o The worst case quantization error is 𝟔𝟔. 𝟒𝟒𝟒𝟒 𝒎𝒎𝒎𝒎
9
Quantization error (Cont’d)
Exercise
The LPC1768 ADC is 12 bit
o How many output values we can get from it?
o What is the step width?
o What is the worst case quantization error?
10
Sampling frequency
During the conversion process, a ‘sample’ is taken repeatedly
and quantized to the accuracy defined by the resolution of the
ADC
o The more samples taken the more accurate the digital
output will be
Sampling is done at a fixed frequency called the sampling
frequency
o Sampling frequency
depends on the
maximum frequency of
the input signal
11
Sampling frequency (Cont’d)
If the sampling frequency is too low then rapid changes in the
analog signal may not be represented
Nyquist sampling criterion: the sampling frequency must be at
least double that of the highest signal frequency
o If it is not satisfied then the aliasing phenomenon occurs
12
Analog input with the mbed
The mbed has up to six analog inputs
(on pins 15-20)
The functions that can be used are:
Function Usage
AnalogIn Create a AnalogIn object connected to the specified pin
read Read the input voltage, represented as a float in the range [0.0, 1.0]
read_u16 Read the input voltage, represented as an unsigned short in the
range [0x0, 0xFFFF]
13
Combining Analog input and output
The ADC is an input device which transfers data into the
microcontroller
To know which values it has created we can use different
methods:
o Use the ADC output values to immediately control an output
variable (e.g. DAC or PWM)
o Display the ADC output values (e.g. on PC screen)
14
Combining Analog input and output (Cont’d)
Exercise Controlling LED brightness by variable voltage
#include "mbed.h"
AnalogOut Aout(p18); // create an analog output on pin 18
AnalogIn Ain(p20); // create an analog input on pin 20
int main()
{
while(1)
{
// transfer analog in value to analog out
Aout = Ain;
}
}
15
Combining Analog input and output (Cont’d)
Example Controlling LED brightness by PWM
#include "mbed.h"
PwmOut PWM1(p21);
AnalogIn Ain(p20);
int main()
{
PWM1.period(0.010); //set PWM period to 10 ms
while(1)
{
PWM1 = Ain; // set duty-cycle using analog in value
wait(0.1);
}
}
PWM pulse width is controlled with the potentiometer
16
Processing data from analog inputs
The second way of making use of the ADC output is to display
them on the PC screen
o Both mbed and the host computer must be ready to send
and receive data
o The host computer must be ready to display data
o This can be achieved using the serial communication with a
PC and a terminal emulator (e.g. Tera Term) covered in
Lecture 2 and the lab
o Writing to the computer screen is then with the printf()
function
17
Processing data from analog inputs (Cont’d)
Example Displaying values on computer
screen
#include "mbed.h"
AnalogIn Ain(p20);
float ADCdata;
int main()
{
printf("ADC Data Values...\n\r");
while(1)
{
ADCdata = Ain;
printf("%1.3f\n\r", ADCdata);
wait(0.5);
}
}
18
Processing data from analog inputs (Cont’d)
Example Scaling ADC outputs to recognized units
o The data displayed in the previous example is just a set of
numbers proportional to the voltage input
o These values can readily be scaled to give a voltage by
multiplying by 3.3
#include "mbed.h"
AnalogIn Ain(p20);
float ADCdata;
int main()
{
printf("ADC Data Values...\n\r");
while(1)
{
ADCdata = Ain*3.3;
printf("%1.3f V\n\r", ADCdata);
wait(0.5);
}
} 19
Applying averaging to reduce noise
The measured value, when the
potentiometer is not being moved, is not
always the same
o It varies around some average value
o This is due to the effect of some
interference which generate noise
int main()
{
printf("ADC Data Values...\n\r");
while(1)
{
ADCdata = 0;
for (int i = 0; i <= 9; i++)
ADCdata = ADCdata + Ain*3.3; // sum 10 samples
ADCdata = ADCdata / 10; // divide by 10
printf("%1.3f V\n\r", ADCdata);
wait(0.5);
}
} 20