Arduino project. Ultrasonic sensor

preview_player
Показать описание
@experiment_lab_bd

Components Needed:
Arduino Uno
HC-SR04 Ultrasonic Sensor
Jumper wires

Wiring:
Connect VCC of the sensor to 5V on the Arduino Uno.
Connect GND of the sensor to GND on the Arduino Uno.
Connect TRIG of the sensor to digital pin 2 on the Arduino Uno.
Connect ECHO of the sensor to digital pin 3 on the Arduino Uno.
Connect an LED to digital pin 13 on the Arduino Uno.

Check 1st comment to get code.
Рекомендации по теме
Комментарии
Автор

const int trigPin = 2;
const int echoPin = 3;
const int ledPin = 13;

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

void loop() {
// Triggering the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reading the duration of the pulse from the sensor
long duration = pulseIn(echoPin, HIGH);

// Calculating distance in centimeters
int distance = duration * 0.034 / 2;

// Outputting the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// Checking if the distance is less than 10 cm
if (distance < 10) {
digitalWrite(ledPin, HIGH); // Set pin 13 high
} else {
digitalWrite(ledPin, LOW); // Set pin 13 low
}

delay(500); // Delay for stability
}

experiment_lab_bd
Автор

can you tell where did u put the jumper cables in Arduino

Lemennce
Автор

Thanks for the information!

If we have a long cable connected between Arduino and a sensor, will there be any impact on sensor readings due to long cable?

If yes, please suggest a solution to it.

Thank you!

ff
Автор

Can we use a different Aurdino ( I have Aurdino uno r3 CH340G

Augustdance