This circuit is designed to control a pair of DC motors using an Arduino UNO microcontroller in conjunction with a 2-channel relay module. The control input is provided by a KY-023 Dual Axis Joystick Module. An HC-SR04 Ultrasonic Sensor is included for distance measurement, which can be used for obstacle detection or distance-based control logic. The circuit is powered by a 12V 7Ah battery, and a LM78xx Voltage Regulator is used to step down the voltage to 5V required by the Arduino UNO and other 5V components.
pin 1
of both motors connected to N.O. 1
and N.O. 2
on the relay module.pin 2
of both motors connected to N.O. 2
on the relay module.VCC
connected to 5V
on the Arduino UNO.GND
connected to GND
on the Arduino UNO.TRIG
connected to D2
on the Arduino UNO.ECHO
connected to D3
on the Arduino UNO.Input
connected to 12v +
on the battery.GND
connected to 12v -
on the battery.Output
providing 5V
to the Arduino UNO and other 5V components.GND
connected to GND
on the Arduino UNO.+5V
connected to 5V
on the Arduino UNO.VRx
connected to A0
on the Arduino UNO.VRy
connected to A1
on the Arduino UNO.5V
and GND
pins used to distribute power to other 5V components.A0
and A1
connected to the joystick module.D2
and D3
connected to the ultrasonic sensor.D4
, D5
, D6
, and D7
connected to the relay module inputs.12v +
connected to the voltage regulator input and COM 1
and COM 2
on the relay module.12v -
connected to the voltage regulator ground and VCC- (GND)
on the relay module.VCC+
connected to 5V
on the Arduino UNO.VCC- (GND)
connected to GND
on the Arduino UNO.IN 1
and IN 2
controlled by digital pins D4
, D5
, D6
, and D7
on the Arduino UNO.// Define pins for joystick
const int joystickX = A0; // Analog pin for joystick X-axis
const int joystickY = A1; // Analog pin for joystick Y-axis
// Define pins for relay module
const int relayPin1 = 5; // Relay pin 1 (controls Motor 1)
const int relayPin2 = 6; // Relay pin 2 (controls Motor 2)
void setup() {
// Initialize joystick pins as input
pinMode(joystickX, INPUT);
pinMode(joystickY, INPUT);
// Initialize relay pins as output
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
}
void loop() {
int xValue = analogRead(joystickX); // Read joystick X value
int yValue = analogRead(joystickY); // Read joystick Y value
// Map the joystick values to motor control logic
if (yValue < 400) {
// Move forward
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
} else if (yValue > 600) {
// Move backward
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
} else if (xValue < 400) {
// Turn left
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, HIGH);
} else if (xValue > 600) {
// Turn right
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, LOW);
} else {
// Stop
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
}
delay(100); // Delay for stability
}
This code snippet is responsible for reading the joystick's analog values and mapping them to control the DC motors via the relay module. The joystick's Y-axis controls forward and backward motion, while the X-axis controls left and right turns. The motors are stopped when the joystick is in the neutral position.