ESP8266 IOT Project | Interface LCD PCF8574(I2C) with ESP8266 nodeMCU

 

This tutorial will guide you to interface LCD_I2C with esp8266 nodeMCU. Here we will use LCD I2C to communicate with esp8266 NodeMCU because it only requires 2 wires.

I2C Protocol

Inter-Integrated Circuit or I2C is a multi-controller/multi-target serial communication bus. Uses to attach lower-speed peripheral ICs to processors and microcontrollers in small-distance or intra-board communication.

It has two lines: Serial Data line (SDA) and Serial Clock line (SCL). The typical voltage it requires is +5 V or +3.3 V.

Serial Data Line (SDA): It is a half-duplex line, which sends data from the master to the slave or vice-versa.

Serial Clock Line (SCL): Serial clock is primarily controlled by the master.

enslaver and slave communication over SDA and SCL lines

The message format for SDA and SCL lines

START and STOP condition:

All transactions in SDA and SCL begin with START (S) and are terminated with STOP(P). A High to LOW in the SDA line while HIGH in SCL defines the START condition. A LOW to HIGH in the SDA line, while SCL is HIGH, describes a STOP condition.

Start and Stop condition

Repeated START condition:

Between each START and STOP condition, the bus is considered as busy. If the master wants to transfer data without releasing the bus, it issues START without STOP, called REPEATED START.

Target Address:

After the START condition, the target address is sent in data format. It is 7-bit long.

Read/Write Bit:

Eight-bit is the data direction(R/W) bit. If it is 0, then it indicates a transmission(WRITE). if it is 1, then it indicates as request data(READ).

ACK/NACK Bit:

After every successful data received target sends the ACK bit to the controller.

LCD 16X2 and PCF8574

Liquid crystal display (LCD) comes in different sizes. LCD 16x2 has 16 columns and 2 rows. It can show a max of 32 characters. In LCD 16x2 each column starts from 0 to 15, and the row starts from 0 to 1. To write a character at a specific location in LCD, we require a specific column and row location. For example

LCD.println(0, 1)

Pins Name Description
1 VSS It is GND pin
2 VCC It is voltage supply pin, connects to power supply (+5V)
3 V0/VEE It is control pin, which adjust contrast of the LCD
4 RS It is register pin. 0 -> command mode and 1-> Data mode
5 R/W It is control pin. 0-> write data in LCD and 1-> read data from LCD
6 E It is enabled pin. 1 -> It allow to R/W in LCD otherwise not
7-14 D0-D7 These are data pin
15 LED+ This pin is connected to +5V
16 LED- This pin is connected to GND

view raw lcd.md hosted with ❤ by GitHub


To direct connect LCD with MCU, we need to secure all these pins to MCU. But we can simplify these connections using PCF8574. The PCF8574 module provides general-purpose remote I/O expansion for most microcontroller families using the I2C interface.

Pins Description
GND It connects to GND
VCC It is connected to +5V
SDA It is serial Data pin. It is half-duplex, which transfer in both direction
SCL It is serial Clock pin. It gets MCU clock

view raw PCF8574.md hosted with ❤ by GitHub

                            

Circuit Diagram

                                                I2C LCD connected to NodeMCU

As shown in the schematic diagram, LCD I2C is connected with esp8266 with pins described in the below table.

LCD I2C esp8266 NodeMCU
GND GND
VCC Vin
SDA D2
SCC D1

Component Required

  • Esp8266
  • LCD and PCF8574 I2C
  • Breadboard
  • Connecting Wires

Setup for LCD I2C in Arduino IDE

If the LCD I2C module is not set up, then we need to download the library-related LCD I2C module. In Arduino, go to the following location:

Sketch -> Include Library ->Manage Library ->search liquid crystal i2c.

if esp8266 node-12e is not available to download on board then do this:

Copy the following link:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

And add it in the following place:

File->Preferences ->Addition Boards Manager URLs-> add link there

Coding

To write text in LCD, we need to address it. can be calculated with the following code snippet below:

#include <Wire.h>
#include <ESP8266WiFi.h>
void setup() {
Serial.begin (115200);
while (!Serial)
{
}
Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
Wire.begin();
for (byte i = 8; i < 120; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1);
}
}
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
}
void loop() {}


#include<Wire.h>
#include<LiquidCrystal_I2C.h>
//setUp lcd address for 16 characters and 2 display
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
//initialize lcd
lcd.init();
//Turn on blacklight and a print message
lcd.backlight();
lcd.println(" Hello World ");
}
void loop() {
// put your main code here, to run repeatedly:
}
view raw showtextlcd.js hosted with ❤ by GitHub

Post a Comment

Previous Post Next Post