Raspberry Pi Pico - LCD I2C

This guide shows you how to connect a 16x2 LCD with an I2C interface to a Raspberry Pi Pico. We will cover:

Raspberry Pi Pico and 16x2 LCD I2C

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×LCD I2C
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LCD I2C 16x2

The 16x2 LCD I2C is a screen with 16 columns and 2 rows. It has an I2C interface and comes with a potentiometer to adjust the screen's contrast.

Pinout

The LCD I2C uses an I2C interface and includes 4 pins.

  • GND pin: connect to GND (0 volts).
  • VCC pin: connect to VCC for power (5 volts).
  • SDA pin: transfers I2C data signal.
  • SCL pin: transfers I2C clock signal.
LCD I2C Pinout

LCD Coordinate

The LCD I2C 16x2 includes 16 columns and 2 rows. The numbering of both columns and rows begins from 0.

Raspberry Pi Pico LCD I2C Coordinate

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico LCD I2C

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the LCD I2C display to the Raspberry Pi Pico according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-LCD-I2C”, then find the LCD I2C library created by DIYables.
  • Click on DIYables-MicroPython-LCD-I2C, then click Install button to install LCD I2C library.
Raspberry Pi Pico LCD I2C library
  • Copy the below code and paste it to the Thonny IDE's editor.
""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-lcd-i2c """ from machine import I2C, Pin from DIYables_MicroPython_LCD_I2C import LCD_I2C import utime # The I2C address of your LCD (Update if different) I2C_ADDR = 0x27 # Use the address found using the I2C scanner # Define the number of rows and columns on your LCD LCD_ROWS = 2 LCD_COLS = 16 # Initialize I2C i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) # Initialize LCD lcd = LCD_I2C(i2c, I2C_ADDR, LCD_ROWS, LCD_COLS) # Setup function lcd.backlight_on() lcd.clear() # Main loop function while True: lcd.clear() lcd.set_cursor(3, 0) # Move the cursor to column 3, row 0 (first row) lcd.print("DIYables") lcd.set_cursor(0, 1) # Move the cursor to column 0, row 1 (second row) lcd.print("www.diyables.io") utime.sleep(2) lcd.clear() lcd.set_cursor(0, 0) # Move to the beginning of the first row lcd.print("Int: ") lcd.print(str(1234)) # Print integer lcd.set_cursor(0, 1) # Move to the beginning of the second row lcd.print("Float: ") lcd.print(str(56.78)) # Print float utime.sleep(2)
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the result on the LCD display.

Video Tutorial

Do More with LCD

Custom Character

To display special characters or symbols such as a heart or an angry bird on an LCD, you need to use the character generator. This is because the lcd.print() function can only show ASCII characters.

The LCD 16x2 can display 32 characters, with 16 positions per line over 2 lines. Each character is made of 40 small squares, set up in 8 rows and 5 columns.

Raspberry Pi Pico LCD 16x2 Pixel

The character generator makes a character (40 pixels). You just need to follow these steps:

Click on each pixel to select/deselect


Copy below custom character code
Replace the heart in the below code
""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-lcd-i2c """ from machine import I2C, Pin from DIYables_MicroPython_LCD_I2C import LCD_I2C import utime # The I2C address of your LCD (Update if different) I2C_ADDR = 0x27 # Define the number of rows and columns on your LCD LCD_ROWS = 2 LCD_COLS = 16 # Initialize I2C i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) # Initialize LCD lcd = LCD_I2C(i2c, I2C_ADDR, LCD_ROWS, LCD_COLS) # Define custom character 0 (heart shape) heart = [ 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 ] # Create the custom character at location 0 lcd.custom_char(0, heart) # Display the custom character lcd.print("Custom Char: ") lcd.print_custom_char(0) # Display the custom character stored at location 0

Result shown on the screen:

LCD custom character

Multiple custom characters

We can create up to 8 special characters (from 0 to 7). Here is how to make and show three of these characters.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-lcd-i2c """ from machine import I2C, Pin from DIYables_MicroPython_LCD_I2C import LCD_I2C import utime # The I2C address of your LCD (Update if different) I2C_ADDR = 0x27 # Define the number of rows and columns on your LCD LCD_ROWS = 2 LCD_COLS = 16 # Initialize I2C i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) # Initialize LCD lcd = LCD_I2C(i2c, I2C_ADDR, LCD_ROWS, LCD_COLS) # Define custom character 0 (heart shape) heart = [ 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 ] # Define custom character 1 (arrow pointing upwards) arrow_up = [ 0b00100, 0b01110, 0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100 ] # Define custom character 2 (arrow pointing downwards) arrow_down = [ 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b11111, 0b01110, 0b00100 ] # Create the custom character at location 0 lcd.custom_char(0, heart) lcd.custom_char(1, arrow_up) lcd.custom_char(2, arrow_down) # Display the custom character lcd.set_cursor(0, 0) # Move the cursor to column 0, row 0 (first row) lcd.print_custom_char(0) # Display the custom character stored at location 0 lcd.set_cursor(5, 0) # Move the cursor to column 5, row 0 (first row) lcd.print_custom_char(1) # Display the custom character stored at location 0 lcd.set_cursor(10, 0) # Move the cursor to column 10, row 0 (first row) lcd.print_custom_char(2) # Display the custom character stored at location 0

Result shown on LCD screen:

LCD multiple custom characters

Summary: how to use custom character on LCD

  • Use the tool above to create binary code for your special character.
  • Write down the binary code of your character (as copied from the step above).
heart = [ 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 ]
  • Create a unique character and position it at a number from 0 to 7
lcd.custom_char(0, heart)
  • Show a unique symbol on the screen any time you choose.
lcd.print_custom_char(0) # Display the custom character stored at location 0

Troubleshooting on LCD I2C

If the text is not showing on the LCD I2C, please check these problems:

  • Adjust the LCD screen brightness by rotating the potentiometer located on the back.
  • The I2C address for the LCD can differ between manufacturers. It typically is either 0x27 or 0x3F. Try these addresses one at a time. If they do not work, use this code to find the right I2C address.
""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-lcd-i2c """ from machine import I2C, Pin import utime i2c = I2C(0, sda=Pin(0), scl=Pin(1)) print('Scanning I2C bus...') devices = i2c.scan() if len(devices) == 0: print("No I2C devices found") else: print('I2C devices found:',len(devices)) for device in devices: print("Decimal address: ",device," | Hex address: ",hex(device)) utime.sleep(2)

The result shown in the Shell at the bottom of Thonny:

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Scanning I2C bus... I2C devices found: 1 Decimal address: 39 | Hex address: 0x27
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!