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

Arduino Nano-Controlled Obstacle-Avoiding Motor System

View and Edit Project In Cirkit DesignerImage of Arduino Nano-Controlled Obstacle-Avoiding Motor System

Circuit Documentation

Summary

This circuit is designed to control a motor using an Arduino Nano microcontroller in conjunction with an L298N Motor Driver. The system also includes an HC-SR04 Ultrasonic Distance Sensor to measure distances and a rocker switch to control power flow. The motor is powered by a 12V battery, while the Arduino Nano, L298N Motor Driver, and the ultrasonic sensor are powered by a separate 5V battery. The Arduino Nano is programmed to read the distance from the ultrasonic sensor and control the motor through the motor driver based on the distance detected and the state of the rocker switch.

Component List

Wiring Details

Arduino Nano

HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

5V Battery

Motor Amarillo Motorreductor Hobby

Rocker Switch

L298N Motor Driver Controller Board Module Stepper Motor

12V Battery

Documented Code

Arduino Nano (Main Controller)

// Define pins for the ultrasonic sensor
const int trigPin = 9;
const int echoPin = 10;

// Define button and motor control pins
const int buttonPin = 7;
const int motorPin = 6;

// Variables to store the sensor reading
long duration;
int distance;

void setup() {
  // Set up pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);  // Button with internal pull-up resistor
  pinMode(motorPin, OUTPUT);
  
  // Start serial communication for debugging
  Serial.begin(9600);
}

void loop() {
  // Check if the button is pressed
  if (digitalRead(buttonPin) == LOW) {
    // Trigger the ultrasonic sensor
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Read the echo pin to calculate distance
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034 / 2;  // Convert to cm

    // Print distance for debugging
    Serial.print("Distance: ");
    Serial.println(distance);

    // If no obstacle is close, move the wheelchair (turn on motor)
    if (distance > 20) {
      digitalWrite(motorPin, HIGH);  // Turn on the motor
    } else {
      digitalWrite(motorPin, LOW);   // Stop the motor if an obstacle is near
    }
  } else {
    // If button is not pressed, stop the motor
    digitalWrite(motorPin, LOW);
  }
}

This code is responsible for controlling the motor based on the distance measured by the ultrasonic sensor and the state of the rocker switch. The motor is activated only if the distance is greater than 20 cm and the rocker switch is turned on.