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.

Component List

Arduino UNO

L298N DC Motor Driver

Gearmotor DC / Motorreductor

IR Sensor (2x)

Tilt Sensor

Wiring Details

Arduino UNO

L298N DC Motor Driver

Gearmotor DC / Motorreductor

IR Sensors

Tilt Sensor

Documented Code

// Define pin connections
int tiltPin = 2;
int irSensor1Pin = 7;
int irSensor2Pin = 6;
int motorEnPin = 3;
int motorIn3Pin = 5;
int motorIn4Pin = 4;

// Variables to store sensor states
bool IsTilt = false;
bool OnlyOneIR = false;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Configure pin modes
  pinMode(tiltPin, INPUT);
  pinMode(irSensor1Pin, INPUT);
  pinMode(irSensor2Pin, INPUT);
  pinMode(motorEnPin, OUTPUT);
  pinMode(motorIn3Pin, OUTPUT);
  pinMode(motorIn4Pin, OUTPUT);

  // Ensure motor is off initially
  digitalWrite(motorEnPin, LOW);
  digitalWrite(motorIn3Pin, LOW);
  digitalWrite(motorIn4Pin, LOW);
}

void loop() {
  // Read sensor values
  int tiltValue = digitalRead(tiltPin);
  int irSensor1Value = digitalRead(irSensor1Pin);
  int irSensor2Value = digitalRead(irSensor2Pin);

  // Update sensor state variables
  IsTilt = (tiltValue == HIGH);
  OnlyOneIR = (irSensor1Value != irSensor2Value);

  // Print sensor states to serial monitor
  Serial.print("Tilt: ");
  Serial.print(tiltValue);
  Serial.print(" IR1: ");
  Serial.print(irSensor1Value);
  Serial.print(" IR2: ");
  Serial.print(irSensor2Value);
  Serial.print(" IsTilt: ");
  Serial.print(IsTilt);
  Serial.print(" OnlyOneIR: ");
  Serial.println(OnlyOneIR);

  // Control motor based on sensor states
  if (IsTilt && OnlyOneIR) {
    digitalWrite(motorEnPin, HIGH);
    digitalWrite(motorIn3Pin, HIGH);
    digitalWrite(motorIn4Pin, LOW);
  } else {
    digitalWrite(motorEnPin, LOW);
    digitalWrite(motorIn3Pin, LOW);
    digitalWrite(motorIn4Pin, LOW);
  }
}

This code is designed to read the states of the tilt sensor and the two IR sensors. If the tilt sensor is activated and only one of the IR sensors is triggered, the motor is turned on in one direction. Otherwise, the motor remains off. The sensor states and the logic result are printed to the serial monitor for debugging purposes.