Ultrasonic Sensor Arduino Project: HC-SR04

Ultrasonic Sensor Arduino Project: HC-SR04

Arduino is the best open-Source Platform for learning Electronics and Programming.

We could easily set up with any sensor or device. there is a huge community of Arduino.

In this post, You will get the code or project of HC-SR04 Ultrasonic Sensor. Here I have done a project that the LED will be turned ON if the object is in 50 cm otherwise LED will be Turned Off.

You could easily modify this code and make it your own according to your project logic.

The Code is represented here.

//Ultrasonic Sensor Arduino Project

#define trigPin 7
#define echoPin 8
#define led 13

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);  
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 50) { 
 Serial.println("Light is ON");
else {
  Serial.println(" cm");

Copyright © 2020-2021 Engineer Jagat All rights reserved. Developed by Ravindra Rana