Thursday, 12 July 2018

Using an I2C backpack with 16 x 2 LCD screen and a micro:bit

Introduction

I couldn't resist buying some I2C LCD backpacks (based on the PCF8574 remote 8-bit i/o expander chip) from China recently as they were less than £1 each. 

They work fine at 3.3v as well as 5v, so I thought they'd be great for the 16 x 2 LCD screens that I have converted to run at 3v

I then discovered that someone in the Chinese micro:bit community had written an I2C LCD package for MakeCode for micro:bit. That makes everything very easy!

Connecting things up

I usually fit a 16 x 1 female socket to my LCD displays, as this makes it very easy to connect to various backpacks, or indeed to a breadboard using jumper wires. The backpacks I bought came with male header pins already soldered in place so it was just a matter of pushing the backpack into place on the LCD display's female socket strip. Remember that you need to use a 3v LCD screen with the micro:bit.

To connect the backpack to a micro:bit, you need access to the I2C pins by using an edge connector  breakout board for the micro:bit such as the bread:bit edge connector board from Proto-Pic. Connect the VCC pin on the LCD backpack to 3v on the micro:bit, GND to GND, SCL to pin 19 on the micro:bit and SDA to pin 20. 

Coding with MakeCode for micro:bit

Before you start, you need to know the I2C address for the LCD backpack. By default it is set to 0x3F (hex) or 63 in decimal. You can change its I2C address by shorting out one or more of the three pairs of pads A0, A1 and A2 on the back of the backpack, just below the contrast potentiometer. Refer to Table 5 of the datasheet for details. (n.b. there is a different variant of this backpack whose default address is 0x27. If you have this one, refer to Table 4 instead).

Armed with that knowledge, now open MakeCode on your browser. You will need to "Add Package" from the middle palette, then cut and paste the following URL into the box that appears: https://github.com/microbit-makecode-packages/i2cLCD1602 . Now you should see a new (green) entry in the middle palette (I2C_LCD1602) which, if you click on it, reveals the blocks you need to control the LCD. The code below just uses a loop to display an incrementing number on the screen.
















That's the easiest (and cheapest) way I have found yet to add an LCD screen to a micro:bit.

No comments:

Post a comment