Mbed Course Notes - Analog Input and Output
Mbed Course Notes - Analog Input and Output
These course notes are written by R.Toulson (Anglia Ruskin University) and T.Wilmshurst
(University of Derby). (c) ARM 2012
These course notes accompany the textbook “Fast and effective embedded system design :
Applying the ARM mbed”
1
Analog input and output
• Introduction to analog signals and data
• Concepts of analog-to-digital conversion
• Analog inputs on the mbed
• Reading and logging data from analog inputs
• Concepts of digital-to-analog conversion
• Analog output with the mbed
• Generating output waveforms
2
Introduction to analog data
• Microcontrollers are often required to interface with analog
signals
3
Concepts of analog-to-digital
conversion
• An analog-to-digital convertor (ADC) is an electronic circuit
whose digital output is proportional to its analog input
4
Concepts of analog-to-digital
conversion
• The conversion is started
Voltage Reference
by a digital input, called
here SC +
5
Concepts of analog-to-digital
conversion
• The `staircase’ visible in a 3-bit ADC
Digital
Output
111
110
101
100
011
010
001
000
Analogue
0 Vmax
Input
6
Concepts of analog-to-digital
conversion
Resolution and quantisation
• By converting an analog signal to digital, we are effectively
approximating it, as any one digital output value has to represent a
very small range of analog input voltages, i.e. the width of any of
the steps on the “staircase” n.
• If we want to convert an analog signal that has a range 0-3.3 V to an
8-bit digital signal, then there are 256 (i.e. 28) distinct output values.
Each step has a width of 3.3/256 = 12.89 mV, and the worst case
quantisation error is 6.45mV.
• The mbed uses a 12-bit ADC. This leads to a step width of 3.3/212,
or 0.8 mV; the worst case quantisation error is therefore 0.4 mV.
7
Concepts of analog-to-digital
conversion
Sampling frequency
• The more samples taken, the more accurate the digital data
will be. Samples are normally taken at fixed periods (i.e.,
every 0.2ms) and define the rate of sampling by the sampling
frequency (the number of samples taken per second).
8
Concepts of analog-to-digital
conversion
Sampling frequency
• For this reason the Nyquist sampling criterion states that the
sampling frequency must be at least double that of the
highest frequency of interest.
9
Concepts of analog-to-digital
conversion
Digital sampling of an analog signal
10
Analog inputs on the mbed
• The mbed has up to six analog inputs, on pins 15 to 20
11
Analog-to-digital conversion on the
mbed
The library functions are shown in the table below
AnalogIn An analog input, used for reading the voltage on a pin
Functions Usage
AnalogIn Create an AnalogIn, 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]
operator float operator float An operator shorthand for read()
12
Reading and logging data from analog
inputs
• Exercise 1: Attach a potentiometer output to mbed pin 20.
– Start a new mbed project and enter the code below.
– This code will continuously display the analog input value when used
with a host PC terminal application.
int main() {
pc.printf("ADC Data Values... \n\r");
while (1) {
ADCdata=Ain;
pc.printf("%f \n\r",ADCdata);
wait (0.5);
}
}
13
Reading data from analog inputs
x <= 0.2 0 0 0 0
14
Concepts of digital-to-analog
conversion
• We can represent the digital-to-analog convertor (DAC) as a block diagram
with a digital input, D, and an analog output, vo
• The output range of the DAC, vr , is the difference between the maximum
and minimum output voltages, i.e.
vr = vmax - vmin
15
Concepts of digital-to-analog
conversion
Voltage Reference
+ For each digital
value input to the
DAC, there is a
corresponding
Digital to +
analog output
D Analogue VO value given by
(n-bit Digital Converter - (Analog
input) Output)
Control Lines
16
Concepts of digital-to-analog
conversion
17
Digital-to-analog conversion on the
mbed
• The mbed has a single analog
output on pin 18
18
Digital to analog conversion on the
mbed
The library functions are shown in the table below
Functions Usage
AnalogOut Create an AnalogOut connected to the specified pin
19
Analog output with the mbed
AnalogOut Aout(p18);
20
Analog output with the mbed
#include "mbed.h"
int main() {
AnalogOut Aout(p18);
while(1) {
Aout=0.25; // 0.25*3.3V = 0.825V
wait(1);
Aout=0.5; // 0.5*3.3V = 1.65V
wait(1);
Aout=0.75; // 0.75*3.3V = 2.475V
wait(1);
}
}
21
Analog output with the mbed
• Exercise 4: Now make a sawtooth wave and view it on an
oscilloscope.
• Create a new program and enter the following code
//Sawtooth waveform on DAC output to view on oscilloscope
#include "mbed.h"
AnalogOut Aout(p18);
float i;
int main() {
while(1)
{
for (i=0;i<1;i=i+0.1)
{
Aout=i;
wait(0.001);
}
}
}
22
Analog output with the mbed
• Exercise 5: Modify your code to create a smoother sawtooth wave,
by implementing finer steps in the for loop:
23
Summary
• Introduction to analog signals and data
• Concepts of analog-to-digital conversion
• Analog inputs on the mbed
• Reading and logging data from analog inputs
• Concepts of digital-to-analog conversion
• Analog output with the mbed
• Generating output waveforms
24