LCD Sensor With Micropython Using Bharart Pi Board

by bharatpi in Circuits > Electronics

144 Views, 0 Favorites, 0 Comments

LCD Sensor With Micropython Using Bharart Pi Board

WhatsApp Image 2024-04-02 at 12.09.43 PM.jpeg

In this tutorial, we will learn how to interface I2C LCD with ESP32 and how to display simple text/numbers on I2C LCD. This I2C LCD is a 16×2 device which means it can display 16 columns by two rows of characters. and it also has an I2C circuit connected with it which makes it easy to connect to the ESP boards. 16X2 LCD without I2C circuit has sixteen pins.

If we want to connect this board directly with the ESP board, we have to use at least eight pins of our board which will be a waste. So the better solution is to use an I2C LCD instead of typical 16×2 LCD. In this tutorial, we are using 16×2 I2C LCD, but LCD of any size will also work the same way as we will learn in this tutorial. The advantage of using an I2C LCD is that we only need to use four pins (including the power pins).


Supplies

WhatsApp Image 2024-04-02 at 12.27.20 PM.jpeg
WhatsApp Image 2024-04-01 at 11.11.49 PM.jpeg
WhatsApp Image 2024-04-01 at 11.09.47 PM.jpeg


Before we start this make sure you have the latest version of Micropython firmware installed in your ESP32 boards and have a running Integrated Development Environment(IDE) in which we will be doing the programming. We will be using uPyCraft IDE.

if you dont have the setup means then you can check the README file of installing and setup of uPyCraft and Thonny IDE in our Bharat Pi github page you can find the link here,

> https://github.com/Bharat-Pi/MicroPython/blob/main/README.md

We will need the following components to connect our ESP board with the I2C LCD :

  1. ESP32.
  2. I2C LCD.
  3. Connecting Wires.
  4. USB Cable.
  5. Breadboard.

Interfacing I2C LCD With ESP32 :

WhatsApp Image 2024-04-01 at 11.30.36 PM.jpeg


In this section, we will see how to connect I2C LCD with ESP32. The I2C LCD will be connected with the ESP32 board with its 4 pins (GND, VCC, SDA and SCL).

Follow the schematic diagram for the ESP modules and connect them accordingly. If you are using ESP32 for this project, connect the ESP32 device with the I2C LCD as shown in the schematic diagram above: 

The VCC pin is connected with the 5V pin from the ESP32 to power up. Both the grounds of the two devices are connected in common. The SCL pin of I2C LCD is connected with the default SCL pin of the board. The SDA pin is connected with the default SDA pin of the board.

I2C LCD Pinout :

This display has four pins:

1. GROUND

2. VCC

3. SDA 

4. SCL

The connection of I2C LCD with the ESP boards is very easy. We have to connect the VCC terminal with 5V pin, ground with the ground (common ground), SCL of the sensor with SCL of the module, and SDA of the sensor with the SDA pin of the ESP modules.

The connections between the devices can be seen below:

ESP32 I2C LCD

VCC            - 5V

GPIO21(I2C SDA) - SDA

GPIO22 (I2C SCL) - SCL

GROUND       - GND

ESP32 I2C Pins:

The I2C pins stated above are set in default. If we want to change the GPIO pins we have to set them in code. The diagrams above show the pinout for the ESP32 respectively.

Getting I2C LCD Address:

Lcd_addr_console.png

When you connect your I2C display with ESP32, you need to check its address. Because every I2C device has an address associated with it. For many devices of I2C LCD, the default address is 0x27 where 0x shows hex format of the numbers. But address can be different in some cases. This address depends on the position of pads A0, A1, and A2 on the I2C controller on this devices.

Now copy this code and upload it your board with the I2C LCD already connected with it.


CODE :



#checking the address of LCD

import machine

sdaPIN=machine.Pin(21)  
sclPIN=machine.Pin(22)

i2c=machine.I2C(sda=sdaPIN, scl=sclPIN, freq=10000)  

devices = i2c.scan()

if len(devices) == 0:
 print("No i2c device !")
else:
 print('i2c devices found:',len(devices))

for device in devices:
 print("At address: ",hex(device))


This code will scan for any I2C devices connected with ESP32 and will specify the number of devices with the address in the console. If using ESP8266, replace the SDA and SCL pins appropriately.

This message shows the address of liquid crystal display is 0x27. You will most likely get the same address for LCD with 16 columns and 2 rows.


Display Text/Numbers on I2C LCD

WhatsApp Image 2024-04-02 at 12.40.06 PM.jpeg


I2C LCD MicroPython Libraries:

For this project we will require two libraries: 1. lcd_api.py and 2. i2c_lcd.py

You can find the libraries on our Bharat Pi github page ,

> https://github.com/Bharat-Pi/MicroPython/tree/main/Bharat_Pi_LCD_Display

Copy both of these libraries and save them in your MicroPython device with the respective file names. Open a new file . Copy the libraries from the links given above. Save them to ESP32 with names lcd_api.py and i2c_lcd.py.

In this section, we will display a message along with numbers on the screen.


CODE :



import machine
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from lcd_i2c import I2cLcd
from time import sleep

I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16

i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)   #initializing the I2C method for ESP32
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000)    #initializing the I2C method for ESP8266
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)

while True:
  lcd.putstr("This is BharatPi")
  sleep(3)
  lcd.clear()
  lcd.putstr("I2C LCD Tutorial")
  sleep(3)
  lcd.clear()
  lcd.putstr("Lets Count 0-10!")
  sleep(3)
  lcd.clear()
  for i in range(11):
    lcd.putstr(str(i))
    sleep(1)
    lcd.clear()
  lcd.putstr("Thank you")
  sleep(3)
  lcd.clear()

Demonstration:

WhatsApp Image 2024-04-02 at 12.13.50 PM.jpeg


This code will display the message 1st "This is BharatPi" , 2nd "I2C LCD Tutorial". Then displays "Lets Count 0-10!" for 3 seconds after clearing the screen. Then we clear the screen using the clear() method on the lcd object. After that we use a for loop to display numbers from 0 to 10 after a delay of 1 second each. After each number is displayed, wait for one second, then clear() will erase the text.

To test this program with ESP32 upload the main.py file to your board. Once the code is uploaded to the board, adjust the brightness of the display through the potentiometer until the LCD starts displaying the messages.

Troubleshooting the LCD Contrast :

WhatsApp Image 2024-04-02 at 8.08.28 PM.jpeg

After wiring the LCD, you will need to adjust the contrast of the LCD. On the I2C module, there is a potentiometer that can be rotated with a small screwdriver.

At the backside of this liquid crystal display, you can see a potentiometer knob (blue coloured). That is used to modify the brightness of the LCD. This potentiometer is very handy when you are using this display module in different light conditions.

After the connections you will see the backlight lights up. you have to turn the potentiometer knob, the 2 rows of rectangles will appear.

If you look closely, you can see tiny rectangles for each character on the screen. If you have made it this far, Your LCD is functioning properly.