My sonic, what?


For my first project of the new year I wanted to use two modules to create a measuring device. I decided on the ultrasonic sensor and a LCD display to show the measurement on.   This will be a precursor to using the ultrasonic sensor to trigger other events based on the measurements.

Before starting modified the LCD so that the support circuits, the VCC, ground and contrast, are on the board instead of having to be part of the breadboard. You can see them in the picture of the project.

Then I opened up the example sketches so that I cold be sure I had working parts. Once I was sure the parts all worked I began the task.


Here is a video of the working project:

An image of the completed cuircutGet the lab book PDF.


Below is the sketch of Project 2013-001:


* use a HC-SR04 ultrasonic sensor and
* print the measurement to a LCD IM161 display


The LCD circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)



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


include libraries and declare initialize commands before void setup()

// include the library code:
#include <LiquidCrystal.h> //Library for LCD display //
#include <NewPing.h> //Library for ultrasonic sensor //

#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 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500 cm.

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //

// NewPing setup of pins and maximum distance.

void setup() {

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

// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);



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); //

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 and tagged . Bookmark the permalink.