HackSpace_-_April_2020

(Frankie) #1

SCHOOL OF MAKING


Add a character display to your project


We can replace the while loop above with
the following:
while True:
for i in range(1,11):
lcd.move_right()
time.sleep(0.5)
for i in range(1,11):
lcd.move_left()
time.sleep(0.5)

This will make the text bounce across the screen using
the move_right() and move_left() methods.
These displays are very much focused around
displaying text. They receive text as character codes
and display it using their own internal font. This means
they’re very easy to work with as we don’t need to
worry about pixels, but it does limit what we can
display. To make matters a bit more flexible, they
include eight slots for custom characters that you can
program yourself.

Each character is a block of pixels five across by
seven up, and you can display whatever you want
in these.
You need to create a binary string to represent the
string of pixels, with a 0 being the pixel off and a 1
being the pixel on. This sounds a bit complex, but
there’s a tool at hsmag.cc/6zj1vD to help us. We’re
going to create a series of characters to create a mini
firework animation, and the first is just a dot in the
middle of the 5×7 array. We can draw this on the
website, and it’ll tell us the hex values for this:
0x0,0x0,0x0,0x4,0x0,0x0,0x0

In CircuitPython, we format this slightly differently and
we create this character with the following:
lcd.create_char(1, b’\x00\x00\x00\x04\x00\x00\x00\
x00’)

We created a simple star animation by growing points
out from this dot and cycling through four characters.
In order to display the characters, you need to show
the Unicode character 0000 to 0007 corresponding to
the character you’ve created. As most keyboards don’t
have an easy way of typing these, we can create them
with the ‘\u’ syntax.
The code for this star animation is as follows:
lcd.create_char(1, b’\x00\x00\x00\x04\x00\x00\x00\
x00’)
lcd.create_char(2, b’\x00\x00\x0a\x04\x0a\x00\x00\
x00’)
lcd.create_char(3, b’\x00\x11\x0a\x04\x0a\x11\x00\
x00’)
lcd.create_char(4, b’\x15\x11\x0a\x15\x0a\x11\x15\
x00’)

while True:
lcd.message = ‘\u0001’
time.sleep(0.5)
lcd.message = ‘\u0002’
time.sleep(0.5)
lcd.message = ‘\u0003’
time.sleep(0.5)
lcd.message = ‘\u0004’
time.sleep(0.5)

(NOT) LOST IN TRANSLATION
The process for using these displays is roughly similar
regardless of the language you use, but the syntax
will be a bit different. Let’s now take a look at how to
get them up and running in Arduino. There’s a library
called LiquidCrystal that should come bundled with the
Arduino installed – if you don’t have it, you should find
it in the library manager.
Let’s look at the same bouncing text example
that we created in CircuitPython (running on the
same hardware):
#include <LiquidCrystal.h>

LiquidCrystal lcd(21, 19, 17, 16, 15, 14);

void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(“hello”);

We created a simple
star animation by
growing points out
from this dot



Above
No GPIO? No
problem. You can get
USB backpacks that
let you drive these
displays from a USB
serial connection
Free download pdf