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

2,606 Comments

  1. Решил заняться продвижением своего сайта и начал искать специалистов. Оказалось, что сейчас много компаний занимающихся продвижением сайтов, но выбрать не так просто. По совету друга обратился в seo агентство — они сразу предложили несколько вариантов, включая продвижение по трафику и оптимизацию контента. Пока изучаю их предложения, но уже вижу, что подход серьёзный. Кстати, когда искал информацию по теме seo продвижение сайтов в москве https://optimizaciya-i-seo-prodvizhenie-sajtov-moskva.ru, наткнулся на несколько полезных статей, которые помогли лучше разобраться в вопросе. Если кто-то уже работал с такими агентствами, поделитесь опытом — как выбрать надёжного партнёра и на что обратить внимание в первую очередь?

Leave a Reply

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