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,472 Comments

  1. UpvoteRocket is an automated vote delivery service for game server toplist websites. Buy votes for 25+ supported ranking sites including XtremeTop100, GTop100, TopG, MMtop200 and more. Real mobile proxy IPs, pay per vote, instant campaign setup.

  2. Youre so cool! I dont suppose Ive learn anything like this before. So nice to seek out somebody with some original ideas on this subject. realy thanks for starting this up. this web site is something that’s needed on the net, someone with a bit originality. useful job for bringing something new to the internet!

  3. UpvoteRocket is the #1 automated voting service for private MMORPG servers. Climb toplist rankings on XtremeTop100, TopG, GTop100 with smart undetectable automation. Ragnarok, WoW, Mu Online & 20+ platforms supported. Pay only for delivered votes.

  4. UpvoteRocket is the leading automated voting service for private MMORPG servers. Our system climbs toplist rankings on XtremeTop100, TopG, GTop100, Top100Arena, and 20+ other platforms with intelligent automation that blends seamlessly with natural traffic. Every vote is delivered through authentic connections with realistic timing patterns to maximize success rates. Whether you run a Ragnarok, World of Warcraft, Mu Online, Lineage 2, or Perfect World server, we handle the voting so you can focus on your game. Pay only for successful votes, track progress in real-time, and watch your server rise to #1 while you sleep.

  5. UpvoteRocket is the leading automated voting service for private MMORPG servers. Our system climbs toplist rankings on XtremeTop100, TopG, GTop100, Top100Arena, and 20+ other platforms with intelligent automation that blends seamlessly with natural traffic. Every vote is delivered through authentic connections with realistic timing patterns to maximize success rates. Whether you run a Ragnarok, World of Warcraft, Mu Online, Lineage 2, or Perfect World server, we handle the voting so you can focus on your game. Pay only for successful votes, track progress in real-time, and watch your server rise to #1 while you sleep.

  6. UpvoteRocket is the #1 automated voting service for private MMORPG servers. Climb toplist rankings on XtremeTop100, TopG, GTop100 with smart undetectable automation. Ragnarok, WoW, Mu Online & 20+ platforms supported. Pay only for delivered votes.

  7. UpvoteRocket is the leading automated voting service for private MMORPG servers. Our system climbs toplist rankings on XtremeTop100, TopG, GTop100, Top100Arena, and 20+ other platforms with intelligent automation that blends seamlessly with natural traffic. Every vote is delivered through authentic connections with realistic timing patterns to maximize success rates. Whether you run a Ragnarok, World of Warcraft, Mu Online, Lineage 2, or Perfect World server, we handle the voting so you can focus on your game. Pay only for successful votes, track progress in real-time, and watch your server rise to #1 while you sleep.

  8. UpvoteRocket is the #1 automated voting service for private MMORPG servers. Climb toplist rankings on XtremeTop100, TopG, GTop100 with smart undetectable automation. Ragnarok, WoW, Mu Online & 20+ platforms supported. Pay only for delivered votes.

  9. Many thanks for this article. I might also like to mention that it can possibly be hard if you are in school and merely starting out to initiate a long credit rating. There are many individuals who are just simply trying to survive and have long or beneficial credit history is often a difficult issue to have.

  10. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this hike.

  11. Throughout the grand scheme of things you get a B+ just for hard work. Where exactly you lost everybody was first on your particulars. You know, they say, the devil is in the details… And it couldn’t be more correct in this article. Having said that, let me inform you what did deliver the results. Your authoring can be rather engaging and that is most likely the reason why I am taking an effort to comment. I do not make it a regular habit of doing that. Next, whilst I can certainly notice a leaps in logic you make, I am not certain of just how you appear to connect your details which produce the conclusion. For now I shall yield to your position however hope in the foreseeable future you connect your facts better.

Leave a Reply

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