Home CodeMicropython Raspberry Pi Pico I2C LCD clock example

Raspberry Pi Pico I2C LCD clock example

by rp2040guy71

In this article we look at interfacing an I2C 16×2 LCD to a raspberry Pi Pico

Most of these LCD displays are either 16×2 or 20×4 and they are based on a

The I2C module usually consists of a PCF8574 chip, which is an IC which converts I2C serial communication to a parallel connections which the LCD usually requires.

Parts Required

 

Name Link
Pico Raspberry Pi Pico Development Board
I2C lCD

Aliexpress link

Connecting cables Aliexpress product link

Lysee 3D Printer Parts & Accessories – AHT20 Temperature and Humidity Sensor Module DHT11 Upgrade I2C XD Humidity Sensor Probe – (Color: Green)

Ebay link

 

Schematic/Connection

To connect the display to your Raspberry PI Pico requires just 4 wires, you just need to wire the Vcc and GND PINs from display to VBuS and a GND PINs of RPI Pico, then SDA and SCL PINs from the module to suitable SDA and SCL PINs from Raspberry PI Pico

Here is a layout in fritzing to show this

 

Code Example

I use thonny for all development

Now its important to verify the I2C address of your LCD

import machine
sdaPIN=machine.Pin(0)
sclPIN=machine.Pin(1)
i2c=machine.I2C(0,sda=sdaPIN, scl=sclPIN, freq=400000)
devices = i2c.scan()
if len(devices) == 0:
 print("No i2c device !")
else:
 print('i2c devices found:',len(devices))
for device in devices:
 print("Hexa address: ",hex(device))

You will see the I2C address in the REPL

You then need to copy 2 libraries to your Pico filesystem

In Thonny, go to top menu File => Save Copy => Raspberry Pi Pico and save each file to the board with the same name as downloaded and with a .PY extension when saving it to the board.

https://github.com/T-622/RPI-PICO-I2C-LCD/blob/main/lcd_api.py
https://github.com/T-622/RPI-PICO-I2C-LCD/blob/main/pico_i2c_lcd.py

Now that you have done all that, lets create a simple clock which will display the date on line 1 and the time on line 2

import utime

import machine
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

I2C_ADDR     = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16

def myclock():
    #Test function for verifying basic functionality
    i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
    lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)    
    lcd.putstr("LCD Works!")
    utime.sleep(2)
    lcd.clear()
    count = 0
    while True:
        time = utime.localtime()
        lcd.move_to(0, 0)
        lcd.putstr("{year:>04d}/{month:>02d}/{day:>02d}".format(year=time[0], month=time[1], day=time[2]))
        lcd.move_to(0, 1)
        lcd.putstr("{HH:>02d}:{MM:>02d}:{SS:>02d}".format(HH=time[3], MM=time[4], SS=time[5]))
        utime.sleep(1)

#if __name__ == "__main__":
myclock()

 

Links

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More