Arduino Ultrasonic Range Sensor hc-sr04

Arduino Ultrasonic Range Sensor hc-sr04.

Hello world! Today i made a pretty handy thing. A Distance meter with a ultrasonic distance sensor. it works perfect and can be used for a lot of things, like robot positioning etc. Don’t mind the phone and Bluetooth module you don’t need it, i’m just displaying the value on my phone with my android serial monitor app.

Arduino Ultrasonic Range Sensor hc-sr04, hc-sr04

Just wire it up like the schematic and it should work after uploading the code.
If you are using a LCD with a I2C then make sure you use the right address, i already set the lines
for using a I2C, you just need to uncomment them, and remove or comment this line below:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Here are the most common addresses. Look at the back of your lcd which screen you have.
//PCF8574 = 0x20, PCF8574A = 0x38, PCF8574AT = 0x3F.
You do not need the bluetooth module! i just used it to display values on my android app.

Parts list:
Arduino uno, I used the RobotDyn uno. A Uno with 7 analog pins.
HC-SR04 Ultrasonic Distance/range sensor.
LCD 1602A, i used blue screen. (The code also has a setup for a I2C, you need to uncomment that part.)
Potentiometer 10k
Set of jumper wires MM/FF/MF
(OPTIONAL)HC-06 Bluetooth module. To display values on my app.


Arduino Ultrasonic Range Sensor hc-sr04.
Author: Danny van den Brande, BlueCore Tech.
This code is made for the HC-SR04 Ultrasonic sensor.
#include <LiquidCrystal.h> 
//#include <LiquidCrystal_I2C.h>//uncomment when using a I2C

//LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);// Uncomment when using a I2C
//make sure you set the right adress. here are the most common ones.
//PCF8574 = 0x20, PCF8574A = 0x38, PCF8574AT = 0x3F.

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 

int trig = 8; 
int echo = 9; 

void setup() 
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  lcd.print("Afstand in cm:");

void loop() {
  long duration, distance;
  digitalWrite(trig, LOW);
  digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = (duration/2) / 29.1;

  lcd.print("                "); 
//  Serial.print("                ");
  lcd.print(" cm");  
  Serial.println(" cm");
Danny van den Brande

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

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