Electrons: Data In and Data Out with an HC-SR04 and a 16x2 Display

99p buys you the cutest mini robot face ever.

Electrons: Data In and Data Out with an HC-SR04 and a 16x2 Display

Previously, I added the display board to a nano - but this just outputted pre-programmed text, and I wanted the nano to send out information that it collects itself. Part of that was so I could play with this rather beautiful-looking distance sensor. My idea is to use this as a proximity sensor for some sort of arduino controlled vehicle in the future...but for now I just wanted to test this little board.

The board was 99p from eBay, but you do seem to get quite a lot for that - have a look at the back:

I’ve not explored what each of the chips do and only one is marked, but it's loads for the money. You’ll note that there are four header pins, voltage and ground, plus the trigger and echo pins. I assumed that the trigger sends the ping command, and the echo pin sends a signal once the bounce-back is received.

I’ve built this on the same board into which my 16x2 matrix is plugged, so I’m going to use two more lines from the Nano board for the trigger and echo. I choose pins D5 & 6:

The headers make the board stand upright from the breadboard, which is useful, and there's just enough space to fit it in. Here you see the wires from the arduino, plus the power lines wired in.

So it's easy to wire it in, and it doesn’t require much thought - the hard bit is the code to send and receive the ping. I then add that into my dot matrix code from the previous sketch. The code I find seems to use a board which only has one data pin - not separate trig and echo ones like I do. Also it outputs the distance it derives from the sensor out over a serial line - and I don’t want that - I need to pipe it back into the display output. But after a while I come up with the following:

// include the library code:
#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6
const int trigPin = 6;
const int echoPin = 5;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");

void loop() {
 // establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
lcd.setCursor(0, 1);
lcd.print("in, ");
lcd.print("cm     ");
long microsecondsToInches(long microseconds)
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
long microsecondsToCentimeters(long microseconds)
  return microseconds / 29 / 2;

I have to fiddle about with it a bit more to get the output looking clean on the screen, but once it's all compiled and uploaded I'm rewarded with the following:

So I’ve now successfully got the Arduino communicating what it receives with the outside world! I use this as a debug mechanism to check I've got the sensor working properly. As I move a book closer or further from the sensor, the distances change.

In a final project, I would probably poll the sensor every so often, and if the distance from an object was closer than 20cm, turn on a motor to take evasive action... This way I can see what the sensor is doing in advance of the action the code would take.

23-Jan-2015 Add comment

blog comments powered by Disqus Permanent Link

Matthew Norman...

... who lives in the UK, who is not an actor, not a clock manufacturer, did not write a rough guide to cuba and also has never written for the Telegraph. I have written books on coldfusion and web databases.

(c) somewhere in space and time: M.Norman