Mbed BK Ed2 CH 5
Mbed BK Ed2 CH 5
Mbed BK Ed2 CH 5
If you use or reference these slides or the associated textbook, please cite the original authors’ work as follows:
Toulson, R. & Wilmshurst, T. (2016). Fast and Effective Embedded Systems Design - Applying the ARM mbed
(2nd edition), Newnes, Oxford, ISBN: 978-0-08-100880-5.
www.embedded-knowhow.co.uk
The Analog-to-Digital Converter (ADC)
frequency.
V reconstructed signal
time
samples
original signal
The Nyquist sampling criterion states that the sampling frequency must be
at least double that of the highest signal frequency. If the sampling criterion
is not satisfied, then aliasing occurs – a new lower frequency is generated,
as illustrated.
Questions from the Quiz
Functions Usage
AnalogIn Create an 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)
Controlling LED Brightness by Variable Voltage
This simple application reads the analog input, and uses it to control the
brightness of an LED by varying the voltage drive to the LED.
Controlling LED Brightness by Variable Voltage
*/
#include "mbed.h"
AnalogOut Aout(p18); //defines analog output on Pin 18
AnalogIn Ain(p20) //defines analog input on Pin 20
int main() {
while(1) {
Aout=Ain; //transfer analog in value to analog out, both
//are type float
}
}
Controlling LED Brightness by PWM
The potentiometer can be used in a similar way to alter the PWM duty
cycle. This Program Example will run on the app board, lighting the red
LED. Alternatively use the previous circuit, except that the LED should be
connected to the PWM output on pin 23. The LED brightness should again
be controlled by the potentiometer.
*/
#include "mbed.h"
PwmOut PWM1(p23);
AnalogIn Ain(p20); //defines analog input on Pin 20
int main() {
while(1){
PWM1.period(0.010); // set PWM period to 10 ms
PWM1=Ain; //Analog in value becomes PWM duty, both
are type float
wait(0.1);
}
}
Controlling PWM Frequency
The potentiometer can again be used to alter the PWM frequency, applying
this program. This can run on app board or breadboard.
#include "mbed.h"
PwmOut PWM1(p23);
AnalogIn Ain(p20);
int main() {
while(1){
PWM1.period(Ain/10+0.001); // set PWM period
PWM1=0.5; // set duty cycle
wait(0.5);
}
}
An Interlude - Displaying Values on the Computer Screen
It is possible to print values from the mbed to the PC screen, so any mbed-generated data
can be displayed. Both mbed and computer need to be configured to send and receive
data. For the computer we need a terminal emulator. The mbed site recommends Tera
Term for Windows users, or CoolTerm for Apple OS X developers. Appendix E tells you
how to set this up.
The mbed can then be made to
appear to the computer as a
serial port, communicating
through the USB connection. It
links up with the USB through
one of its own asynchronous
serial ports.
Displaying Values on the Computer Screen
/*Program Example 5.4: Reads input voltage through the ADC, and transfers to PC
terminal
*/
#include "mbed.h"
Serial pc(USBTX, USBRX); //enable serial port which links to USB
AnalogIn Ain(p20);
float ADCdata;
int main() {
pc.printf("ADC Data Values...\n\r"); //send an opening text message
while(1){
ADCdata=Ain;
wait(0.5);
pc.printf("%1.3f \n\r",ADCdata); //send the data to the terminal
}
}
C code This program uses the mbed API to set up the serial link, explained in Chapter
feature 7. It also uses the printf( ) function for the first time, along with some of its far-
from-friendly format specifiers. Check Section B9 (Appendix B) for some
background on this.
Scaling ADC Outputs to Recognised Units
Multiplying the float value read from the ADC by 3.3 converts the result into
a voltage reading. These lines can be added to the previous program.
ADCdata=Ain*3.3;
wait(0.5);
pc.printf("%1.3f",ADCdata);
pc.printf("V\n\r");
10k
Vo
/*Program Example 5.5: Inputs signal through ADC, and outputs to DAC. View DAC
output on oscilloscope. To demonstrate Nyquist, connect variable frequency signal
generator to ADC input. Allows measurement of conversion times, and explores
Nyquist limit. */
#include "mbed.h"
AnalogOut Aout(p18); //defines analog output on Pin 18
AnalogIn Ain(p20); //defines analog input on Pin 20
DigitalOut test(p5);
float ADCdata;
int main() {
while(1) {
ADCdata=Ain; //starts A-D conversion, and assigns analog value to ADCdata
test=1; //switch test output, as time marker
test=0;
Aout=ADCdata; // transfers stored value to DAC, and forces a D-A conversion
test=1; //a double pulse, to mark the end of conversion
test=0;
test=1;
test=0;
//wait(0.001); //optional wait state, to explore different cycle times
}
}
Chapter Review
• An ADC is available in the mbed; it can be used to digitise analog input
signals.
• It is important to understand ADC characteristics, in terms of input range,
resolution, and conversion time.
• Nyquist’s sampling theorem must be understood, and applied with care
when sampling AC signals. The sampling frequency must be at least twice
that of the highest frequency component in the sampled analog signal.
• Aliasing occurs when the Nyquist criterion is not met, this can introduce
false frequencies to the data. Aliasing can be avoided by introducing an anti-
aliasing filter to the analog signal before it is sampled.
• Data gathered by the ADC can be further processed, and displayed or
stored.
• There are numerous sensors available which have an analog output; in
many cases this output can be directly connected to the mbed ADC input.