Skip to main content

Arduino Tutorial - Ultrasonic Sensor HC-SR04 with Buzzer and LED (Distance Meter )

Description:

In this Arduino Tutorial we will learn how the HC-SR04 Ultrasonic Sensor works and how to use it with Buzzer and LED.

Hardware Required:

- Arduino Board
- Ultrasonic Sensor HC-SR04
- Buzzer
- LED
- Breadboard and Jump Wires

Connections:

HC-SR04 Sensor

- The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.
- The HC-SR04 sensor attach to the Breadboard
- The Sensor VCC connect to the Arduino Board +5V
- The Sensor GND connect to the Arduino Board GND
- The Sensor Trig connect to the Arduino Board ANALOG 1
- The Sensor Echo connect to the Arduino Board ANALOG 0

  LED
- The LED attach to the Arduino UNO
- The LED (from LED's long leg) connect to the Arduino Board Digital 13
- The LED short leg (-) connect to the Arduino Board GND

Code Descriptions:

- First you have to define the Trig and Echo pins. In this case they are the pins number 9 and 10 on the Arduino Board and they are named trigPin and echoPin. Then you need a Long variable, named “duration” for the travel time that you will get from the sensor and an integer variable for the distance.
- In the setup you have to define the trigPin as an output and the echoPin as an Input and also start the serial communication for showing the results on the serial monitor.

Get the Code: 

// defines pins numbers
const int trigPin = A1;
const int echoPin = A0;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(ledPin, HIGH);
}
else{
  digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Comments

  1. CASINO ROYALE: BLACKJACK RACING SITES - Casino Roll
    The Blackjack game is played 바카라 잔 by 8 쪽박 걸 players in two different w88 코리아 modes. The 안전 토토 사이트 dealer must pick from six or eight cards from a deck of 52 룰렛 프로그램 cards, two blackjack

    ReplyDelete

Post a Comment

Popular posts from this blog

Staring with my blog

This is my new video just for fun but my channel is realted to robotic and programing where you can learn new projects and ideas I have created this bolg to give you recent information to my channel (link to channel) i will start uploading my first video by new video release Bluetooth Control Robot Car (using L298N) on 10th of december for recent uploads stay updated to my channel and subcrice my channel (click here) Vande Matram!