3/17/2020
mbed I2C API
• mbed API uses 8 bit I2C addresses, so make sure to take
any 7 bit address from datasheets and left shift by 1
• .frequency() Sets I2C clock frequency. Older devices may
need a slower I2C clock near 100 or 400Khz. Default is
1Mhz.
• May need to set a R/W register address in addition to I2C
address on newer complex devices such as IMUs
• Some Devices will auto increment register address
232
Many newer I2C devices and mbed APIs
support faster multi-byte R/W Cycles
233
1
3/17/2020
mbed Timer APIs
• Timer - Create, start, stop and read a timer
• Timeout - Call a function after a specified delay
• Ticker - Repeatedly call a function
• wait - Wait for a specified time
• time - Get and set the realtime clock
234
Mbed Timer APIs
• All use one hardware timer (timer 3 on LPC1768)
• Use hardware timers for short time delays (<30
min)
• Use Real Time Clock (RTC) time for longer delays
• Must set RTC before it starts running
• External interrupts can also start and stop timers
235
2
3/17/2020
mbed Timer Applications
• Sonar – time delay*speed of sound is distance
measurement
• Sonar driver uses a timer with interrupts
236
mbed Timer Applications
• Most processors have multiple timers
– Several General Purpose
– Watchdog
– OS time slice
– Real Time Clock
• A Timer is needed in an Operating System for Time
Slice Interrupts (1-100ms typical)
237
3
3/17/2020
Mbed Timer Tutorial
• There are several additional types of
hardware timers found on most
microcontrollers.
• The following tutorial describes which
applications that they are typically used
for:
– Using Hardware Timers
238
mbed USB Examples
• On LPC7168 mbed board, a USB Virtual Com
Port is included and the on-board flash appears
as a USB flash drive using firmware in the USB
microcontroller on back. Not all mbed boards
support additional USB host/slave interfaces
• USB drivers are more complex than other I/O
interfaces. Most USB and network drivers have
threads and need the RTOS
• Flash drive, mouse, keyboard and HID device
examples
239
4
3/17/2020
mbed USB Examples
• LPC1768 chip has a USB interface that
can support using mbed as a USB Slave
or Host device, but it also needs a USB
connector breakout board and a USB
software driver
240
mbed Filesystem
• Filesystem drivers are available for SD cards or a USB
Flash drive – just add breakout board and plug in media
• PC can also R/W the files when media is mounted on PC
• Filesystem calls several C++ virtual functions for R/W–
so can be ported to other digital storage media with
additional work
241
5
3/17/2020
mbed CAN bus Examples
• Used in cars to send data between
microcontrollers and read OBD (On-Board
Diagnostic) II error codes
• Needs a CAN driver board and OBD II cable for
LPC1768
242
mbed CAN bus Examples
• Only a few very basic OBD II & CAN codes are
public domain – limits student projects
• Codes used to help repair cars by plugging in
OBD II cable under dash and using a OBD II
code reader
243
6
3/17/2020
I/O Device Interfacing Case Studies
• Several examples of different I/O devices
with code and wiring information for parts
used in laboratory projects are available
on wiki pages
• These will now be examined in depth
• They will use the standard hardware I/O
interfaces introduced earlier
• Code examples use the mbed APIs
244
I/O Device Interfacing Case Studies
• Digital Input, Output and Interrupts
– Using Pushbuttons and Debouncing
– Relays
– Solenoids
– Stepper Motors
– Navigation Switch (Digital Joystick)
245
7
3/17/2020
I/O Device Interfacing Case Studies
• PWM
– Dimming LEDs
– Using RBG LEDs
– Audio Output with a Speaker
– DC Motor Speed Control
– RC Servos
– Electronic Speed Control modules (ESC)
246
I/O Device Interfacing Case Studies
• Analog Input
– Potentiometer
– Temperature Sensor
– Light Sensor
– MEMs Microphone
– Analog Joystick
247
8
3/17/2020
I/O Device Interfacing Case Studies
• Analog Output
– Audio output using a speaker
248
I/O Device Interfacing Case Studies
• I2C
– I/O Expander
– Touch keypad
– LIDAR distance sensor
– 9 DOF IMU
249
9
3/17/2020
I/O Device Interfacing Case Studies
• SPI
– I/O Expander
– Using a uSD card for file I/O
250
I/O Device Interfacing Case Studies
• USB Host Device
– Using a USB Flash Drive
– Using a USB Keyboard
– Using a USB Mouse
• USB Slave Device
– Simulating USB Mouse input
– Simulating USB Keyboard input
251
10
3/17/2020
I/O Device Interfacing Case Studies
• Serial
– Using a TTL serial LCD module
– Using an RS232 serial device
– Using a GPS module
– Interfacing to other microprocessors
• Wi Fi module
• Bluetooth module
• iRobot Roomba or Create Robot
252
Driving Motors and Relays
• High current devices like motors, relays,
solenoids, buzzers, speakers, and high output
LEDs can require more than 500MA of current
• Even though voltage levels may be the same,
digital outputs from a GPIO (parallel) port typically
drive only 5-20MA of current
• They cannot drive high current devices directly
and trying to do so will likely blow out the output
circuit
• In some cases, driver circuits will also need to
boost voltage levels in addition to current
253
11
3/17/2020
Driver Circuits
• A higher current driver circuit must be added
after the digital output pin and before the device
• A driver circuit typically uses a discrete power
transistor to amplify current levels
• For DC motors, consider using an H-bridge
circuit module. It contains four power transistors
than can also reverse the motor.
• Diodes are often used for additional protection
across the load on motors and relays. When you
turn off the current in an inductive load it
generates a reverse voltage spike that might
damage the transistor (back EMF). The diode
shorts it out.
254
Driving a high current Load
Vcc
Discrete Power
Digital Transistor
Logic LOAD
Control
Signal Optional Voltage
Suppression Diode
255
12
3/17/2020
Use Decoupling Capacitors
on Power Supply lines!
• Decoupling or bypass capacitors should be placed
across the power supply lines (Vcc and GND) on each
board or near the high current device.
• These capacitors help filter out the noise spikes on the
power supply that are generated by switching high
current loads.
• Processor chips also need them as shown in the
manufacturer’s data sheets!
256
Decoupling Capacitors
Vcc
10-1000uf .01-.1uf
Electrolytic Ceramic
GND
•Two capacitors (made of different dielectric materials) are
typically connected in parallel from Vcc to GND.
• A larger electrolytic capacitor (i.e., 100+ uf) and a smaller one
(i.e., .01 to .1 uf) in parallel are needed to filter noise
257
13
3/17/2020
This Phidgets relay board contains two electromechanical
relays that can switch loads up to 5 A at 240 VAC or 10 A
at 100 VDC. The maximum operation speed is 20 times
per minute and lifetime is 105 to 107 cycles. The control
input is a digital logic signal. The control input feeds into a
discrete power transistor that provides the higher current
levels needed to drive the relay’s coil (5V at 75 mA).
258
Limitations of Mechanical Relays
Easy way to switch high current and/or high
voltage loads and provides electrical
isolation, but…
• Number of lifetime ON/OFF cycles is limited by
mechanical wear (typically 106 to 107 cycles)
• Slow ON/OFF times – around 20 per minute
• Contacts can wear out due to arcing on inductive loads
(perhaps as few as 105 cycles) even on rated loads
• Oxidation on relay contacts can be a problem on low
voltage analog signals (around 2V needed to initially
punch through oxidation layer between metal contacts)
• Worn out relays will sometimes stick
259
14
3/17/2020
H-Bridge - DC Motor Driver Circuit
260
H-Bridge Control Functions
Input Function Operation
10 Forward DC Motor runs in the
forward direction
01 Reverse DC Motor runs in the
reverse direction
00 Stop Motor is not
connected – Coasts
11 Brake* or Motor Terminals
Shorted or
Short Power Supply Power Supply
(not allowed!) Shorted!
*The Brake function requires a more complex decoder circuit to control the power
transistors. Check the H-Bridge data sheet to make sure it is supported before using
it. In some old very simple H-Bridge circuits using only one type of power transistor
(i.e., N or P), the fourth state must be avoided (i.e., illegal state) and it will short out
the power supply! Most new H-bridge ICs now support the brake feature, but in
some cases it might still overheat the IC when used too frequently.
261
15
3/17/2020
H-Bridge Example - Forward
HIGH LOW
262
H-Bridge Example - Reverse
LOW HIGH
263
16
3/17/2020
Figure 3.10 Fairchild FAN8100N Low Voltage Dual H-Bridge DC Motor Driver IC.
Images courtesy of Fairchild Semiconductor.
264
Higher current H-Bridge modules often use discrete power
transistors assembled on a board. This dual H-Bridge module
switches up to 10 amps at 24V DC. The eight power
transistors can be see arranged on the right side of the board.
Photograph courtesy of RoboticsConnection.
265
17
3/17/2020
This ST VNH2SP30 30Amp DC MOSFET motor driver IC
contains an H-bridge circuit. It supports 5V PWM speed
control up to 20Khz. Image courtesy of www.pololu.com.
266
Electromechanical Relays
This electromechanical relay contains an electromagnetic coil (right side of
image) that moves a metal arm to make and break an electrical connection.
Photograph courtesy of Omron.
267
18
3/17/2020
Electromechanical Relay Limitations -
why other approach may be needed
•The number of lifetime ON/OFF cycles is limited by mechanical wear (typically
106 to 107 cycles)
•They have slow ON/OFF times – around 20 per minute. Too slow for motor
speed control or dimming lights.
•Relay contacts can wear out due to arcing on inductive loads (perhaps as few
as 105 cycles) even on rated loads.
•Oxidation on relay contacts can be a problem on low voltage analog signals.
(around 2 volts is needed to initially punch through the oxidation layer that
occurs between any two metal contacts)
•Worn out relays will sometimes stick due to mechanical wear and an audible
click is typically heard during switching.
268
Solid State Relays
• Optical isolation between input and output load
• Unlike electromechanical relays, No moving parts
• May be fast enough for motor speed control and dimming lights
• Resistant to shock and vibration
• 150X more reliable than mechanical relays
The NEC SSR chip family seen above can switch
AC or DC from .2 to 2 Amps at 60-600 Volts
269
19
3/17/2020
This optically isolated solid state relay module can control a
120V AC 10 Amp circuit using a digital logic input signal.
Photograph courtesy of Opto22.
270
NEC SSR Device Schematic
INPUT Photovoltaic OUTPUT
GaAs LED Diode Array FET Switch
Diode Stack Thyristor/Charge Control
271
20
3/17/2020
This Phidgets SSR board uses an NEC SSR IC and
can switch 2 Amps at 40 VDC or 28 VAC. A digital logic
output bit controls it. Typically, this digital control signal
would be a bit coming from a parallel I/O (GPIO) output
port on a computer.
272
Mbed Driver Tutorial and Code
Examples
• See the wiki page at:
– https://developer.mbed.org/users/4180_1/note
book/relays1/
• It contains links to mbed code and wiring
examples of driver ICs, breakout boards,
relays, H-bridge modules, AC dimmers,
and other devices used to drive high
power and high voltage devices using a
microprocessor.
273
21
3/17/2020
I/O Device Interfacing Case Studies
• More complex systems with several I/O
devices and drivers:
– Model Trains
– ESC Motor drivers for Quadcopters
– Shadow Robot Base
– IoT Clock using uLCD and Ethernet
274
22