Skip to content

hc-sr04 #12072

@veillyin

Description

@veillyin

using ultrasonic distance hc-sr04 with buzzer, led and Arduino

jumper 1 to 5v
jumper 2 to gnd
jumper 3 to pin
jumper 4 to pin

the buzzer attach to breadboard. on + to pin. and - to gnd

the led attach to the breadboard. same (but with resistor)

code:
//defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

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

void setup() {
pinMode(trigPin, OUTPUT) ; // Sets the trigPin as an Ouput
pinMode(echoPin, INPUT) ; // Sets the echpoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledpin, OUTPUT);

// 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 (buzzer, HIGH);
digitalWrite (ledPin, HIGH);
}
else{
digitalWrite (buzzer, LOW);
digitalWrite (ledPin, LOW);
}

Metadata

Metadata

Assignees

Labels

Type: InvalidOff topic for this repository, or a bug report determined to not actually represent a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions