Amazing invention. Arduino project. Experiment Lab

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

Components:

Arduino Uno
HC-SR04 Ultrasonic Sensor
Servo Motor (e.g., SG90)
Jumper wires

Wiring:

Connect the Sonar Sensor:
Connect the VCC pin of the sonar sensor to the 5V output on the Arduino.
Connect the GND pin of the sonar sensor to the GND on the Arduino.
Connect the TRIG pin of the sonar sensor to digital pin 2 on the Arduino.
Connect the ECHO pin of the sonar sensor to digital pin 3 on the Arduino.

Connect the Servo Motor:
Connect the red (power) wire of the servo to the 5V output on the Arduino.
Connect the brown (ground) wire of the servo to the GND on the Arduino.
Connect the orange (signal) wire of the servo to digital pin 9 on the Arduino.

Power Supply:
Connect the Arduino Uno to your computer using a USB cable for power.
Your hardware setup should now be ready. Make sure your components are powered properly, and upload the provided Arduino code to your Arduino Uno using the Arduino IDE. The code will control the servo based on the distance measured by the sonar sensor.

Code:

Check 1st comment to get code.

Subscribe our channel to get more videos.
Thanks❤
Рекомендации по теме
Комментарии
Автор

#include <Servo.h>

const int trigPin = 2; // Trig pin of the sonar sensor
const int echoPin = 3; // Echo pin of the sonar sensor
const int servoPin = 9; // Signal pin of the servo motor

Servo myServo;

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

void loop() {
// Measure distance
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0343; // Speed of sound is 343 m/s at sea level

Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// Check distance and control servo
if (distance < 10) {
// If distance is less than 10 cm, move servo from 0 to 90 degrees
myServo.write(0);
delay(2000); // Wait for 2 seconds

// Move servo from 90 to 0 degrees
myServo.write(90);
delay(2000); // Wait for 2 seconds
}
}

experiment_lab_bd