Raspberry Pi Pico -- 16x2 LCD, 4Bit Mode Interface, BCD Counter
by PugazhM in Circuits > Raspberry Pi
3896 Views, 1 Favorites, 0 Comments
Raspberry Pi Pico -- 16x2 LCD, 4Bit Mode Interface, BCD Counter
This experimentation is about interfacing 16X2 LCD with Raspberry Pi Pico, and using Python for programming the LCD
Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.
https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...
1602 LCD 4Bit mode video link
Abstract
The Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040 features a dual-core Arm Cortex-M0+ processor with 264KB internal RAM and support for up to 16MB of off-chip Flash. It provides wide range of flexible I/O options includes I2C, SPI, and uniquely Programmable I/O (GPIO) pins.
In Embedded system design, character LCD display [16x2, 16x4, 20x2 or 20x4 LCDs] is used for display the system status, menu navigation and configuring system parameters. This experimentation is about interfacing 16X2 LCD with Raspberry Pi Pico, and using Python for programming the LCD.
Reference
“Raspberry Pi Pico, Getting Started on Board Blink LED” Instruct-able / You-tube by PugazhM
The “lcd_4bit_mode.py” driver library
Components
Raspberry Pi Pico
Micro USB Cable
16x2 LCD Keypad Shield
Schematic
The 16x2 is very common type LCD, with two rows.
Each row displays 16 characters of either 5x7 or 5x8 dot matrix characters. Totally 32 alpha numerical charters can be displayed in two rows.
It has inbuilt font ROM, for decoding the ASCII input.
There's a dot pitch between two characters and a space between lines, thus separating characters and lines.
The 16x2 LCD supports 8-bit interface mode or 4-bit interface mode.
This experiment uses 4-bit interface mode.
The generally available Arduino shield of “16x2 LCD Keypad shield”, can be directly connected into Arduino Uno board. It consists of inbuild LCD brightness control circuit and 5 Keys. The keys are interconnected with potential divider network and when pressed, it provides divided potential on an analog pin.
The “16x2 LCD Keypad shield” can be connected to Raspberry Pi Pico GPIO pins (GPIO0--GPIO6). LCD operates at 4-bit data mode as given in above circuit.
If the display is not visible, adjust the Contrast pot (1K), to make it visible.
16x2 LCD Pin Connection
The above table describes the 16x2 LCD -- Shield – RPi Pico pin connections
16x2 LCD – 4bit Mode Library
Following LCD functionalities are provided by the “lcd_4bit_mode.py” python library.
Reset
WriteCommand
WriteData
BackLightOn
BackLighOff
CursorOn
CursorOff
CursorBlink
ClearScreenCursorHome
WriteLine
Download “lcd_4bit_mode.py” driver library.
Open the RPi Pico as drive, and then copy the library into root directory.
'''
Licence: GPLv3 Copyright 2021 Muthuswamy Pugazhendi Name:- M.Pugazhendi Date:- 12thJul2021 Version:- V0.1 e-mail:- muthuswamy.pugazhendi@gmail.com ''' import utime
class LCD16x2: def __init__(self, RS, ENABLE, BACK_LIGHT, D4, D5, D6, D7): self.RS = RS self.ENABLE = ENABLE self.BACK_LIGHT = BACK_LIGHT self.D4 = D4 self.D5 = D5 self.D6 = D6 self.D7 = D7 self.Reset() def Reset(self): self.RS.value(0) self.WriteCommand(0x03) self.WriteCommand(0x03) self.WriteCommand(0x03) #Initialize LCD into 4 bit mode self.WriteCommand(0x02) #Enable 5x7 character mode self.WriteCommand(0x28) #Cursor off self.WriteCommand(0x0C) #Increment cursor self.WriteCommand(0x06) #Clear screen self.WriteCommand(0x01) #Sleep for two mSeconds utime.sleep_ms(2) # Generate EnablePulse def EnablePulse(self): self.ENABLE.value(1) utime.sleep_us(40) self.ENABLE.value(0) utime.sleep_us(40)
# Write a byte to LCD # Separate into 2 nibbles and then write to LCD def WriteByte(self, data): self.D4.value((data & 0b00010000) >>4) self.D5.value((data & 0b00100000) >>5) self.D6.value((data & 0b01000000) >>6) self.D7.value((data & 0b10000000) >>7) self.EnablePulse() self.D4.value((data & 0b00000001) >>0) self.D5.value((data & 0b00000010) >>1) self.D6.value((data & 0b00000100) >>2) self.D7.value((data & 0b00001000) >>3) self.EnablePulse() # Write a command to LCD def WriteCommand(self, data): # Disable Register Select self.RS.value(0) # Write Command Byte to LCD self.WriteByte(data) # Write a data to LCD def WriteData(self, data): # Enable Register Select self.RS.value(1) # Write Command Byte to LCD self.WriteByte(data) # Disable Register Select self.RS.value(0) # Writes a string into Line 1 or Line2 def WriteLine(self, string, line_number): if(line_number == 1): self.WriteCommand(0x80) for x in string: self.WriteData(ord(x)) if(line_number == 2): self.WriteCommand(0xC0) for x in string: self.WriteData(ord(x)) # Clear Screen def ClearScreenCursorHome(self): self.WriteCommand(0x01) self.WriteCommand(0x02) # Clear screen and put the cursor into Home needs longer time # Introduce two mSeconds delay utime.sleep_ms(2) # Back light On def BackLightOn(self): self.BACK_LIGHT.value(1) # Back light Off def BackLightOff(self): self.BACK_LIGHT.value(0) # Cursor On def CursorOn(self): self.WriteCommand(0x0E)
# Cursor Blinking def CursorBlink(self): self.WriteCommand(0x0D) # Cursor Off def CursorOff(self): self.WriteCommand(0x0C)
Downloads
16x2 LCD Up Counter
LCD is initialized and welcome screen of “Raspberry Pi Pico 16x2 LCD” is displayed in two rows, for about 5 seconds.
The count variable is initialized with 250, and added by one count, at every 2 Seconds interval.
Updated count variable, is displayed on the LCD “UP COUNTER” at line one counts at line two.
'''
Demonstrates the use of 1602 LCD Keypad shield with RPi Pico. * 16x2 LCD Connection diagram * LCD 4 bit mode interface * Displaying welcome screen * Clear Display, Cursor on, Cursor Off, Backlight On, Back light Off * Write Command, Write data functionalities * The Raspberry Pi Pico pin connections for 16x2 LCD Keypad shield are given below: # LCD Power Pins * 16x2 LCD VCC pin to VBUS * 16x2 LCD GND pin to GND # LCD Data Pins * 16x2 LCD D4 pin to GPIO0 * 16x2 LCD D5 pin to GPIO1 * 16x2 LCD D6 pin to GPIO2 * 16x2 LCD D7 pin to GPIO3 # LCD Control Pins * 16x2 LCD RS pin to GPIO4 * 16x2 LCD ENABLE pin to GPIO5 * 16x2 LCD BACK LIGHT pin to GPIO6 # Caution do not connect A0 pin of shield to RPi Pico. # Potential divided key connections, provides +5VDC output. Name:- M.Pugazhendi Date:- 13thJul2021 Version:- V0.1 e-mail:- muthuswamy.pugazhendi@gmail.com '''
import machine
# Import lcd_4bit_mode import lcd_4bit_mode
# Import time import time
# Initialize LCD pins
RS = machine.Pin(4,machine.Pin.OUT) ENABLE = machine.Pin(5,machine.Pin.OUT) BACK_LIGHT = machine.Pin(6,machine.Pin.OUT) D4 = machine.Pin(0,machine.Pin.OUT) D5 = machine.Pin(1,machine.Pin.OUT) D6 = machine.Pin(2,machine.Pin.OUT) D7 = machine.Pin(3,machine.Pin.OUT)
display = lcd_4bit_mode.LCD16x2(RS,ENABLE,BACK_LIGHT,D4,D5,D6,D7)
display.BackLightOn()
# Line one string display.WriteLine('Raspberry Pi',1)
# Line two string display.WriteLine('Pico 16x2 LCD',2) time.sleep(5)
count = 250 while True: # Clear Screen display.ClearScreenCursorHome() #Count up count = count + 1 #Write into LCD display.WriteLine(' UP COUNTER',1) display.WriteLine(' ' + str(count),2) #Wair for two seconds time.sleep(2)
Downloads
Conclusion
The project is successfully completed with Raspberry Pi Pico and 1602 LCD Keypad shield
The 16x2 LCD can be used for many embedded projects as alpha numerical display.
Video Links
Result_Video.mp4
Visit "VESZLE - The Art Of Electronics" you tube channel for further information and videos.
https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...
If you enjoyed this instruct-able, then make sure you subscribe
1602 LCD 4Bit mode video link