Arduino UNO Controlled Dual Servo Motor System with IR Sensor for Object Detection
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
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.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
Servo Motor 9G (Servo 1)
- Description: A small and lightweight servo motor.
- Pins: VCC 5V, PMW, GND
Servo Motor 9G (Servo 2)
- Description: A small and lightweight servo motor.
- Pins: VCC 5V, PMW, GND
IR Sensor
- Description: An infrared sensor used to detect objects.
- Pins: out, gnd, vcc
Wiring Details
Arduino UNO
- GND: Connected to GND of Servo Motor 1, Servo Motor 2, and IR Sensor.
- D10: Connected to PMW of Servo Motor 1.
- D9: Connected to PMW of Servo Motor 2.
- D2: Connected to out of IR Sensor.
Servo Motor 9G (Servo 1)
- VCC 5V: Connected to VCC 5V of Servo Motor 2 and vcc of IR Sensor.
- PMW: Connected to D10 of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
Servo Motor 9G (Servo 2)
- VCC 5V: Connected to VCC 5V of Servo Motor 1 and vcc of IR Sensor.
- PMW: Connected to D9 of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
IR Sensor
- out: Connected to D2 of Arduino UNO.
- gnd: Connected to GND of Arduino UNO.
- vcc: Connected to VCC 5V of Servo Motor 1 and Servo Motor 2.
Code Documentation
/*
* 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.