Danny van den Brande
Published © CC BY-SA

Arduino - HC SR04 Ultrasonic Distance Sensor

Hello world! Today I made a pretty handy thing. A Distance meter with a ultrasonic distance sensor. It works perfect!

BeginnerProtip1 hour2,962
Arduino - HC SR04 Ultrasonic Distance Sensor

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
×1
Jumper wires (generic)
Jumper wires (generic)
×1
HC-06 Bluetooth Module
×1

Story

Read more

Schematics

Schematic

Code

LCD_1602A_ULTRASONIC_DISTANCE_METER.ino

Arduino
This code is made for the HC-SR04 Ultrasonic sensor.
/*
Author: Danny van den Brande, Arduinosensors.nl. 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);
  Serial.begin(9600);
  lcd.begin(16,2); 
  lcd.setCursor(0,0);  
  lcd.print("Afstand in cm:");
}

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

  lcd.setCursor(0,1);  
  lcd.print("                "); 
//  Serial.print("                ");
  lcd.setCursor(0,1);   
  lcd.print(distance); 
  lcd.print(" cm");  
  Serial.print(distance);
  Serial.println(" cm");
  delay(250); 
}

Credits

Danny van den Brande

Danny van den Brande

36 projects • 77 followers
"Hello world."

Comments