pat
Published © LGPL

Joystick-Controlled Car

Create a joystick-controlled 2-wheel car with Arduino! Explore robotics and coding with this exciting hands-on project.

BeginnerFull instructions provided148
Joystick-Controlled Car

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Joystick Unit MEGA328P I2C/Grove Connector
M5Stack Joystick Unit MEGA328P I2C/Grove Connector
×1
AA Batteries
AA Batteries
×4

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long
Premium Female/Female Jumper Wires, 40 x 3" (75mm)
Premium Female/Female Jumper Wires, 40 x 3" (75mm)
Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Schematics

Code

Code

C/C++
#include <AFMotor.h>

// Initialize motors
AF_DCMotor leftMotor(1); // Motor 1 connected to M1
AF_DCMotor rightMotor(4); // Motor 2 connected to M4

// Joystick pins
const int joystickX = A0;
const int joystickY = A1;

void setup() {
  leftMotor.setSpeed(200); // Set initial speed values
  rightMotor.setSpeed(200); // Set initial speed values
}

void loop() {
  int xValue = analogRead(joystickX); // Read X-axis value
  int yValue = analogRead(joystickY); // Read Y-axis value

  if (yValue < 400) {
    moveForward();
  } else if (yValue > 600) {
    moveBackward();
  } else if (xValue < 400) {
    turnLeft();
  } else if (xValue > 600) {
    turnRight();
  } else {
    moveStop();
  }
}

void moveForward() {
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);
}

void moveBackward() {
  leftMotor.run(BACKWARD);
  rightMotor.run(BACKWARD);
}

void turnLeft() {
  leftMotor.run(BACKWARD);
  rightMotor.run(FORWARD);
}

void turnRight() {
  leftMotor.run(FORWARD);
  rightMotor.run(BACKWARD);
}

void moveStop() {
  leftMotor.run(RELEASE);
  rightMotor.run(RELEASE);
}

Credits

pat
13 projects • 4 followers
AI Convergence Engineering, Software Engineering, ML, deep learning, image processing, computer vision, circuit design, and Edge AI devices.

Comments