When I bought the electronics starter kit for the Raspberry Pi, it came with a small 16 character by 2 line LCD. I found instructions on the Adafruit website for wiring it up and controlling it. I thought it might also be a useful thing to use with the NodeMCU board so I had a look for any instructions for that.
Unfortunately everything I found was for the I2C version of the display and I've got the plain parallel bus version, so I decided to have a go at wiring it up and programming it myself.
Wiring up the board
The wiring is quite straightforward, although a lot of wires are required. Six GPIO pins are needed at the NodeMCU end, along with power and ground.
Starting with the power pins, connect one of the ground pins from the NodeMCU to the ground rail of the breadboard. The 'Vin' pin supplies 5 volts (if used with a usb connection) so connect a wire from that to the '+' rail of the breadboard.
Four data lines are required: D4-D7 on the LCD. To make things easier I connected these to pins 4-7 on the NodeMCU board. The final two connections from the NodeMCU are 'Clock' (called 'E' on the display board) which went to pin 2, and 'RS' which went to pin 1. Data line D0-D3 from the display board are left unconnected.
The rest of the pins on the board go to either 0V, 5V or a potentiometer set as a voltage divider (ignore the 3 LEDs in the pictures, they aren't needed here).
Connect the following pins to ground (0V):
- 'RW' (set permanently to Write mode since we aren't reading from the board)
- 'VSS' (the board's ground connection)
- 'K' (the ground connection for the backlight)
- One side of the two potentiometers.
Connect the 'VDD' pin to 5V and also the other side of the two 10K potentiometers. Finally, the 'V0' and 'A' pins can be connected to the centre of the two potentiometers to provied adjustable contrast and backlight.
Programming the board
I based my Lua code on the Python code downloaded from the Adafruit website. Since I was converting the code from one language to another, I read through it carefully to make sure I only converted the bare minimum to get it working.
My first version of the code was little more than a few subroutines to send bytes to the board. While I was reading the NodeMCU Lua FAQ, I noticed that they recommend that code is written to be event driven instead of the more traditional program flow used in scripting. They also state that routines called from an event can only take a maximum of 10 milliseconds to run before they affect background services. After reading that I changed my code to buffer the text and use a timer to check the buffer and send the bytes to the display.
The current version is probably too cautious and sends a byte at a time. It might be more efficient to send a line at a time but that should be easy to change.
The code can be found over on Github. I have decided to put it there since it is easier for me to keep it up to date and easier for anyone else to download.
|Story location: Home / computing / raspberry_pi /|
I first used a Raspberry Pi when I was working in Leicester and I bought one for home not long after that. I set it up as a file/media server and it has been sitting in the front room quietly doing its job since then.
Last year I bought a Pi 3 and an electronics starter kit (which consisted of a breadboard, LEDs, switches and various other components) so I could learn how to connect things to the Pi.
A few weeks ago I was given a NodeMCU development board (my friend Tim has already written about some of his experiments with his).
I followed the instructions on flashing the firmware and getting a script running. For some reason the ESPlorer software doesn't seem to behave well on my computer. It usually takes several rounds of restarting/resetting/reconnecting before it will agree to communicate with it. The command line tools have no problems, neither does CoolTerm. After a day when it felt like I was spending half of my time struggling to get ESPlorer to connect properly, I decided to ditch it and use the nodemcu-uploader command line instead.
The first thing I wrote was the Hello World of the microcontroller world: the flashing LEDs. The next stage was to use the serial ports to connect the Raspberry Pi and the NodeMCU together.
Setting up the Raspberry Pi Serial Port
By default, the serial port on the Pi is configured to be used for a serial console , mainly for diagnostics. To use it for other things, you need to disable the console. There are lots of different instructions to do this but for the Pi 3 it boils down to editing the /boot/config.txt file and adding the line
followed by editing /boot/cmdline.txt and removing the
entry. After rebooting the Pi, the GPIO pins 14 & 15 should be available for the serial port.
Writing the script for the NodeMCU
I took my flashing LED script and modified it to listen to the serial port. Sending 'r','g' or 'b' will light up the respective LEDs, any other character will turn them off. If the following script is saved as serial.lua, it can be uploaded to the NodeMCU using
python nodemcu-uploader.py -port /dev/serial0 upload serial.lua
as long as the NodeMCU Uploader has been installed first.
-- Listen on the serial port for a colour. -- Toggle the colour of the LED. green=2 blue=1 red=3 gpio.mode(red,gpio.OUTPUT) gpio.mode(green,gpio.OUTPUT) gpio.mode(blue,gpio.OUTPUT) -- Start with the LEDs off gpio.write(red,gpio.LOW) gpio.write(green,gpio.LOW) gpio.write(blue,gpio.LOW) uart.setup(0,115200,8, uart.PARITY_NONE, uart.STOPBITS_1, 1) uart.on("data",1, function(char) if char=="r" then print("red") gpio.write(red,gpio.HIGH) gpio.write(green,gpio.LOW) gpio.write(blue,gpio.LOW) elseif char=="g" then print("green") gpio.write(red,gpio.LOW) gpio.write(green,gpio.HIGH) gpio.write(blue,gpio.LOW) lighton=2 elseif char=="b" then print("blue") gpio.write(red,gpio.LOW) gpio.write(green,gpio.LOW) gpio.write(blue,gpio.HIGH) else print("Off") gpio.write(red,gpio.LOW) gpio.write(green,gpio.LOW) gpio.write(blue,gpio.LOW) end end,0)
The script can be run using
python nodemcu-uploader.py -port /dev/serial0 file do serial.lua
Getting the Pi to talk to the NodeMCU
I connected the RX GPIO pin on the Pi to the TX pin on the NodeMCU, and vice versa. The easiest way of powering the NodeMCU was to use the power out pins from the Pi. I found that connecting the 5V out to Vin on the NodeMCU worked reliably. If I had the Pi plugged into a good quality USB power adaptor then I could sometimes get it to work by connecting one of the 3.3V pins of the Pi to one of the 3.3V pins of the NodeMCU board, but this isn't guaranteed to work.
The script uses the serial port library, which can be installed using
sudo apt-get install python-serial
The Python script is fairly simple and just cycles through the LEDs a few times. The third line runs the lua script, in case it wasn't already running. There isn't a pause between cycling between the LEDs but the serial timeout is set to 1 second and the port.read(100) command attempts to read 100 bytes. Since there should only be the colour names being returned, this automatically adds a 1 second delay. This might not be very good programming practice but it was the easiest way of incorporating reading a variable length response and adding a pause all at once.
import serial port = serial.Serial("/dev/serial0", baudrate=115200, parity=serial.PARITY_NONE, timeout=1.0) port.write("dofile('serial.lua')\r\n") for i in range(1,10): for c in ['r','g','b']: port.write(c) r = port.read(100) print(r)
This first demonstration is pretty trivial, although it took a few days of trial and error to get everything working. In due course I'll experiment with connecting different sensors to the Pi and NodeMCU.
I think my next step is to use Java on the Pi to do the same thing. I have had a quick look at Java serial port stuff and it isn't as straightforward as Python but since I do most of my programming in Java these days, it will be useful to know how to do that.