Skip to content

Ultrasonic distance sensor with Arduino and HC-SR04 (Arduino + HC-SR04)

Good night,

Going on with my adventures with Arduino and sensors, today I decided to play with Ultrasonic sensor HC-SR04 to measure distance.

HC-SR04 is pretty easy to work with, it has 4 pins, VCC to energy, Trig to start a measurement, Echo to get the distance, and GND to ground.

It has a library to get it working, it is called Ultrasonic. The only problem that I had was the Library is out of date, it still uses WProgram.h but Arduino 1.0.1 uses Arduino.h, so I updated it. (You can get on the end of the post).

The library must be included on your dir “arduino-1.0.1/libraries” and you must restart your IDE.

Sensor VCC connects to Arduino +5V pin
Sensor Trig connects to Arduino PIN 12
Sensor Echo connects to Arduino PIN 13
Sensor GND connnects to Arduino GND PIN

You can configure it on trig and echo variables.

[code lang=”c”]
#include “Ultrasonic.h”
int trig = 12;
int echo = 13;
Ultrasonic ultrasonic(trig,echo);

void setup() {
Serial.begin(9600);
}

void loop()
{
Serial.print(“Distance CM:”);
Serial.println(ultrasonic.Ranging(CM));
Serial.print(“Distance INC:”);
Serial.println(ultrasonic.Ranging(INC));
delay(100);
}
[/code]

Referências:
HC-SR04 DataSheet
Biblioteca Ultrasonic atualizada para Arduino 1.0.1

Published inAndroid

1,555 Comments

  1. Well crafted post, the structure flows naturally from one point to the next without forcing transitions, and a stop at motionwithclarity kept the same flow going, you can tell when a writer has thought about how their content reads rather than just what it contains and this is one of those examples.

  2. Felt a small spark of recognition when the post named something I had been struggling to articulate, and a look at expertvertex produced more such moments, the rare service of giving readers language for fuzzy intuitions is one of the higher values that good writing can provide and this site offered several today instances.

  3. Worth recognising the specific care that went into how this post ended, and a look at broadcastnova maintained the same careful conclusions, endings are where most blog content falls apart and this site has clearly invested in the closing stretches of its pieces rather than letting them simply trail off when energy fades.

  4. Beats most of the alternatives on the topic by a noticeable margin, and a look at ideasrequiremovement did not change that at all, this is one of the better corners of the open internet for this kind of content and I am glad I clicked through rather than skipping past quickly like I usually do.

  5. Reading this as part of my evening winding down routine fit perfectly, and a stop at momentumdesignlab extended the wind down nicely, content that calms rather than agitates is what I want at the end of the day and this site provides that calming reading experience reliably which is increasingly rare across the modern web.

Leave a Reply

Your email address will not be published. Required fields are marked *