Simple Project With the Ultrasonic Sensor (HC-SR04) +LED -Arduino Tutoriel-

64,330

15

9

Introduction: Simple Project With the Ultrasonic Sensor (HC-SR04) +LED -Arduino Tutoriel-

Ultrasonic ranging module HC - SR04 provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules includes ultrasonic transmitters, receiver and control circuit.

This is a simple example of using the ultrasonic sensor (HC-SR04) in Arduino where we will turn on a led with the variation of distance and print the distance from an object to the serial monitor.

Step 1: All the Necessary Components

1. Arduino Uno

2. Ultrasonic Sensor (HC-SR04).

3. Mini-BreadBoard

4. 1 kohm Resistor.

5. Jumpers.

6. Blue LED.

Step 2: Connect the Components

The connection of the components is very easy it is only necessary to follow the following pictures.

Step 3: Write Your Code

#define trigPin 13

#define echoPin 12

#define led 11

void setup()

{ Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led, OUTPUT);

}

void loop()

{ long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance < 10)

{ digitalWrite(led,HIGH);

}

else {

digitalWrite(led,LOW);

}

Serial.print(distance);

Serial.println(" cm");

delay(500);

}

Step 4: Upload and Run .

You can change the value of distance by replacing 10 in the condition if (distance <=10) by the desired value.

Arduino Contest 2017

Participated in the
Arduino Contest 2017

LED Contest 2017

Participated in the
LED Contest 2017

2 People Made This Project!

Recommendations

  • Puzzles Speed Challenge

    Puzzles Speed Challenge
  • "Can't Touch This" Family Contest

    "Can't Touch This" Family Contest
  • CNC Contest 2020

    CNC Contest 2020

9 Discussions

0
eliud mwwangi
eliud mwwangi

3 months ago

I had a problem with running the code sir
It tells me that 'serial' was not declared in this scope

0
MattG250
MattG250

Question 8 months ago on Step 4

I'd like to add a second sensor that activates a second LED. Any tips for connections or code?

0
Tarun1114
Tarun1114

11 months ago

am trying to interfacing the ARDUINO UNO board to ultrasonisensor and it will get output from the sensor but i did't know what the problem it will display once in a time ,after the excuited the code.so please provide me solution for this code.

0
amaanbubber
amaanbubber

1 year ago

can anyone give me the code

0
Unknown4
Unknown4

1 year ago

Excellent project for people (like me) who are just starting with Arduino
Just a tip: It's ok to skip the resistor

0
SaidiDikra
SaidiDikra

Reply 1 year ago

Yes ! If you use the blue LED

0
BraveG1
BraveG1

Question 1 year ago

Sir do you have codes to control two LED's in 1 ultrasonic sensor ? thank you for your response sir.

0
DIY Hacks and How Tos

Cool project. You should enter this into the Arduino contest that is currently running on the site.