Arduino UNO Controlled DC Motor with IR and Tilt Sensor Feedback
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary of the Circuit
This circuit is designed to control a DC gearmotor using an Arduino UNO microcontroller in conjunction with an L298N DC motor driver. The system also includes two infrared (IR) sensors and a tilt sensor to provide input signals to the Arduino, which then processes these signals to determine the motor's operation. The motor is controlled based on the tilt sensor's state and the detection of objects by the IR sensors. The Arduino UNO board is responsible for reading sensor inputs, making decisions, and driving the motor through the motor driver.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Purpose: Acts as the central processing unit of the circuit, reading sensor inputs and controlling the motor driver.
L298N DC Motor Driver
- Description: A motor driver module capable of driving two DC motors.
- Purpose: Receives control signals from the Arduino and drives the gearmotor accordingly.
Gearmotor DC / Motorreductor
- Description: A DC motor with a gearbox for increased torque.
- Purpose: The actuator of the system, providing motion as controlled by the motor driver.
IR Sensor (2x)
- Description: An infrared sensor capable of detecting the presence of objects.
- Purpose: Provides input to the Arduino to detect objects in the vicinity of the sensor.
Tilt Sensor
- Description: A sensor that detects tilting motion.
- Purpose: Sends a signal to the Arduino when a tilt is detected.
Wiring Details
Arduino UNO
- 5V: Connected to the VCC pins of both IR sensors and the pin2 of the tilt sensor.
- GND: Connected to the GND pins of both IR sensors.
- D7: Connected to the OUT pin of the first IR sensor.
- D6: Connected to the OUT pin of the second IR sensor.
- D5: Connected to the IN3 pin of the L298N motor driver.
- D4: Connected to the IN4 pin of the L298N motor driver.
- D3: Connected to the ENB pin of the L298N motor driver.
- D2: Connected to the pin1 of the tilt sensor.
L298N DC Motor Driver
- IN3: Connected to D5 on the Arduino UNO.
- IN4: Connected to D4 on the Arduino UNO.
- ENB: Connected to D3 on the Arduino UNO.
- OUT3: Connected to Pin2 of the gearmotor.
- OUT4: Connected to Pin1 of the gearmotor.
Gearmotor DC / Motorreductor
- Pin1: Connected to OUT4 on the L298N motor driver.
- Pin2: Connected to OUT3 on the L298N motor driver.
IR Sensor
- VCC: Connected to 5V on the Arduino UNO.
- GND: Connected to GND on the Arduino UNO.
- OUT: Connected to D7 on the Arduino UNO (first sensor) and D6 on the Arduino UNO (second sensor).
Tilt Sensor
- Pin1: Connected to D2 on the Arduino UNO.
- Pin2: Connected to 5V on the Arduino UNO.
Documented Code
// Pin definitions
const int irSensor1Pin = 7;
const int irSensor2Pin = 6;
const int tiltSensorPin = 2;
const int motorEnPin = 3;
const int motorIn3Pin = 5;
const int motorIn4Pin = 4;
// Boolean variables
bool IsTilt = false;
bool OnlyOneIR = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize sensor pins
pinMode(irSensor1Pin, INPUT);
pinMode(irSensor2Pin, INPUT);
pinMode(tiltSensorPin, INPUT);
// Initialize motor control pins
pinMode(motorEnPin, OUTPUT);
pinMode(motorIn3Pin, OUTPUT);
pinMode(motorIn4Pin, OUTPUT);
}
void loop() {
// Read sensor values
int irSensor1Value = digitalRead(irSensor1Pin);
int irSensor2Value = digitalRead(irSensor2Pin);
int tiltSensorValue = digitalRead(tiltSensorPin);
// Update boolean variables
IsTilt = (tiltSensorValue == HIGH);
OnlyOneIR = (irSensor1Value != irSensor2Value);
// Print boolean states to serial monitor
Serial.print("IsTilt: ");
Serial.print(IsTilt);
Serial.print(" | OnlyOneIR: ");
Serial.println(OnlyOneIR);
// Control DC motor
if (IsTilt && OnlyOneIR) {
digitalWrite(motorEnPin, HIGH);
digitalWrite(motorIn3Pin, HIGH);
digitalWrite(motorIn4Pin, LOW);
} else {
digitalWrite(motorEnPin, LOW);
digitalWrite(motorIn3Pin, LOW);
digitalWrite(motorIn4Pin, LOW);
}
// Small delay to avoid spamming the serial monitor
delay(100);
}
Filename: sketch.ino
Description: This code is designed to run on an Arduino UNO. It reads inputs from two IR sensors and a tilt sensor to determine the state of the system. Based on these inputs, it controls a DC gearmotor through the L298N motor driver. The motor is enabled and set to rotate in a specific direction when the tilt sensor is triggered and only one of the IR sensors detects an object. The serial monitor outputs the status of the tilt and IR sensors for debugging purposes.