Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled DC Motor with Dual IR Sensors and Tilt Detection

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled DC Motor with Dual IR Sensors and Tilt Detection

Circuit Documentation

Summary

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 IR sensors and a tilt sensor to provide input signals to the Arduino, which then processes these signals to control the motor's operation based on the sensor readings. The IR sensors are used to detect objects or movement, while the tilt sensor detects the orientation of the device. The Arduino UNO controls the motor driver, which in turn drives the gearmotor. The motor is enabled or disabled based on the logic determined by the sensor inputs.

Component List

Arduino UNO

L298N DC Motor Driver

Gearmotor DC / Motorreductor

IR Sensor

Tilt Sensor

Wiring Details

Arduino UNO

L298N DC Motor Driver

Gearmotor DC / Motorreductor

IR Sensors

Tilt Sensor

Documented Code

const int irSensor1 = 6;
const int irSensor2 = 7;
const int tiltSensor = 2;
const int motorEnable = 3;
const int motorIn3 = 5;
const int motorIn4 = 4;

bool IsTilt = false;
bool OnlyOneIR = false;

void setup() {
  Serial.begin(9600);

  pinMode(irSensor1, INPUT);
  pinMode(irSensor2, INPUT);
  pinMode(tiltSensor, INPUT);

  pinMode(motorEnable, OUTPUT);
  pinMode(motorIn3, OUTPUT);
  pinMode(motorIn4, OUTPUT);

  digitalWrite(motorEnable, LOW);
  digitalWrite(motorIn3, LOW);
  digitalWrite(motorIn4, LOW);
}

void loop() {
  int irSensor1Value = digitalRead(irSensor1);
  int irSensor2Value = digitalRead(irSensor2);
  int tiltSensorValue = digitalRead(tiltSensor);

  IsTilt = (tiltSensorValue == HIGH);
  OnlyOneIR = (irSensor1Value != irSensor2Value);

  Serial.print("IsTilt: ");
  Serial.print(IsTilt);
  Serial.print(" | OnlyOneIR: ");
  Serial.println(OnlyOneIR);

  if (IsTilt && OnlyOneIR) {
    digitalWrite(motorEnable, HIGH);
    digitalWrite(motorIn3, HIGH);
    digitalWrite(motorIn4, LOW);
  } else {
    digitalWrite(motorEnable, LOW);
    digitalWrite(motorIn3, LOW);
    digitalWrite(motorIn4, LOW);
  }

  delay(737);
}

The code is written for the Arduino UNO and is responsible for reading the sensor inputs and controlling the motor driver. The IR sensors are connected to digital pins D6 and D7, while the tilt sensor is connected to digital pin D2. The motor driver's enable and input pins are controlled by digital pins D3, D4, and D5. The code includes a setup routine to initialize the pins and a loop routine that reads the sensor values, processes the logic, and controls the motor accordingly. The motor is activated only when the tilt sensor is triggered and only one of the IR sensors detects an object.