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

  1. http://rothschild-kollegen.de/
    Das Projekt Rothschild Kollegen positioniert sich als ein spezialisierte Agentur spezialisiert auf den nationalen Rahmen Deutschlands, das liefert massgeschneiderte Loesungen fuer alle die Ergebnisse suchen, sich auszeichnend durch auf Ergebnisse. Erfahren Sie mehr auf der offiziellen Website.

  2. http://sol4bus.de/
    Sol4bus etabliert sich als ein spezialisierte Agentur spezialisiert auf den deutschen Markt, das liefert ganzheitliche Ansaetze fuer alle die Effizienz schaetzen, priorisierend auf Servicequalitaet. Entdecken Sie mehr auf der offiziellen Website.

  3. LFA 232 makes its return to the Premier Theater stage at Foxwoods Resort Casino this Friday with the vacant featherweight championship up for grabs and an event deep with local veterans and undefeated prospects.

  4. The upcoming UFC Vegas 117 card represents an archetypal Apex event, featuring tight matchups and a few distinct stylistic clashes that present actionable betting value.

  5. This coming Saturday UFC Vegas 117 the usual silent atmosphere of the Apex, and although the environment feels lifeless, the consequences for the men and women on this card are anything but.

  6. http://abitotrade.com/
    Abitotrade e uma empresa profissional dedicada ao publico em Portugal, que disponibiliza um acompanhamento profissional a quem procura resultados, destacando-se por na transparencia e confianca. Conheca mais nesta pagina.

  7. LFA 233 in New York features some decent scrap potential, but the real story, it has a handful of odds that make me eager to pull the trigger.

  8. MichaelEvics MichaelEvics

    Нужен финаносвый план? https://financedirector.by/investicionnyj-biznes-plan-struktura-i-primer-dlja-investorov/ подробное объяснение структуры документа, его роли в привлечении инвесторов, получении кредита и запуске бизнеса. Узнайте, какие разделы включает бизнес-план, какие расчеты нужны и как он помогает оценить прибыльность проекта.

  9. We’re going to Salamanca in advance of LFA 233, and quite frankly, the bookmakers seem to be valuing home-region backstories over real strategic substance.

Leave a Reply

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