0% found this document useful (0 votes)
5 views

project

The document provides an Arduino code for an ultrasonic sensor project that measures distance and activates an LED and buzzer based on the distance detected. It includes setup instructions for connecting the ultrasonic sensor, LED, and buzzer to the Arduino. The code continuously measures distance and outputs the value to the serial monitor while controlling the LED and buzzer accordingly.

Uploaded by

piyush sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

project

The document provides an Arduino code for an ultrasonic sensor project that measures distance and activates an LED and buzzer based on the distance detected. It includes setup instructions for connecting the ultrasonic sensor, LED, and buzzer to the Arduino. The code continuously measures distance and outputs the value to the serial monitor while controlling the LED and buzzer accordingly.

Uploaded by

piyush sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Arduino.

h>

#define trigPin 7

#define echoPin 6

#define ledPin 13

#define buzzerPin 3

int sound = 250;

void setup() {

Serial.begin(9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(ledPin, OUTPUT);

pinMode(buzzerPin, 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 <= 30) {

digitalWrite(ledPin, HIGH);

sound = 250;

} else {

digitalWrite(ledPin, LOW);

if (distance > 30 || distance <= 0) {

Serial.println("Out of range");

noTone(buzzerPin);

} else {

Serial.print(distance);

Serial.println(" cm");

tone(buzzerPin, sound);

delay(500);

}
Step 1: Connect the Ultrasonic Sensor to the Arduino:

 VCC (or +5V) of the sensor to 5V pin on the Arduino


 GND of the sensor to GND pin on the Arduino
 Trig (Trigger) pin of the sensor to pin 7 on the Arduino
 Echo pin of the sensor to pin 6 on the Arduino

Step 2: Connect the LED to the Arduino:

 Anode (longer leg) of the LED to pin 13 on the Arduino


 Cathode (shorter leg) of the LED to GND on the Arduino through a
current-limiting resistor (e.g., 220 ohms)

Step 3: Connect the Buzzer to the Arduino:

 Positive (longer pin or marked pin) of the Buzzer to pin 3 on the


Arduino
 Negative (shorter pin) of the Buzzer to GND on the Arduino

Step 4: Power the Arduino:

 Connect the Arduino to your computer using a USB cable for power.

You might also like