Intel Edison: Distance Bug - HC-SR04




Introduction: Intel Edison: Distance Bug - HC-SR04

About: 31 years old tinker and diy hobbyist. From Oulu, Finland. IG @mkarvonen_instructables

Say hello to the distance bug!

The basic idea in this project is that it measures distance with ultrasound and displays it in centimeters to the LCD display. Bonus feature is that if the button is pushed the LCD will turn green and it will save the current distance to the SD card. On the screen it will say "Saved to SD card".

There is also a feature if the measurement is too close or too far it will display "Out of range".

The measurements are also recorded by serial monitor or if you want use it with 9 V battery and it will become portable.

Project is fairly easy to do.

Step 1: Basic Components.

This project is done with Intel Edison board.

This can be done with any Arduino board available.

I'm using a connector shield over the board.

You will also need two basic cables to the shield and one modified cable.

Then you will need a push button, RGB-LCD screen, A micro SD card and a distance sensor (HC-SR04)

Step 2: Modding the Cable.

This is very simple to do.

Just solder 4 wires to a connector that you can connect to the HC-SR04.

Just make sure that the wires are in right order. That means basicly that the Vcc and the GND must be in the right place. The Trigger and Echo can be coded on the board so that is not very critical how they are in the connector.

Step 3: Connecting and Coding.

Connect the pieces to the board like in the picture and connect the board to the PC.

Open your coding program and start coding.

First Define the needed components and global variables.

#define echoPin 7
#define trigPin 8 #define LEDPin 13 #include #include "rgb_lcd.h" #include const int chipSelect = 4;

rgb_lcd lcd;

const int colorR = 255; const int colorG = 255; const int colorB = 255;

int maximumRange = 400; int minimumRange = 0; long duration, distance; const int pinButton = 3;

Then start with void setup.

Open serial with Serial.begin(115200); and set the LCD and the colors. then set the Trigger and echo pin for the distance censor. Lastly select the SD card for saving data.

void setup() {
Serial.begin (115200);

pinMode(pinButton, INPUT); lcd.begin(16, 2); lcd.setRGB(colorR, colorG, colorB); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDPin, OUTPUT); Serial.print("Start write to card.");

if (!SD.begin(chipSelect)) { Serial.println("No card or failure"); return; } Serial.println("Card found"); }

Then start working with Void loop.

this is the main program that will loop to infinity.

The trigger pin sends the ultrasound and the sound is returned to the echo pin. This can be calculated by using the speed of sound. Distance = Duration/58.2.

digitalWrite(trigPin, LOW);

digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); //Calculate distance based on speed of sound distance = duration/58.2;


If distance is out of range the program will write "out of range" to the LCD and serial monitor

Else it will calculate the distance every 150 ms and print it.

if (distance >= maximumRange || distance <= minimumRange){

Serial.println("Out of range"); lcd.setCursor(0,0); lcd.print("Out of range"); digitalWrite(LEDPin, HIGH); }

else {

Serial.println(distance); lcd.print(distance); lcd.setCursor(3,0); lcd.print("Cm"); digitalWrite(LEDPin, LOW);


If the button is pushed the program will open the SD card and seek the Distance.txt file and save the current distance to the log. This can be viewed later on a PC.

The command will also turn the display Green for 2 seconds and show the saved distance.

When 2 seconds are done the program will continue its main task. Measuring distance.

{ File dataFile ="Distance.txt", FILE_WRITE); const int colorR = 0; const int colorG = 255; const int colorB = 0;

if (dataFile) { dataFile.println(distance); dataFile.close(); lcd.setCursor(0,1); lcd.print("Saved to SD card."); Serial.println("saved to SD card."); lcd.setRGB(colorR, colorG, colorB); delay(2000); const int colorR = 255; const int colorG = 255; const int colorB = 255; lcd.setRGB(colorR, colorG, colorB); } }

delay(150); }

Step 4: Measure the Real Distance.

This is just to see that the measurement is actually real.

The measurement is pretty accurate for about 4 meters. After that there is a lot of "Out of range" texts.

Step 5: Wrap the Wires Up and Admire Your Work.

That's it. Have fun playing with your new friend.

Thank's for reading! Remember to follow me if you would like to see more of these simple builds.

The Distance.txt file is the actual recorded file.

Remember to look my other project's also ;)

If you want updates in real time follow me on Twitter @MiskaKarvonen

See you soon!

Be the First to Share


    • Photography Challenge

      Photography Challenge
    • New Year, New Skill Student Design Challenge

      New Year, New Skill Student Design Challenge
    • Anything Goes Contest 2021

      Anything Goes Contest 2021



    6 years ago on Introduction

    Hi mkarvonen.. It is very cool project.. i tried it with galileo.. it is working fine but only problem is when i restart the board then previously uploaded sketch is not working.. Please tell me to start the sketch automatically..


    7 years ago on Introduction

    Hey, this looks cool. What kinds of applications would you use this distance sensor for?


    Reply 7 years ago on Introduction


    I could say that this is just for fun. But seriously this can measure the distance of wall to wall or floor to ceiling or from table to wall and stuff like that. If you like you could measure square meter's from a room with it. Or measure how far the TV and speaker setup is to ensure the best possible audio and visual experience. The possibilities are infinite :D

    Thanks for the comment!