Making a LCD panel work with the Pi4

Shalav Kakati
4 min readFeb 28, 2021

a LCD panel permits us to display characters and symbols in a limited number of rows and columns. It is very useful as most of the time, IOT devices will be in headless mode with no keyboard, mouse, monitor but with a wifi or wired ethernet link to a upstream device. So knowing how to use it is a good thing for any experiment.

There are various kind of LCD with differences in the number of rows and columns. All have a contrast control, some have a backlight control as well. The one I ordered online had contrast only, though we initially thought it had backlight as well (should have checked its specs more carefully).

The goal was to take a 4 row x 20 col LCD, and figure out how to display characters on it and how to manipulate it through software. the data to be output would be the earlier experiment running in parallel on the DHT22 temperature, humidity sensor

wired up the LCD device after I soldered it (badly) to a 16 pin header… the panel to our surprise did not come with the pin headers…we had to solder it …. we ordered a “soldron” brand as thats what dad said was used in his college (early 1990s) but a cheap no-name product came. we also had no flux and the rod had a “spade” tip. end result was a terrible quality of soldering and was a big iffy whether it would work but finally it did. we finally got a proper soldering rod, flux and a ceramic pointed tip for future work.

stark difference between a proper soldron rod with ceramic coated pointy tip and the no-name low grade rod on top I used (my first soldering effort at that) to put the pin header into the LCD display PCB holes
terrible quality of soldering :)

followed the instructions here for a 4-bit data output mode. There is another mode 8-bit that needs more wires….the wires were already a lot on this one.

https://www.youtube.com/watch?v=zC3i3CbKZfw

then looked around for code to run the LCD and found one on github that configured some other pins but was overall having the functionality. uses the gpio in BOARD mode, tried in BCM mode some other code but could not figure out how to make it work

pi@raspberrypi:~ $ cat dht22lcd.py#program will read temp, humidity and print it on console screen and the LCD display also import Adafruit_DHTimport timefrom datetime import datetimeimport sysfrom RPLCD import CharLCDimport RPi.GPIO as GPIOGPIO.setwarnings(False)GPIO.setmode(GPIO.BOARD)GPIO.setup(33, GPIO.OUT)GPIO.setup(31, GPIO.OUT)GPIO.setup(29, GPIO.OUT)GPIO.setup(23, GPIO.OUT)lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=20, rows=4, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])DHT_SENSOR = Adafruit_DHT.DHT22DHT_PIN = 27print(“testing read of sensor once”)humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)if humidity is not None and temperature is not None:    print(“Temp={}C Humidity={}”.format(temperature, humidity))else:    print(“Sensor failure. Check wiring.”)    exit()time.sleep(1)print(“starting monitoring phase”)while True:    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)    time.sleep(1)    if humidity is not None and temperature is not None:       now = datetime.now()      current_time = now.strftime(“%d:%m:%y %H:%M:%S”)      print(“%s temp %3.3f humidity %3.3f” % (current_time,temperature, humidity))      #we have to address the LCD as a matrix with (0,0) in the top left cover and reposition the curser onto the row,column we need to      lcd.cursor_pos = (2, 0)      buf = u”Temp: %3.3f C\n” % temperature #lcd lib need unicode      lcd.write_string(buf)      lcd.cursor_pos = (3, 0)      buf = u”Humidity: %3.3f %%\n” % humidity      lcd.write_string(buf)      lcd.cursor_pos = (0,0)      lcd.write_string(“Shalav test”)      lcd.cursor_pos = (1,0)      lcd.write_string(“weather report”)    else:      lcd.cursor_pos = (0, 0)      print(“Sensor failure. Check wiring.”)      exit()GPIO.cleanup()

finally realized the LCD did not have a backlight so the backlight 10k potentiometer was of no use, only the contrast potentiometer was needed

removed the contrast potentiometer and experimented with a 5k ohm and 1k+1K=2k ohm resistor to see the impact of increasing the resistance on the contrast…

10k ohm potentiometers for contrast and backlight control. the pins skip one hole on the breadboard

I let it run for 24 hours and it was solid and running cool

blew moist air onto the sensor to observe the humidity change …

Video showing the output of hanging humidity by blowing moist air through a paper tube

so now that I have working code for LCD panel can use it for displaying the output of any experiment, apart from printing on the terminal.

I have also setup the Pi for remote access over wifi for next experiment which involves a camera

--

--

Shalav Kakati

Grade 11 IBDP student, interested in the intersections of mechanical, electrical and biomedical engineering.