Alright folks! here we are again. Adding a screen to the project! I had to do a bit of research on i2c and the 1602 module which led me to the library needed. Link to the library i2c_lcd.py is at the bottom and is included in my GitHub repo. The library needs to be saved to the pi pico, this can be done in Thonny. Just upload the i2c_lcd.py code into Thonny and click save as > save to RP2040 as i2c_lcd.py. This will ensure our code will be able to import code needed from the Library. Let’s get started!
This is what I told ChatGPT to do:
Now add a i2c lcd 1602 screen that displays when the led is on or off. Use pin 2 for SDA and pin 3 for SCL. Make sure to use the libraries i2c_lcd.py in the program.
ChatGPTs Answer:
To add an I2C LCD 1602 screen to the existing code and display whether the LED is on or off, you need to incorporate the necessary libraries and initialize the LCD screen. Below is the modified code including the LCD initialization and displaying the LED state:
import machine
import utime
from i2c_lcd import I2cLcd # Ensure i2c_lcd.py is on the Pico
# Define the I2C address of the LCD (use the address detected by the I2C scanner)
I2C_ADDR = 0x27 # Replace with your LCD's I2C address
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
# Initialize I2C on bus 1 with SDA on Pin 2 and SCL on Pin 3
i2c = machine.I2C(1, scl=machine.Pin(3), sda=machine.Pin(2), freq=100000)
# Initialize the LCD
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# Define GP0 as an output pin for the LED
led_pin = machine.Pin(0, machine.Pin.OUT)
# Define GP1 as an input pin with a pull-up resistor for the button
button_pin = machine.Pin(1, machine.Pin.IN, machine.Pin.PULL_UP)
# Variables to track button states
button_state = button_pin.value()
prev_button_state = button_state
# Function to display LED state on the LCD
def update_lcd(led_state):
lcd.clear() # Clear the LCD before writing new text
utime.sleep_ms(100) # Give a small delay to ensure clearing completes
if led_state:
lcd.move_to(0, 0)
lcd.putstr("LED: ON")
else:
lcd.move_to(0, 0)
lcd.putstr("LED: OFF")
# Display initial state
update_lcd(led_pin.value())
# Main loop
while True:
# Read the state of the button
button_state = button_pin.value()
# Check if the button state has changed (active low)
if button_state != prev_button_state:
# Debounce delay
utime.sleep_ms(20)
# Confirm the state change
if button_state == button_pin.value():
# If the button is pressed
if button_state == 0:
# Toggle the LED
led_pin.toggle()
# Update the LCD with the new LED state
update_lcd(led_pin.value())
# Update the previous button state
prev_button_state = button_state
# Short delay to reduce CPU usage
utime.sleep_ms(50)
Explanation
I2C Initialization:
i2c = machine.I2C(1, scl=machine.Pin(3), sda=machine.Pin(2), freq=100000)
: Initializes I2C on bus 1 with SDA on Pin 2 and SCL on Pin 3, operating at a frequency of 100 kHz.
LCD Initialization:
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
: Initializes the LCD with the specified I2C address and dimensions (2 rows, 16 columns).
LED and Button Setup:
The LED is connected to GPIO0, configured as an output.
The button is connected to GPIO1, configured as an input with an internal pull-up resistor.
Display Update Function:
update_lcd(led_state)
: Clears the LCD, waits for a short delay to ensure clearing, and then writes the current state of the LED ("LED: ON" or "LED: OFF").
Initial Display State:
update_lcd(led_pin.value())
: Displays the initial state of the LED on the LCD when the program starts.
Main Loop:
The main loop continuously reads the button state, toggles the LED when the button is pressed, and updates the LCD with the new LED state.
The button state is debounced with a delay to prevent false triggers.
We have success!
Videos:
Because it was hard to see the lcd in the first video, I created a separate video for an up-close of the lcd during operation.
Fritzing Design:
That’s it, now for the links.
GitHub Links:
GitHub Pi Pico Code: https://github.com/TheDumbEngineer/TheDumbEngineer/blob/main/ProjectThree.py
GitHub i2c_lcd.py Library: https://github.com/TheDumbEngineer/TheDumbEngineer/blob/main/i2c_lcd.py
GitHub Fritzing: https://github.com/TheDumbEngineer/TheDumbEngineer/blob/main/ThirdProject.fzz
Components Link: (since I provided links in the previous posts for the components needed leading up to this, I will just add a link to the 1602 I2C LCD screen)
1602 I2C LCD Screen: https://amzn.to/3XgVQbg