This is only a preview of the March 2017 issue of Silicon Chip. You can view 48 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "All-new Swimming Pool Lap Counter":
Items relevant to "The Stationmaster: walk-around PWM train control":
Articles in this series:
Items relevant to "New SC200 Audio Amplifier - Part 3":
Items relevant to "El Cheapo Modules, Part 5: LCD module with I²C":
Purchase a printed copy of this issue for $10.00. |
U s in g
Che a p
A s i a n ic
on
r
t
c
e
l
E
es
l
u
d
o
M
Par t 5
The “New Blue” 16x2 LCD
module with piggy-back
2
I C serial interface
by JIM ROWE
This module combines a 16x2 backlit alphanumeric LCD module with a
small “piggy-back” module that provides it with an I2C serial interface.
This allows it to be hooked up to any of the common micros via only two
wires, letting multiple displays (or other I2C devices) share the same 2-wire
bus, while also freeing up some of the micro’s I/O pins for other purposes.
L
CD modules with two lines of 16
characters have been around for
many years and we’ve used them in
numerous projects. They are also now
much cheaper due to being popular for
use with Arduino, Micromite and the
Raspberry Pi.
However, many of these Arduino
and other micros are a little limited
when it comes to I/O pins, which
means that the six or seven pins required to interface to a standard LCD
module can leave you with too few pins
to interface with other components.
POWER
LED1
CON1
4
3
2
1
2x
4.7k
3x
10k
13
SDA
15
SCL
14
1
2
I 2 C ADDRESSING
1
1
0
0
1
1
0
0
1
0
1
0
1
0
1
0
BACKLIGHT
ON/OFF
INT
P0
P1
SCL
P2
3
P3
P4
A0
P5
A1
P6
P7
A2
4
5
6
7
9
10
11
12
15
2
4
SDA
IC1
PCF8574T
1
1
1
1
0
0
0
0
100nF
λ
1k
16
Vdd
GND
HEX ADDRESS
27
26
25
24
23
22
21
20
A
K
VCC
A2 A1 A0
This problem can be solved by using
an LCD with a serial interface or alternatively, attaching a small piggy-back
module to a parallel LCD to provide
serial/parallel translation.
By using a piggy-back module that
communicates using the 2-wire I2C
6
RS
EN
Vdd
ABL
16 x 2
LCD MODULE
CONTRAST
D7 D6 D5 D4 D3 D2 D1 D0
14 13 12 11 10 9
8
7
GND R/W
1
LCD
CONTRAST
3
VR1
10k
KBL
5
16
C
E
Q1
S8050
4.7k
B
Vss
(LINKS
OUT = 1
IN = 0)
8
S8050
J3Y
B
A2 A1 A0
NOTE: LCD MODULE HAS 100Ω RESISTOR IN SERIES WITH BACKLIGHT ANODE (PIN 15)
C
E
Fig.1: complete circuit for the piggy-back and LCD module together. Some of these modules use a slightly different chip
for IC1, that being the PCF8574AT, the main difference being the hex address range is instead between 38h and 3Fh.
82 Silicon Chip
siliconchip.com.au
The circuit of Fig.1 shows the LCD
module at upper right, with the rest of
the circuitry being that of the piggyback, which connects to the module
via the usual 16-pin SIL connector
along the top.
All of the serial-to-parallel conversion is performed by IC1, a Philips/
NXP PCF8574T device. This is designated as a “remote 8-bit I/O expander
for the I2C bus”. In other words, it accepts serial data over the I2C two-wire
bus, via pins 14 and 15, and it makes
the data available in parallel format at
pins 4-7 and 9-12.
In this case, output pin 4 is used to
control the LCD’s RS (register select)
control pin, while pin 6 controls the
EN (enable) pin and pins 9-12 feed
the character codes to pins D4-D7 of
the LCD. That leaves pin 5 of IC1 to
control the LCD’s R/W pin, and pin
7 to control the LCD backlight via
transistor Q1.
What about pins 1, 2 and 3 of IC1?
They’re used to set the address of IC1
on the I2C bus. All three pins have
10kW pullup resistors connecting them
to logic high (VCC) but the LCD module PCB also provides three pairs of
tiny pads so that any of the pins can
be tied to ground.
This allows the chip’s I2C address
to be set to any hexadecimal value
between 0x20 (32) and 0x27 (39),
just by bridging the pairs of pads,
as shown in the small table at lower
left in Fig.1.
So the default I2C address of the
piggy-back module (and thus LCD) is
0x27 with all links out but this can be
changed to 0x20 simply by fitting all
three links, or to any address in between by fitting one or two links. This
allows a number of the LCD-piggyback
combinations to be connected to the
same I2C bus, with each one given a
different I2C address so that the micro
driving the bus can send data to any
one it chooses.
Other I2C devices can reside on the
same bus (eg, temperature sensors,
memories, other microcontrollers), as
long as you ensure that no two devices
have the same address.
siliconchip.com.au
ADC5/SCL
ADC4/SDA
IO2/PWM
ADC3
IO3/PWM
ADC2
GND
IO4/PWM
ADC1
VCC
IO5/PWM
ADC0
SDA
IO6/PWM
IO7
IO8
ARDUINO UNO,
FREETRONICS
ELEVEN OR
DUINOTECH
CLASSIC
IO9/PWM/MOSI
LCD WITH I2C SERIAL BACKPACK
16 x 2 LCD
SCL
VIN
GND
GND
IO10/PWM/MISO
+5V
IO11/PWM/SCLK
+3.3V
IO12
RESET
IO13
+5V
GND
AREF
SDA
SCL
DC VOLTS
INPUT
What’s inside
IO0/RXD
IO1/TXD
USB TYPE B
MICRO
protocol, you end up with an LCD that
can be driven using just two wires:
one for the serial data (SDA) and the
other for the serial clock (SCK). That’s
apart from the ground and power wires
(typically +5V).
There are some serial I2C LCD modules that use a slightly different chip
for IC1, the PCF8574AT. This is virtually identical to the PCF8574T shown
in Fig.1, except that the I2C address
range is between 0x38 and 0x3F.
By using a combination of the two
chips, up to 16 different serial I2C
LCDs to be connected to the same I2C
bus, provided you use eight with the
PCF8574T bridge chip and eight with
the PCF8574AT chip.
Fig.1 also shows that the piggy-back
has a power-on indicator (LED1), a
2-pin SIL connector and jumper shunt
which can be used to disable the LCD’s
backlight if not required. Trimpot VR1
which can be used to adjust LCD contrast in the usual way (via pin 3).
Note that the SDA and SCL lines
connecting between pins 1 and 2 of
CON1 and pins 14 and 15 of IC1 are
each fitted with a 4.7kW pull-up resistor, as the I2C bus uses active-low log-
Fig.2: pin connections for the
LCD and piggy-back module to
an Arduino or compatible device.
Instead of passing the SDA/SCL
to the ADC4/ADC5 pins on the
Arduino, it can be connected to
the SDA and SCL on the other
side of the Arduino. For most
Arduino boards, these pins are
normally connected in parallel.
ic. These resistors can be left in place
if the module is the only slave device
connected to the I2C bus.
But if you’re going to be hooking up
other I2C slave devices to the same bus,
all but one should have the SDA and
SCL pull-up resistors removed.
Using it
This type of module really needs
to be hooked up to a micro, and that
turns out to be fairly easy to do with
any of the popular micros.
All you have to do is connect the VCC
and ground pins to a suitable voltage
source (which may be the same one
that’s powering the micro) and the
SDA and SCL pins to the I2C bus pins
on the microcontroller. Fig.2 shows
how this is done with an Arduino
Uno or a compatible like the Freetronics Eleven or Duinotech Classic. It
couldn’t be much simpler.
By the way, although the SDA
Reading Hexadecimal Numbers
In this article values prefixed with “0x” correspond to a hexadecimal number;
you might also see values suffixed or prefixed with “h”. Reading from left-toright each character corresponds to a 4-bit long value. With 0-9 being equal
to themselves and A-F (case-insensitive) are equal to 10-15 respectively. A
hexadecimal value is calculated as if each character is appended to the other
to form one long string of bits.
Ergo, 0x5A (or 5Ah) is equivalent to 01011010 in binary and 90 in decimal
form. A string of bits can be read as the sum of each individual non-zero bit,
with each bit being equal to 2n-1 while n is the index of that bit starting from
the right. So 0101 is equal to 23-1 + 21-1 = 4 + 1 = 5. A longer example BCDEh would just equal to 48350 in decimal and 1011110011011110 in binary.
March 2017 83
The underside of the LCD module’s PCB has the piggy-back module (black)
located above it. The jumper shunt located on the piggy-back module can be
used to disable the LCD backlighting if it’s not needed.
and SCL pins of the LCD module
are shown in Fig.2 connected to the
ADC4/SDA and ADC5/SCL pins at upper right on the Arduino, they could
instead be connected to the pins
marked SDA and SCL on the other
side of the Arduino down near the
USB connector. On most Arduino
boards, these pin pairs are connected
in parallel.
It’s just as easy to connect the serial I2C LCD module to a Micromite, as
you can see from Fig.3.
Of course, connecting the module
up to a micro is only half the story.
Then you have to work out how to get
the micro to send it the data you want
displayed.
The complicating factor here is that
quite a few people have written “libraries” to make it easier to drive this
kind of serial I2C LCD module from an
Arduino sketch, by providing a set of
simple function calls like:
lcd.print(“Text”);
And so on, which is all very well,
but even though most of these library
files have the name “LiquidCrystal_
I2C.h”, they are often different in terms
of their finer details and compatibility with any particular serial I2C LCD
module.
Rather than you going through the
same sort of hassles we did to find a
suitable library, we’ll simply point
you at some that we found to work.
These are available at the following
links:
https://github.com/fdebrabander/
Arduino-LiquidCrystal-I2C-library
https://github.com/marcoschwartz/
LiquidCrystal_I2C
It’s possible that these are actually
the same library, because in one place
we found the author listed as Frank de
Brabander but the maintainer as Marco
Schwartz. We found both through the
following website: www.arduinolibraries.info/libraries/
Anyway, these libraries do seem
to work with the module shown, as
+5V
+3.3V
26
GND
25
VCC
24
SDA
22
SCL
21
MICROMITE
17
16
14
10
9
5
4
3
RESET
84 Silicon Chip
How about a Micromite?
Programming a Micromite to
talk to the I2C LCD module is not
quite as easy as with an Arduino, as
currently the inbuilt MMBasic LCD
commands only support the parallel interface. You will find a program
called I2CLCD.bas in the MMBasic
Library, which can be downloaded
in zipped-up form from the bottom of
this page: http://geoffg.net/maximite.
html#Downloads
However, this program was written for a piggy-back module with a
different configuration than the one
which most piggy-backs seem to use
(and we have shown in Fig.1). Then
there’s a further issue in that the I2C
command syntax has changed as
MMBasic has evolved. As a result,
we ended up having to re-write the
software completely.
Changing over the program’s commands to suit the different connections
between the PCF8574T bridge chip
Where To Buy
GND
18
you’ll find out by downloading the
“Hello World” sketch (HelloWorld.
ino) from the Silicon Chip website
(www.siliconchip.com.au) and running it. We’ve included a copy of the
library (as a ZIP file) within the package. The resulting display is shown in
the adjacent photo.
Incidentally don’t forget to change
the I2C address shown in the sketch
(0x27) to 0x3F (= 3Fh), if your piggy-back module is fitted with a PCF8574AT instead of a PCF8574T.
You’ll also have to change this
address if you have changed the
address using the three small pairs
of pads.
LCD WITH I2C SERIAL BACKPACK
16 x 2 LCD
(SDA)
(SCL)
Fig.3: pin connections for the
LCD and piggy-back module to a
Micromite.
We’ve stocked some of these modules in our Online Shop so that
you can acquire and experiment
with them.
Alternatively, you can find similar
units (either pre-assembled or as
two separate items) on eBay and
AliExpress, and also 20x4 character
I2C LCDs which cost very little more
than the 16x2 types.
The piggy-back should also
work with 20x2 and 16x4 size alphanumeric LCDs, however, these
are far less popular than the other
two sizes.
siliconchip.com.au
Silicon Chip
Binders
REAL
VALUE
AT
$16.95
*
PLUS P
&
P
The top of the LCD module. The screen is mounted on a PCB measuring 80 x
36mm, while the visible area of the LCD measures 64 x 14.5mm.
and the LCD module itself wasn’t too
hard. The major difficulty was in getting the program to initialise the LCD’s
controller correctly.
The correct set-up commands have
to be sent to it soon after power is
applied, and these commands have
to be sent in a particular order, with
pauses between them to allow the
controller to process them before the
next command arrives for correct operation.
After downloading as much information as we could find regarding
the correct initialisation sequence
and timing for the Hitachi HD44780U
and Samsung KS0066U LCD controller chips (which seem to be the two
most commonly used in current alphanumeric LCD modules), we were
finally able to get the program working correctly and reliably. We found
this website most helpful: http://web.
alfredstate.edu/weimandn/lcd/lcd/
lcd_initialization/
Basically, our program (called JRI-
2CLCD.bas) just displays a “Hello,
world!” message over and over on the
LCD; just like the one for the Arduino. You can download this from our
website, open it in MMEdit and then
upload it to your Micromite and you
should get the same display as shown
in the photos.
As with the Arduino sketch, you
may need to change the I2C address
given for your display’s piggy-back, if
it has some of the address links fitted
or is using the PCF8574AT chip instead of the PCF8574T. Look for this
line near the start:
DIM AS INTEGER I2CAddr = &H27
‘ (A2=A1=A0=1)
All you need to do is change ‘&H27’
into the correct address for your module. This program provides a good
starting point for writing your own
MMBasic programs using an I2C LCD.
It’s fairly well commented, so you
should be able to see how to adapt the
program to display other things. SC
Serial USB-UART bridge module – another version
Since writing the third article in this series (for the January 2017 issue),
we’ve become aware of another popular version of the serial USB-UART
bridge module based on the CP2102 device. This one is very similar
to the one we discussed in the January 2017 article, but differs in two
respects. One is that instead of a micro-USB socket on the USB end
of the module, it is fitted with a full size type A USB plug – providing a
more rugged connection and compatibility with a standard USB type A to
type A extension cable.
The other difference (wait for it!) is that the connections to the six pins
of the SIL connector on the other end of the module are NOT the same
as those on the smaller module. So make sure that you allow for the
differing SIL pin connections when you connect the module to your micro
or other device.
siliconchip.com.au
Are your copies of SILICON
CHIP getting damaged
or dog-eared just lying
around in a cupboard or
on a shelf? Can you quickly find a particular issue
that you need to refer to?
Keep your copies
safe, secure and
always available with
these handy binders
These binders will protect your
copies of SILICON CHIP. They
feature heavy-board covers,
hold 12 issues & will look great
on your bookshelf.
H 80mm internal width
H SILICON CHIP logo printed
in gold-coloured lettering on
spine & cover
Silicon Chip Publications
PO Box 139
Collaroy Beach 2097
Order online from www.
siliconchip.com.au/Shop/4
or call (02) 9939 3295 and
quote your credit card number. *See website for overseas prices.
March 2017 85
|