The other day I connected up my LCD display and BAM, it was dead. I had to order another and when it arrived I found it was not the same. There was this odd board attached to the back of it. Turns out this was a I2C interface. A serial line connection that only needs power and two data lines. This would be awesome if it was easy to get rolling but..

Turns out that it just took some looking at the comments on Amazon, where I bought the display and a web trip to the manufacturers page to get the right address for the serial interface out of the demo app, it was wrong on the product page.


The Sketch folder I use.

A PDF of the sketch, project_2014_001

Pictures of the board.



2014.001 photo 2 2014.001 photo 1 2014.001 photo 3 2014.001 photo 4














And finally the sketch, giving descriptions and links as I go.


* Use a HC-SR04 ultrasonic sensor and
* Print the measurement to a SainSmart IIC/I2C/TWI
* 1602 Serial LCD Module Display For Arduino

The ultrasonic circuit:
* VCC (+5)
* TRIG pin 9
* ECHO pin 8
* GND (ground)



I2C Connection
*GND (ground)
*VCC (+5)

include libraries and declare initialize commands before void setup()

// include the library code:
#include <Wire.h> //http://arduino.cc/en/reference/wire#.UyhwYflkTh4
//#include <LCD.h> // listed in working example of I2C but not needed here //http://playground.arduino.cc/Code/LCD#.UyhwfvlkTh4
#include <LiquidCrystal_I2C.h>

/*#include <LiquidCrystal.h> //Library for LCD display //https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
*see this for how to use https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
*new LiquidCrystal is not needed but may be faster
*Liquidcrystal library at http://arduino.cc/en/Reference/LiquidCrystal#.UyhwBvlkTh5

#include <NewPing.h> //Library for ultrasonic sensor //https://code.google.com/p/arduino-new-ping/

//Define variables for NewPing.h
#define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 8 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500 cm.

//some settings for I2C that works with this LCD from http://www.amazon.com/review/R3JN683O0YUPAD/ref=cm_cr_pr_viewpnt#R3JN683O0YUPAD

//define I2C variables for LiquidCrystal_I2C.h
#define I2C_ADDR 0x27 //Address of LCD (Sainsmat shows 0x3f but I found via http://arduino.arigato.cz/I2C_LCD_BackPack_for_1602_and_2004/LCD_16x2_BackPack_v003.ino it was 0x27
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

//Initialize LCD
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //http://playground.arduino.cc/Code/NewPing

void setup() {

Serial.begin(115200); //setup serial communications for monitoring

// set up the LCD’s number of columns and rows:

lcd.begin (20,4,LCD_5x8DOTS);



void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):

delay(100); // Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.

//Send ping, get median (from 5) ping time in microseconds (uS).
unsigned int uS = sonar.ping_median(5); //http://playground.arduino.cc/Code/NewPing#Methods

lcd.clear();// clear LCD for next measurement
lcd.setCursor(0, 0); //cursor to top left
lcd.print(“CM”); // Centimeter label
lcd.setCursor(8, 0); //cursor to top center
lcd.print(“IN”); //Inch label

lcd.setCursor(0, 1); //cursor to bottom left
lcd.print(uS / US_ROUNDTRIP_CM); //Print centimeters
lcd.setCursor(8, 1); //cursor to bottom center
lcd.print((uS / US_ROUNDTRIP_CM)*0.393701); //print Inches

Serial.print(“Ping: “);
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)





About jcoffey

This entry was posted in Arduino, commentary and tagged , , . Bookmark the permalink.