This circuit integrates an HC-SR04 Ultrasonic Sensor and two SG90 servo motors controlled by an Arduino UNO microcontroller. The ultrasonic sensor is used to measure distances, and the servo motors are used to perform mechanical movements based on the sensor's readings. The Arduino UNO reads the distance values from the ultrasonic sensor and controls the position of the servo motors accordingly.
// Include the Servo library
#include <Servo.h>
// Define Trig and Echo pins of Ultrasonic sensor
const int trigpin = 10;
const int echopin = 11;
// variables for the duration and the distance
long duration;
int distance;
// Create servo objects for controlling the servo motors
Servo myServo1;
Servo myServo2;
void setup() {
pinMode(trigpin, OUTPUT); // set the trigpin as an Output
pinMode(echopin, INPUT); // set the echopin as an Input
Serial.begin(9600);
myServo1.attach(12); // Defines on which pin is the first servo motor attached
myServo2.attach(13); // Defines on which pin is the second servo motor attached
}
void loop() {
// rotates the first servo motor from 15 to 165 degrees
for (int i = 15; i <= 165; i++) {
myServo1.write(i);
delay(30);
distance = calculateDistance(); // calls a function for calculating the distance measured by the ultrasonic sensor for each degree
Serial.print(i); // Sends the current degree into the Serial port
Serial.print(","); // Sends additional character right next to the previous value needed later in the processing IDE for indexing
Serial.print(distance); // sends the distance value into the serial port
Serial.print("."); // Sends addition character right next to the previous value
}
// Repeats the previous line from 165 to 15 degrees
for (int i = 165; i >= 15; i--) {
myServo1.write(i);
delay(30);
distance = calculateDistance();
Serial.print(i);
Serial.print(",");
Serial.print(distance);
}
// Check if the ultrasonic sensor detects an obstruction
if (distance <= 10) { // Adjust the distance threshold as needed
// Rotate the second servo motor to 180 degrees immediately
myServo2.write(180);
} else {
// Reset the second servo motor to its original position (e.g., 0 degrees)
myServo2.write(0);
}
}
// Function to calculate the distance measured by the ultrasonic sensor
int calculateDistance() {
// Send a pulse of 10 microseconds to the trigpin
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
// Measure the time it takes for the pulse to return to the echopin
duration = pulseIn(echopin, HIGH);
// Calculate the distance based on the speed of sound
distance = (duration / 2) * 0.0343; // Assuming the speed of sound is 343 m/s
return distance;
}
The code provided is for the Arduino UNO microcontroller. It includes the setup and loop functions, which are standard in Arduino programming. The setup
function initializes the pins and attaches the servo motors to their respective pins. The loop
function contains the logic for rotating the servo motors and reading the distance from the ultrasonic sensor. The calculateDistance
function calculates the distance based on the duration of the ultrasonic pulse's echo.