3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C

Introduction

 

This new 3.3V serial character LCD is a good display tool to output information from microcontroller platforms such as Raspberry Pi Pico, microbit, or 3.3V Arduino. 

It comes with both Inter IC (I2C or IIC) and Serial Peripheral Interface  (SPI) interface. With 2 lines x 16 characters display and 5×8 dots with cursor surely the best choice for many makers to display their data.  The power supply required is 3.3V+ and compatible with most of the MCU available in the market. So in this tutorial, we will demonstrate the setup and program of the Raspberry Pi Pico to display on this LCD. Let’s go!

Video

Essential Hardware

I will be using a Raspberry Pi 400 to develop the program for Pico 🙂 You can use any computer or laptop that has Thonny Python IDE to do that.

Software

Setup

 

STEP 1: Install MicroPython into Raspberry Pi Pico

If you are new and have a fresh Pi Pico, you might need this step to help you. You can refer to this tutorial How to Set Up and Program Raspberry Pi Pico to help you set up your new Raspberry Pi Pico. If you confirmed that your Pi Pico is already been installed with MicroPython, you can skip this STEP and go straight to the next step.

STEP 2: Download Library

Boot up the computer to program the display. In this tutorial, I am using Raspberry Pi 400 as my computer to access Thonny Python IDE for coding. You can use any computer or laptop as long as it has Thonny Python IDE to program the Pi Pico. To program the display, we need the library or the “driver” of this LCD. You can get the library at this GitHub LCM1602-14_LCD_Library created by Bhavithiran. The following picture will show you on how to download the file from GitHub:

Click the link given, the view will look like this.

Lcd 1

Then, click the ‘code’ green button and click download ZIP. The file will automatically download and we can use it.

Lcd 2

The Protocol

LCD Display “Hello World” with I2C Serial Communication Protocol

I2C or Inter-Integrated Circuit is the communication protocol that only uses two wires for the communication which are data (SDA) and another, the clock (SCL). The communication address between this LCD and the device is 62 (DEC) or 0x3E. First, let’s program to display “Hello World” on this LCD with I2C protocol using MicroPython. Below shows the circuit diagram to set up the LCD display with I2C protocol on Pi Pico :

 

I2c Combine

The actual view of the setup : 

STEP 1 : Program and Library Upload into Raspberry Pi Pico

Open the Thonny Python IDE on your computer. Plug the USB into the computer. 

2021 02 18 104505 1920x1080 Scrot

If the system does not detect, click the Stop/Restart backened button. Then we can verify if the devices connect or not by seeing at the terminal

2021 02 18 104515 1920x1080 Scrot 1

Right click under MicroPython devices file location and click the New directory to create the library folder for storing the files. Enter any name and the folder will created. For me, I used lib as the name for the library folder.

2021 02 18 105553 1920x1080 Scrot 1

The lib folder has been created

2021 02 18 105610 1920x1080 Scrot

Enter the lib folder under MicroPython devices section. At the ‘This computer’ files location, find the LCD_I2C.py name then right click it and click the Upload to /lib. We can see that the file was copied under the MicroPython device section.

2021 02 18 110138 1920x1080 Scrot

Exit the lib folder under MicroPython device section, then enter the Examples folder under This Computer section to upload the program in Pi Pico.

2021 02 18 110600 1920x1080 Scrot

Right click the hello_world_i2c.py file under This computer section and click the Upload to / to upload the file into Pi Pico. We can see the file was copied under the MicroPython device section.

2021 02 18 110903 1920x1080 Scrot

So now we are done upload the program into Raspberry Pi Pico. What left is the execute the program.

STEP 2: Program Execution 

Find the hello_world_i2c.py file under the MicroPython device section then click it. The view will look like this:

2021 02 18 111932 1920x1080 Scrot

Then, find the Run current script the green look play button to execute the program.

2021 02 18 112247 1920x1080 Scrot 2

The program will execute and the output will look like this :

Dsc 0128 1

The formation of the sample program will make the LCD to:

  • Display the text for about 1 second then it will blink.
  • Text will disappear and the cursor will blinking.

If you want to maintain LCD text display in Pi Pico without needed to always execute with Thonny Python IDE, you need to save the file name as main.py . Find ‘file’ at the right above of the Thonny Python IDE, click it and find the ‘save as’.

Save it under MicroPython device :

2021 02 25 170218 1920x1080 Scrot

Name it as main.py

2021 02 25 170307 1920x1080 Scrot

You are ready to go! Verify it by plug it off and connect with any power supply.

So, we are done display the text on LCD with I2C communication protocol.

LCD Display “Hello World” with SPI Communication Protocol

SPI or Serial Peripheral Interface is the four wire-based full-duplex communication protocol that consists of four wires for the communication which are MOSI (master out slave in), MISO (master in slave out), SCL(serial clock that produces by the master) and SS(slave select line which use to select specific slave during the communication). Here, we will learn on how to display “Hello World” on this LCD  with SPI protocol using MicroPython. Below show the circuit diagram to setup the LCD display with SPI protocol on Pi Pico :

Spi Combine

The actual view of the setup : 

STEP 1 : Program and Library Upload into Raspberry Pi Pico

Open the Thonny Python IDE, then plug the Pi Pico onto the computer. If the system does not detect, click the Stop/Restart Backened button. Make sure that lib folder already been created. If the lib folder does not created yet, you can go to STEP 1 under I2C protocol steps for assisting. 

2021 02 18 123858 1920x1080 Scrot 1

Enter the lib folder under MicroPython devices section. Under the ‘This computer’ files location, find the LCD_SPI.py name then right click it and click the Upload to /lib. We can see that the file was copied under the MicroPython device section.

2021 02 18 124157 1920x1080 Scrot

Exit the lib folder under MicroPython device section, then enter the Examples folder under This Computer section for us to upload the program in Pi Pico.

2021 02 18 124326 1920x1080 Scrot

Right click the hello_world_spi.py file at This computer section and click the Upload to / to upload the file into Pi Pico. We can see the file was copied under the MicroPython device section.

2021 02 18 124839 1920x1080 Scrot

So now we are done upload the program into Raspberry Pi Pico. What left is the execute the program.

STEP 2 : Program Execution 

Find the hello_world_spi.py file under MicroPython device section then click it. Find the Run current script the green look play button to execute the program.

2021 02 18 125038 1920x1080 Scrot

The program will execute and the output should look like this :

Dsc 0129

The formation of the sample program will make the LCD to:

  • Display the text for about 1 second then it will blink.
  • Text will disappear and the cursor will blinking.

If you want to maintain LCD text display in Pi Pico without needed to always execute with Thonny Python IDE, you need to save the file name as main.py. Find ‘file’ at the right above of the Thonny Python IDE, click it and find the ‘save as’.

Save it under MicroPython device :

2021 02 25 171851 1920x1080 Scrot

Name it as main.py

2021 02 25 171902 1920x1080 Scrot

You are ready to go! Verify it by plug it off and connect with any power supply.

So, we are done to display the text on LCD with I2C communication protocol.

Application

As we know, Pi Pico was coming with a temperature sensor built-in. So, how about we make some application with it and display the temperature on LCD. This could help us to revise what we learnt from above. Below I included the code for both protocols, you may modify, improve and it is good for you to code by yourself. The explanation of how the code works are been included in the code(comment view).

Temperature display for I2C protocol code :

from LCD_I2C import *    #import LCD I2C library
import math
import machine
import utime

_tempSensor = machine.ADC(machine.ADC.CORE_TEMP) #initialize temperature sensor in Pi Pico
lcd = LCD(sda=2, scl=3) #I2C communcation

conv = 3.3 / (65535) #convert raw reading from sensor to voltage

lcd.set_cursor(0,0)
lcd.write("Temperature :")
lcd.set_cursor(3,1)
lcd.write("Deg Celcius")

while True:
    reading = _tempSensor.read_u16() * conv #analog input read and apply the conversion factor
    temperature = 27 - (reading - 0.706)/0.001721 #convert temperature to degree celcius
    lcd.set_cursor(0,1)
    lcd.write(str(math.trunc(temperature))) #display the temperature and round off it
    utime.sleep(1)

Put the code in main.py to execute permanently :

2021 03 11 103649 1920x1080 Scrot

Temperature display for SPI protocol code :

from LCD_SPI import *    #import LCD_SPI library
import math
import machine
import utime

_tempSensor = machine.ADC(machine.ADC.CORE_TEMP) #initialize temperature sensor in Pi Pico
lcd = LCD(sck=2, tx=3, cs=5) #SPI communcation

conv = 3.3 / (65535) #convert raw reading from sensor to voltage

lcd.set_cursor(0,0)
lcd.write("Temperature :")
lcd.set_cursor(3,1)
lcd.write("Deg Celcius")

while True:
    reading = _tempSensor.read_u16() * conv #analog input read and apply the conversion factor
    temperature = 27 - (reading - 0.706)/0.001721 #convert temperature to degree celcius
    lcd.set_cursor(0,1)
    lcd.write(str(math.trunc(temperature))) #display the temperature and round off it
    utime.sleep(1)

Put the code in main.py to execute permanently :

2021 03 11 095539 1920x1080 Scrot 1

Output for both codes :

Dsc 0246

Conclusion

In the conclusion, this 3V3 Serial character LCD is an amazing display device because of available in two types of serial communication. This could ease people who want to use SPI communication at the same time experiment with I2C. The table below shows the simple comparison between two communication protocols for this product.

Inter-Integrated Circuit (I2C) Serial Peripheral Interface (SPI)
Less Connection Wire More Connection Wire
Slower Communication Faster Communication

hello_world_spi.py and hello_world_i2c.py are only the example code for you to try to run and see whether the display is working or not. You may improve, modify, update or create your own code. However, always include the library due to the hardware instruction set so that the display can work.

Thanks for reading this tutorial. If you have any technical inquiries, please post them at Cytron Technical Forum.

2 thoughts on “3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C”

  1. As soon as the LCD is initialised , the back light goes off and will not come back on until after the power is removed and restored.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

HC-05 & HC-05(ZG)
Automatic Plant Watering System Using REKA:BIT
DIY USB MIDI Drum Kit using Maker Pi Pico and CircuitPython
DIY Cat Stealing Money Bank using Reka:bit
FFF/FDM 3D Printer Bed Leveling Guide
Tutorials of Cytron Technologies Scroll to Top