DEV Community

Cover image for Arduino Nano ESP32 - Car
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino Nano ESP32 - Car

A classic way to get past blinking LEDs with Arduino is building something that drives around. This build turns an Arduino Nano ESP32 into a 2WD robot car you steer with an IR remote: press UP, DOWN, LEFT, RIGHT or OK and the car moves accordingly. It's a good next project if you already know how to flash code to the board and want to combine a motor driver with a sensor input.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Nano ESP32 - Car

What you need

Wiring

Arduino Nano ESP32 - Car wiring diagram

The L9110S motor driver sits between the Arduino and the two DC motors. It has no onboard 5V regulator, so it draws motor power straight from its VCC and GND pins. That lets you run the whole car from a single source: four 1.5V AA batteries (6V) feed both the L9110S VCC/GND and the Arduino's Vin/GND. The IR receiver is powered from the Arduino's 5V pin instead. Make sure the L9110S, the Arduino, and the IR receiver all share a common GND, otherwise signals won't read correctly. Motor A connects to pins D5 and D6, motor B to D9 and D10, and the IR receiver signal goes to D11.

How it works

The IR remote sends a code when you press a key. The IR receiver picks that up and hands it to the Arduino on D11. The Arduino decodes which key was pressed and then drives the two DC motors through the L9110S by setting its input pins HIGH or LOW in different combinations: both motors spinning the same way moves the car forward or backward, and spinning them in opposite directions turns it left or right. The battery powers the ESP32, the motors, the driver, and the IR receiver all at once.

Code

The sketch uses the DIYables_IRcontroller library to read key presses from the IR receiver on D11. In loop(), it checks irController.getKey() and switches on the result: KEY_UP calls CAR_moveForward(), KEY_DOWN calls CAR_moveBackward(), KEY_LEFT and KEY_RIGHT turn the car, and KEY_OK stops it. Each movement function just sets the four motor pins (MOTOR_A_IA_PIN, MOTOR_A_IB_PIN, MOTOR_B_IA_PIN, MOTOR_B_IB_PIN) HIGH or LOW in the pattern needed for that direction, with digitalWrite(). The code runs the car at full speed only; speed control would need a different approach.

arduino-nano-esp32-car.ino

/*
 * This Arduino Nano ESP32 code was developed by newbiely.com
 *
 * This Arduino Nano ESP32 code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-car
 */

#include <DIYables_IRcontroller.h>  // IR Remote Controller library

#define IR_RECEIVER_PIN D11  // The Arduino Nano ESP32 pin connected to IR receiver
#define MOTOR_A_IA_PIN D5    // The Arduino Nano ESP32 pin connected to the A-IA pin of the L9110S (Motor A)
#define MOTOR_A_IB_PIN D6    // The Arduino Nano ESP32 pin connected to the A-IB pin of the L9110S (Motor A)
#define MOTOR_B_IA_PIN D9    // The Arduino Nano ESP32 pin connected to the B-IA pin of the L9110S (Motor B)
#define MOTOR_B_IB_PIN D10   // The Arduino Nano ESP32 pin connected to the B-IB pin of the L9110S (Motor B)

DIYables_IRcontroller_17 irController(IR_RECEIVER_PIN, 200);  // debounce time is 200ms

void setup() {
  Serial.begin(9600);
  irController.begin();

  pinMode(MOTOR_A_IA_PIN, OUTPUT);
  pinMode(MOTOR_A_IB_PIN, OUTPUT);
  pinMode(MOTOR_B_IA_PIN, OUTPUT);
  pinMode(MOTOR_B_IB_PIN, OUTPUT);
}

void loop() {
  Key17 key = irController.getKey();
  if (key != Key17::NONE) {
    switch (key) {
      case Key17::KEY_UP:
        Serial.println("CAR - MOVING FORWARD");
        CAR_moveForward();
        break;

      case Key17::KEY_DOWN:
        Serial.println("CAR - MOVING BACKWARD");
        CAR_moveBackward();
        break;

      case Key17::KEY_LEFT:
        Serial.println("CAR - TURNING LEFT");
        CAR_turnLeft();
        break;

      case Key17::KEY_RIGHT:
        Serial.println("CAR - TURNING RIGHT");
        CAR_turnRight();
        break;

      case Key17::KEY_OK:
        Serial.println("CAR - STOP");
        CAR_stop();
        break;

      default:
        Serial.println("WARNING: unused key:");
        break;
    }
  }
}

void CAR_moveForward() {
  digitalWrite(MOTOR_A_IA_PIN, HIGH);
  digitalWrite(MOTOR_A_IB_PIN, LOW);
  digitalWrite(MOTOR_B_IA_PIN, HIGH);
  digitalWrite(MOTOR_B_IB_PIN, LOW);
}

void CAR_moveBackward() {
  digitalWrite(MOTOR_A_IA_PIN, LOW);
  digitalWrite(MOTOR_A_IB_PIN, HIGH);
  digitalWrite(MOTOR_B_IA_PIN, LOW);
  digitalWrite(MOTOR_B_IB_PIN, HIGH);
}

void CAR_turnLeft() {
  digitalWrite(MOTOR_A_IA_PIN, HIGH);
  digitalWrite(MOTOR_A_IB_PIN, LOW);
  digitalWrite(MOTOR_B_IA_PIN, LOW);
  digitalWrite(MOTOR_B_IB_PIN, LOW);
}

void CAR_turnRight() {
  digitalWrite(MOTOR_A_IA_PIN, LOW);
  digitalWrite(MOTOR_A_IB_PIN, LOW);
  digitalWrite(MOTOR_B_IA_PIN, HIGH);
  digitalWrite(MOTOR_B_IB_PIN, LOW);
}

void CAR_stop() {
  digitalWrite(MOTOR_A_IA_PIN, LOW);
  digitalWrite(MOTOR_A_IB_PIN, LOW);
  digitalWrite(MOTOR_B_IA_PIN, LOW);
  digitalWrite(MOTOR_B_IB_PIN, LOW);
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

Before uploading code, disconnect the wire from Vin on the Arduino, since the board is powered over USB during programming and you don't want two power sources fighting. Flip the car upside down while testing so the wheels don't drive it off your desk. If the car doesn't respond to the remote, check that the IR receiver, Arduino, and L9110S all share the same GND, since a floating ground is a common reason signals look absent even though wiring seems correct.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-car

Top comments (0)