This is only a preview of the February 2022 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
KickStart
b y M ike Tooley
Part 7: Plug and play with I2C
Our occasional KickStart series aims to
show readers how to use readily available
low-cost components and devices to
solve a wide range of common problems
in the shortest possible time. Each of the
examples and projects can be completed
in no more than a couple of hours using
Fig.7.1. A low-cost real-time clock (RTC)
designed for use with a wide range of
I2C-compatible microcomputer and
microcontroller systems.
N
owadays, microcontrollers
and single-board computers
usually provide you with
a very handy method of connecting
external devices using a popular ‘twowire’ inter-integrated circuit interface
supported by a wide range of low-cost
bus-compatible devices. This versatile
interface is variously known as ‘IIC’,
‘I2C’, or ‘I2C’, and it will allow you easily
to interface your controller with a host
of devices, such as I/O multiplexers;
sensors for temperature, pressure and
humidity; magnetometers; real-time
clocks; motion sensors and a variety of
display controllers. I2C is also found in
some interesting programmable devices,
such as the FM radio chip featured in
this article.
I2C is a very simple bus system where
bidirectional serial data appears on one
line (SDA) and a clock signal is sent on
a second line (SCL). It thus requires only
two bus lines plus, of course, a common
ground connection. In order to avoid
conflict, each device connected to the
I2C bus is software addressable using a
unique address. The advantage of these
minimal connecting requirements is that
equipment based on I2C can be very easily
modified and expanded without the need
for major hardware changes.
42
‘off-the-shelf’ parts. As well as briefly
explaining the underlying principles and
technology used, the series will provide
you with a variety of representative
solutions and examples, along with just
enough information to be able to adapt
and extend them for your own use.
This seventh instalment provides an
introduction to the popular and simple I2C
interface. To help you get started, we’ve
provided a useful practical example in the
form of an Arduino Nano-based FM radio
using I2C for controlling both the radio
module and an OLED station display.
I2C was the brainchild of
Philips, but several of its leading
competitors (including Motorola/
Freescale, NEC, Siemens,
STM and Texas Instruments)
have developed their own
I 2 C-compatible products.
In addition, Intel’s SMBus
provides a stricter definition of
I2C that helps to improve the
interoperability of I2C devices
Fig.7.2. I2C bus with two bus masters and three slaves.
from different manufacturers.
n On-chip filtering is usually incorporated
Fig.7.1 shows a typical I2C
to reject transient noise spikes that can
device, a battery-backed real-time clock
be present on the bus data line
(RTC) module. I2C bus compatibility
n The number of devices that can be
makes it very simple to add this device
to a wide range of microcomputer and
connected to the same bus is limited
microcontroller systems. Note that in
only by the maximum specified bus
order to simplify interconnection the
capacitance (400pF). This makes I2C
2
I C bus connections are duplicated on
highly expandable.
opposite edges of the board.
Key features of I2C
Bus interface logic
The single data line is shared between
multiple devices, so I2C uses a system
Key features of the I2C bus include:
n Only two bus lines (plus ground) thus
of addressing to identify the device that
it needs to communicate with. Data
minimal interconnecting wiring
n Each device connected to the I2C
communication is initiated by means of
a unique start sequence. This involves
bus is software addressable with a
pulling the data line (SDA) low while
unique address (see Table 7.1 for
the clock line (SCL) is high. This can
some examples)
n Simple master/slave relationships
be achieved by using very simple bus
interface logic where each of the bus lines
exist at all times, with bus masters
are normally pulled high and driven low
operating as master-transmitters or as
when activated by a device connected to
master-receivers
n I2C is a true multi-master bus, which
the bus (see Fig.7.2).
Fig.7.3 shows a simple bus transaction
incorporates collision detection
which begins with a start condition (S)
and arbitration to prevent data
and ends with a stop condition (P). Note
corruption if two or more masters
simultaneously attempt to
initiate data transfer
n Serial, 8-bit-oriented,
bidirectional data transfers
can be made at up to 100 kbit/s
in standard mode, and up to
400 kbit/s in fast mode. This
is perfectly adequate for most
non-critical applications
Fig.7.3. An I2C bus transaction.
Practical Electronics | February | 2022
Fig.7.4. Simplified I2C bus interface logic.
how address information is transmitted serially on the SDA line
during clock cycles 1 to 7 of the first bus cycle, while serial data
follows during clock cycles 1 to 8 of the next bus cycle. Notice
also how the bus lines are placed in a quiescent high state both
before and after the bus transaction.
Fig.7.4 shows the minimal logic required to interface with the
I2C bus. A bidirectional gate arrangement is used for input to and
output from both the local serial clock (SCLK) and serial DATA
lines of each bus-connected device. Incoming data and clock
signals are regularised by means of a high-impedance input buffer
stage, while output data and clock signals drive the bus using
an open-drain MOS device. Note that the bus requires pull-up
resistors so that the bus lines go high before the start condition
(S) and after the end condition (P) – as shown in Fig.7.3.
Addressing
Following the start sequence, transmitted data is only allowed
to change when the clock is in its low state. In its basic form,
and by virtue of the seven bits available for addressing (see
Fig.7.3), the I2C protocol caters for a total of 127 devices. In
addition to the seven bits used for addressing, the first byte of
an I2C transfer generated by a bus master includes a bit that
indicates the direction of the data transfer. Note that the address
is transferred with the most-significant bit first. Table 7.1 shows
I2C addresses for a diverse selection of I2C devices; it should
give you an idea of just how versatile and useful this simple bus
really is. Note that addresses are quoted in hexadecimal (thus
‘0x20’ is 20 in hexadecimal, 32 in denary or 100000 in binary).
a second bus line (SCL). Serial peripheral interface (SPI), on
the other hand, offers a full-duplex point-to-point connection
where the data is passed in and out on separate lines (MOSI
and MISO). SPI is therefore faster and often easier to use than
I2C, but there can often be situations in which I2C is preferred
simply because this is the interface that’s built into the chip
or device that you intend to use!
Introducing the TEA5767 radio chip
The TEA5767 is a single-chip (see Fig.7.5) electronically tuned
FM stereo radio designed specifically for use in simple lowvoltage applications controlled via the I2C bus. The device is
completely adjustment-free and only requires a minimum of
small and low-cost external components. The main features of
the radio are:
n Integrated RF amplifier for high sensitivity
n 87.5MHz to 108MHz tuning range for the US and Europe
(76MHz to 91MHz in Japan)
n RF automatic gain control (AGC)
n Fully integrated FM demodulator
n FM IF selectivity performed internally
n Crystal reference frequency oscillator (operating at 32.768kHz or
at 13MHz crystal with an externally applied 6.5MHz reference)
n Synthesised phase-locked loop (PLL) tuning
n Soft mute and signal-dependent mono-to-stereo switching.
I2C compared with SPI
I2C is a very simple bus system where bidirectional data
appears on a single line (SDA) and a clock signal is sent on
Table 7.1: I2C addresses for a selection of devices
Device
Function/application
Address range
BME280
Temp, pressure and humidity sensor
0x76 or 0x77
CAP1188
8-channel capacitive touch sensor
0x28 to 0x2D
MCP23008
I2C GPIO expander
0x20 to 0x27
MCP9808
Digital temperature sensor
0x18 to 0x1F
PCA9685
16-channel PWM driver
0x40 to 0x7F
SAA2502
MPEG audio source decoder
0x30 and 0x31
SSD1306
OLED display driver
0x37
TDA9860
Hi-Fi audio processor
0x40 to 0x41
TEA5767
FM radio receiver
0x60
TMP007
IR temperature sensor
0x40 to 0x47
Practical Electronics | February | 2022
Fig.7.5. The TEA5767 FM radio module.
43
The channel selector (change)
button (PB1) is repeatedly pushed to
cycle through the four channels and
its state (HIGH or LOW) is stored in
the buttonState variable.
The operating frequencies and
channels shown in the code are
for use in the Sussex area of the
UK, but they can be easily changed
to those being used in your local
area. All you need to do is change
the frequency and station text for
each channel. If required, other
stations can be catered for by
adding further code after the block
for Channel 4. A few of the most
popular stations are listed in Table
7.2, and most UK readers should
be within range of one or more of
these. Note that it will be necessary
to alter the code at the top of the
loop to reflect the new number of
available channels.
Fig.7.6. Circuit of the I2C-controlled FM radio.
The TEA5767 is easy to use and only requires a few lines of code
for channel selection. For example, the following single line
of code sets the TEA5767’s operating frequency to 104.8MHz
(BBC Radio Sussex):
Constructing the Arduino-based TEA5767 FM radio
The minimal wiring for the Arduino-based FM radio is shown
in Fig.7.7. The three modules (Arduino Nano, TEA5767 and
OLED display) can be conveniently mounted in a small ABS
enclosure, allowing access to the two 3.5mm jack sockets used
radio.setFrequency(104.8); // BBC Radio Sussex
Note that the appropriate library routines must first be referenced
for inclusion in your code or an error message will be generated
and the application will simply not run.
Table 7.2: Some popular UK FM radio stations
Station
Frequency (MHz)
BBC Radio Bristol
94.9 and 104.6
BBC Radio Cornwall
95.2 and 103.9
BBC Radio Cymru
92.4 and 92.7
BBC Essex
95.3 and 103.5
BBC Radio Humberside
95.9
BBC Radio Kent
96.7 and 104.2
BBC London
94.9
BBC Radio Manchester
95.1
u8g2.clearBuffer(); // First clear the display memory
u8g2.setFont(u8g2_font_helvB14_tf);
// Select
the font to be used
u8g2.drawStr(0,25,"BBC Radio Sussex"); // The text
to be displayed
u8g2.sendBuffer(); // Finally send it to the display
BBC Merseyside
95.8
BBC Radio Scotland
92.5, 92.6, 92.7, 92.8, 92.9 etc.
BBC Radio Sheffield
88.6
BBC Radio Solent
96.1 and 103.8
BBC Radio Ulster
93.1
Note once again that the necessary library routines must be
referenced for inclusion in the code and the display driver
must first be initialised.
BBC Radio Wales
90.2, 90.3, 90.4, 90.5, 90.4 etc.
BBC Radio York
95.5, 103.7, and 104.3
Belfast 89FM
89.3
Capital FM
97.4, 103.2
Classic FM
100.1, 100.2, 100.3, 100.4 etc.
Greatest Hits Radio
96.2 and 97.4
Heart (Essex, Solent, Surrey, Sussex)
97.5
Heart (North East, West Midlands)
100.7, 101.2
Heart (North Wales)
88.0
Heart (Scotland)
101.1, 103.3
Heart (South West)
100.8, 101.2
Manx Radio
88.9
OLED display
The 0.91-inch OLED (organic light-emitting diode) display uses
a matrix of 128x32-pixel LEDs. The device is controlled using an
SSD1306 driver chip, which also contains an I2C interface. The
I2C address of the device is 0x3C and it operates from a supply
voltage in the range 3.3 to 5V at a typical current of less than
8mA. The OLED display is very easy to use and only requires
a few lines of code. For example, the following code fragment
displays a simple text message:
The I2C-controlled FM radio
The circuit of our I2C-controlled FM radio is shown in Fig.7.6. It
shows the Arduino Nano controller linked to the TEA5767 FM
radio and OLED display using the I2C bus (the SDA and SCL
lines are respectively connected to pins A4 and A5 on the Nano).
The code for the circuit (I2C_FM_radio.ino) is available for
download from the February 2022 page of the PE website.
The channel change button (PB1) is connected to digital input
D3 on the Nano. This line is configured as an input using the
following two lines of code:
const int buttonPin = 3;
// Channel change button
pinMode(buttonPin, INPUT);
44
Practical Electronics | February | 2022
You will need...
Fig.7.7. Wiring schematic for the I2C-controlled FM radio.
for the antenna input and headphone/
speaker output. Access will also be
required for the USB programming/5V
external DC supply connector. The
wiring schematic is shown in Fig.7.7
and a prototype breadboard layout is
shown in Fig.7.8.
Finally, this project provides
plenty of scope for modification and
experimentation. A capacitive touch
switch could easily be added for pre-set
channel selection. Alternatively, a rotary
encoder could be used to give full tuning
coverage of the VHF band in 100kHz steps.
An Arduino Uno could be substituted for
the Nano and the raw DC input could be
used instead of the USB connector. The
two YouTube video presentations listed
in Going further overleaf should provide
you with plenty of food for thought!
To build the I2C radio you need one
each of the following:
n Arduino Nano
n I2C-bus-compatible TEA5767 FM
radio module (see Going further)
n I2C-bus-compatible OLED display
module (see Going further)
n Telescopic antenna fitted with
3.5mm jack plug (often bundled
with the TEA5767 radio module)
n Headphones or external speaker
fitted with a stereo 3.5mm jack plug
n Miniature NO (normally open)
pushbutton switch
n 4.7kΩ resistor
n USB lead for connection to a PC (for
programming)
n USB 5V DC power supply (or any
switched 5V supply)
n Small ABS enclosure (or prototype
breadboard for test purposes).
Going further
This section details a variety of sources
that will help you locate the component
parts and further information that will
allow you to understand I2C and add a
wide range of I2C-bus-compatible devices
to your projects. It also provides links to
relevant underpinning knowledge and
manufacturers’ data sheets.
Fig.7.8. The prototype I2C-controlled FM radio on-test, tuned to (FM) BBC Radio Sussex.
Practical Electronics | February | 2022
45
Table 7.3: Going Further with I2C
Topic
Meet the
I2C bus
Source
Texas Instruments have a useful introduction to the I2C:
https://bit.ly/pe-Feb22-ks1
For a comprehensive directory of I2C addresses, visit:
https://i2cdevices.org
The Arduino website provides a variety of resources to support the
Nano: https://bit.ly/pe-dec21-ard1
Arduino
Nano
The Arduino’s integrated development environment (IDE) can be
downloaded from: https://bit.ly/pe-feb22-ks3
Electronics Teach-In 8 – Introducing the Arduino (available from
Practical Electronics) provides a one-stop source of ideas and
practical information.
TEA5767
FM radio
module
The TEA5767 FM radio module is available from several on-line
suppliers, including Amazon and eBay. The NXP/Philips datasheet
for the chip itself (not the complete module) is available from:
https://bit.ly/pe-feb22-ks4
0.91-inch
OLED
display
The 0.91-inch OLED display using an SSD1306 driver is available
from on-line suppliers, including AZ-Delivery, Amazon and eBay –
search: ‘0.91" inch OLED SSD1306’.
YouTube
videos
Csongor Varga has produced a detailed video showing the TEA5767
FM radio module in action: https://youtu.be/yp0HVGjakMs
Ralph Bacon provides another excellent introduction to using the
TEA5767: https://youtu.be/yWf9uxL6zgE
Notes
An excellent PowerPoint introduction to I2C can be found at:
https://bit.ly/pe-feb22-ks2
To identify the I2C address of a connected device, a scanner
application (i2c_scanner) is one of the example files in the
I2C Wire library. Note: this sketch tests the standard 7-bit
addresses and any devices with higher-bit addresses might
not be seen properly.
An Arduino Uno can be substituted for the Nano, but it will
require a much larger enclosure.
You may need to use the IDE’s in-built Library Manager to
download the two library files listed in the code.
A more complex FM radio design based on the TEA5767 can
be found at: https://bit.ly/pe-feb22-ks5
This Instructables tutorial provides details of a TEA5767
FM radio based on an Arduino Uno (rather than a Nano):
https://bit.ly/pe-feb22-ks6
AZ-Delivery provide a useful eBook Quick Start Guide.
Download it for free at: https://bit.ly/pe-feb22-ks7
These two video presentations differ in style and
content, but both will provide you with plenty of useful
background information, as well as some useful ideas for
further development.
Teach-In 8 CD-ROM
Exploring the Arduino
EE
FR -ROM
CD
ELECTRONICS
TEACH-IN 8
FREE
CD-ROM
The Arduino offers a remarkably effective platform for developing a
huge variety of projects; from operating a set of Christmas tree lights
to remotely controlling a robotic vehicle wirelessly or via the Internet.
Teach-In 8 is based around a series of practical projects with plenty of
information for customisation. The projects can be combined together
in many different ways in order to build more complex systems that can
be used to solve a wide variety of home automation and environmental
monitoring problems. The series includes topics such as RF technology,
wireless networking and remote web access.
The CD-ROM also includes a bonus – an extra 12-part series based around the popular
PIC microcontroller, explaining how to build PIC-based systems.
SOFTWARE
FOR
THE TEACH-IN
8
SERIES
FROM THE PUBLISHERS OF
This CD-ROM version of the exciting and popular Teach-In 8 series
has been designed for electronics enthusiasts who want to get to
grips with the inexpensive, immensely popular Arduino microcontroller,
as well as coding enthusiasts who want to explore hardware and
interfacing. Teach-In 8 provides a one-stop source of ideas and
practical information.
PLUS: PICs and the PICkit 3 – A beginners guide
£8.99
INTRODUCING THE ARDUINO
• Hardware – learn about components and circuits
• Programming – powerful integrated development system
• Microcontrollers – understand control operations
• Communications – connect to PCs and other Arduinos
PLUS...
PIC n’MIX
PICs and the PICkit 3 - A beginners
guide. The why and how to build
PIC-based projects
Teach In 8 Cover.indd 1
04/04/2017 12:24
PRICE
£8.99
Includes P&P to UK if
ordered direct from us
SOFTWARE
The CD-ROM contains the software for both the Teach-In 8 and PICkit 3 series.
ORDER YOUR COPY TODAY at: www.electronpublishing.com
46
Practical Electronics | February | 2022
|