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
- Arduino Nano: A compact microcontroller board based on the ATmega328P, featuring digital and analog I/O pins.
- HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible): A sensor that measures distance by emitting ultrasonic waves and measuring the time taken for the echo to return.
- 5V Battery: Provides power to the Arduino Nano, L298N Motor Driver, and the ultrasonic sensor.
- Motor Amarillo Motorreductor Hobby: A hobby motor that is controlled by the L298N Motor Driver.
- Rocker Switch: A switch that controls the power flow to the L298N Motor Driver and subsequently to the motor.
- L298N Motor Driver Controller Board Module Stepper Motor: A motor driver module that allows for control of one or two motors.
- 12V Battery: Provides power to the motor through the L298N Motor Driver.
Wiring Details
Arduino Nano
- D6: Connected to the Input 1 of the L298N Motor Driver to control the motor.
- D7: Connected to the output of the rocker switch to control the power flow.
- D9: Connected to the TRIG pin of the HC-SR04 Ultrasonic Distance Sensor to trigger distance measurement.
- D10: Connected to the ECHO pin of the HC-SR04 Ultrasonic Distance Sensor to receive the echo signal.
HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)
- VCC: Connected to the +5V power from the 5V Battery.
- TRIG: Connected to D9 on the Arduino Nano to receive trigger signals.
- ECHO: Connected to D10 on the Arduino Nano to send back the echo signal.
- GND: Connected to the ground of the 5V Battery.
5V Battery
- +: Connected to the +5V power input of the L298N Motor Driver, VCC of the HC-SR04 Ultrasonic Distance Sensor, and the rocker switch.
- -: Connected to the ground of the L298N Motor Driver and the HC-SR04 Ultrasonic Distance Sensor.
Motor Amarillo Motorreductor Hobby
- VCC: Connected to the Output A of the L298N Motor Driver to receive power.
- GND: Connected to the Output A of the L298N Motor Driver to complete the circuit.
Rocker Switch
- Output: Connected to D7 on the Arduino Nano to control the power flow.
- Input: Connected to the +5V power from the 5V Battery.
L298N Motor Driver Controller Board Module Stepper Motor
- Input 1: Connected to D6 on the Arduino Nano to receive control signals.
- +5V Power: Connected to the +5V power from the 5V Battery.
- Ground: Connected to the ground of the 5V Battery.
- Output A: Connected to the Motor Amarillo Motorreductor Hobby to control the motor.
- +12V Power: Connected to the +12V power from the 12V Battery to power the motor.
12V Battery
- +: Connected to the +12V power input of the L298N Motor Driver to power the motor.
- -: Connected to the ground of the L298N Motor Driver.
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.
