This circuit is designed to control two servo motors based on input from an IR sensor using an Arduino UNO. When the IR sensor detects an object, the servo motors will open. When the object is no longer detected, the servo motors will remain closed.
Arduino UNO
Servo Motor 9G (Servo 1)
Servo Motor 9G (Servo 2)
IR Sensor
/*
* This Arduino sketch controls two servo motors based on input from an IR sensor.
* When the IR sensor detects an object (e.g., a rail), the servo motors will open.
* When the object is no longer detected, the servo motors will always remain closed.
*/
#include <Servo.h>
Servo servo1; // Servo motor connected to pin D9
Servo servo2; // Servo motor connected to pin D10
const int irSensorPin = 2; // IR sensor output connected to pin D2
const int servo1Pin = 9; // Servo motor 1 control pin
const int servo2Pin = 10; // Servo motor 2 control pin
void setup() {
// Initialize the IR sensor pin as input
pinMode(irSensorPin, INPUT);
// Attach the servo motors to their respective pins
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
// Initialize servos to closed position
servo1.write(90); // Closed position
servo2.write(90); // Closed position
}
void loop() {
// Read the IR sensor value
int irSensorValue = digitalRead(irSensorPin);
// If the IR sensor detects an object
if (irSensorValue == HIGH) {
// Open the servo motors
servo1.write(0); // Open position
servo2.write(0); // Open position
} else {
// Keep the servo motors closed
servo1.write(90); // Closed position
servo2.write(90); // Closed position
}
// Small delay to avoid rapid switching
delay(100);
}
This code initializes the IR sensor and two servo motors. When the IR sensor detects an object, the servo motors move to the open position. When the object is no longer detected, the servo motors return to the closed position.