How to connect and use a Character LCD using only 2 pins

//How to connect and use a Character LCD using only 2 pins

Connect and use a Character LCD with only 2 pins using the I2C bus


Using a character LCD with an Arduino is nothing new, but let’s make it very simple by using the I2C bus.

By using this little I2C LCD board, we can control the LCD using only 2 wires, and not worry about resistors to adjust the contrast since it’s all included.

To make this tutorial more interesting, we will use the HC-SR04 ultrasonic range finder that we used before and display the results on the LCD.


We are using a 20×4 Character LCD so we have 4 lines of 20 characters each available.

The I2C LCD module is connected to pin A4-SDA and A5-SCL.

The HC-SR04 is connected to pin 11-echo and 12-trigger.

VCC and Ground from the Arduino are connected to the breadboard rails .

We then connect the VCC and Ground to both the I2C module and the HC-SR04.


Before we can use the I2C module, we need to find out it’s HEX address so we can communicate with it.

We find the I2C module address by running this “I2C Scanner” sketch on our Arduino :

*note:  Watch the tutorial video to see the code in action and the results. 

#include "Wire.h"

void setup()

Serial.println("\nI2C Scanner");

void loop()
byte error, address;
int nDevices;


nDevices = 0;
for(address = 1; address < 127; address++ )
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
error = Wire.endTransmission();

if (error == 0)
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.println(" !");

else if (error==4)
Serial.print("Unknow error at address 0x");
if (address<16)
if (nDevices == 0)
Serial.println("No I2C devices found\n");

delay(5000); // wait 5 seconds for next scan

As you can see in the tutorial video, the address for our I2C module is 0X27, so we will use that value in our Sketch to communicate with it.

We will use the “NewPing” library to communicate with the HC-SR04 ultrasonic range sensor.

We are also using the “NewLiquidCrystal” library in our Sketch, this library unlike the one included with the Arduino IDE is more recent, faster and can use the I2C bus, so might as well use it.

You can download the libraries at the bottom of this tutorial page.

*Rename the original LiquidCrystal folder in the Arduino/Libraries to something like LiquidCrystal_Old before extracting the NewLiquidCrystal library, since the folders have the same name, and the original library will conflict with this new one.

As always, please watch the tutorial video for more information.

#include "Wire.h"
#include "LCD.h"
#include "LiquidCrystal_I2C.h"
#include "NewPing.h"

#define I2C_ADDR 0x27  // Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup()
 lcd.begin (20,4); //  our LCD is a 20x4, change for your LCD if needed

// LCD Backlight ON

lcd.home (); // go home on LCD
lcd.print("Range Finder HC-SR04"); 

void loop()
 unsigned int uS =; // Send ping, get ping time in microseconds (uS).
 unsigned int cm = sonar.convert_cm(uS); // Convert into centimeters
 lcd.setCursor (0,1); // go to start of 2nd line
 lcd.print("Current Distance:");
 lcd.setCursor (0,3); // go to start of 4th line
 lcd.print("Ping: ");
 lcd.print(" cm ");


Copy and paste the above code(s) in the Arduino IDE to program your Arduino.

Used Libraries:

Download the NewLiquidCrystal library here:

Download the HC-SR04 library here:

Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder.

By | 2018-02-23T17:25:45-05:00 February 10th, 2015|Tutorials|


  1. Keith March 8, 2019 at 10:03 am - Reply

    Have it running on a 2560 Mega and it works just fine.

  2. Mustafa YILDIZ January 13, 2019 at 4:54 am - Reply

    Can you please share your I2C backpack and 20×4 lcd fritzing parts? Thanks

  3. Jean-Charles Viens December 22, 2018 at 2:45 pm - Reply

    Well, I must be dumb as a post because I have been trying to have my I2C driven LCD displays (because I ordered more than one thinking one or two might be defective) working for over a year and I was never successful. Even the scanner doesn’t pick anything up on a Sunfounder or genuine Arduino. Either I wire them directly on the SDA and SCL pins or use A4 & A5. Extremely frustrating.

Leave A Comment