This tutorial demonstrates how to use character LCD and color TFT displays on the nRF52840 with CircuitPython. It explores the DisplayIO library and using Bluetooth low energy to interact with LCD displays from a mobile phone.
This is my 3nd CircuitPython tutorial. The first one demonstrated getting started with the Adafruit Feather nRF52840 Express and BLE. The 2nd one showed how to flash the Nordic nRF52840 dongle using OpenOCD on a Raspberry Pi and create BLE applications.
This tutorial will start by connecting a character LCD display to a Nordic nRF52840 dongle. Below is a 16×2 character LCD display. It’s very legible, easy to connect and great for providing user feedback. They come in different sizes and colors. They’re inexpensive and can also readily be salvaged from old electronic equipment such as phones, printers, faxes, etc.
The display interface uses 4 bit mode which requires 6 GPIO pins. Two potentiometers are added to control contrast and brightness. The LCD ground pin is connected to a ground on the dongle. The Read/Write pin (RW) is grounded to ensure write only. The 3.3V dongle could potentially be damaged if you tried to read the 5V display. The backlight cathode is grounded and 1 terminal from each pot is grounded. The display Vcc is connected to Vbus on the dongle which is the USB 5V bus. The other terminal from each pot is also connected to 5V. This causes the pots to act as variable voltage dividers between 0 and 5V. The wiper from the first pot is connected to the LCD display contrast pin. The wiper from the other pot is connected to the backlight anode which will control brightness. The RS pin is connected GPIO 0.13. Enable is connected to 0.15. Data lines D4, D5, D6 and D7 are connected to 0.17 0.20, 0.22 and 0.24 respectively.
By the way, it doesn’t really matter which GPIO pins you use for the display control and data lines because any available pins can be specified in the code.
The Adafruit CircuitPython bundle contains the CharLCD library which can be used to control character LCD displays. Copy the adafruit_character_lcd folder from the bundle to the Nordic dongle which appears as a USB drive labelled CircuitPy. The following code displays a message on the display and after a few seconds the text is shifted off to the right.
Next we’ll make the display more interactive with Bluetooth low energy (BLE). From the CircuitPython bundle copy the adafruit_ble folder to the dongle. Only a few modifications to the existing code are necessary.
The code sets up a UART server and waits for BLE connections. Once connected the main program loop checks for incoming messages. The message text is displayed on the LCD and an acknowledgment is returned to the BLE client. The Adafruit Bluefruit LE Connect app can be used to connect from a mobile phone (both Android and iOS .)
In my previous MicroPython OLED tutorial I created a MicroPython library for the SSD1351 display and a simple brick game.
I updated my SSD1351 library so it’s CircuitPython compatible. I’ve also included a couple of CircuitPython examples. The SSD1351 library and all the demos can be downloaded on my GitHub page. One of the new examples is a simple maze demo.
Instead of a wired slider, the main character is controlled wirelessly using the control pad that’s included with the Adafruit Bluefruit LE Connect app. I designed the library to be performant but it’s all coded in MicroPython. If I were to add a lot of monsters, weapons and other props, the animation might not be so fluid. Fortunately, CircuitPython provides a native LCD library called DisplayIO which has many great features such as tilesets which can be used for sprites or bitmap fonts. There are groups which can organize different layers of graphics. Layers can have transparent backgrounds so they can be stacked on top of other layers. The DisplayIO library is currently very beta. It’s a work in progress and the code is in flux.
DisplayIO is adding support for many graphical LCD displays. One that is currently supported is the ST7735.
It’s an inexpensive 16 bit color TFT graphic LCD display. It comes in different sizes and resolutions. This one is 1.8 inch (128 by 160 pixels.) There’s a fast SPI interface which makes it easy to connect to most IOT devices. It operates at 3.3V so it’s compatible with the nRF52840.
For the next demo, I’ll switch to an Adafruit Feather nRF52840 Express board because I encountered hardware crashing using the Nordic dongle. Using the DispalyIO library, the SPI library, the Bluetooth library and the DHT library probably was too much for the dongle which has less memory than the Feather.
The ST7735 ground is connected to a ground on the Feather. Vcc is connected to 3.3V. RST (Reset) is connected to GPIO 9. A0 which toggles between data and command mode is connected to GPIO 10. SDA is connected to MO (Master Out Slave In). SCL is connected SCK (Serial clock). CS (Chip Select) is connected to GPIO 11. The backlight LED anode is connected to 3.3V. The backlight cathode is connected to ground.
A DHT22 sensor will provide temperature and humidity data.
The DHT22 ground pin is grounded. The DHT22 Vcc pin is connected to 3.3V. The data pin is connected to GPIO 5. A 10K ohm pull up resistor is placed across the data line and 3.3V. As with the character LCD display wiring, you can use different GPIO pins, although I would stick with the dedicated MOSI and serial clock pins if your board has them because it makes coding easier.
The ST7735 DisplayIO driver is currently separate from the CircuitPython bundle. Download the driver and copy it to the Feather. There are several variants of the ST7735 so it may be necessary to modify the init sequence. On my ST7735R I had to change command 0x20 (inversion off) to 0x21 (inversion on.) I also had to the change the MADCTL 0x36 (memory data access control) data from 0x18 to 0xC8 to vertically flip the display. Please note that Adafruit now provides a separate ST7735R library.
A couple more libraries from the bundle are also required. Copy the adafruit_ble and adafruit_dht libraries from the bundle to the Feather. The demo uses a background picture which needs to be in bitmap format and sized to 128×160 pixels like the following.
The following boiler plate code sets up DisplayIO to work with the ST7735.
By the way, there are CircuitPython boards that have built-in displays such as the PyPortal. They don’t require the above initialization code. The displays can be driven simply by referencing board.display.
One very cool feature of the DisplayIO library is that when your code isn’t running it will automatically pipe the serial console to the display. Therefore, even though the above program doesn’t have any drawing commands, text or bitmaps yet, it’s still enough to test if the display works. Just run the program and then click Serial and enter the REPL. The display should echo everything you type and show the serial output.
Next the code will be updated to add a background image, terminal text, a DHT22 sensor and BLE support.
The bitmap fills the background and the sensor temperature and humidity readings scroll over in a transparent terminal overlay.
The Bluefruit LE Connect app can now connect to the display. The plotter feature charts the temperature and humidity data from the DHT22 sensor.
My next tutorial demonstrates how to add Internet access to an nRF52840 board running CircuitPython by using an ESP32 as a WiFi coprocessor.