Arduino Nano - Car

This tutorial instructs you how to build a IR-remote-controlled car use Arduino Nano, and IR remote controller.

IR remote control car

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×RC Car
1×L298N Motor Driver Module
1×IR Remote Controller Kit
1×CR2025 Battery (for IR Remote controller)
1×1.5V AA Battery (for Arduino Nano and Car)
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Robot Car

In the world of Arduino Nano, the robot car is commonly known as the RC car, remote control car, smart car, or DIY car. This nifty creation can be controlled from a distance using either an IR remote controller or a smartphone app via Bluetooth/WiFi. It can make sharp turns to the left or right and move forward or backward with ease.

A 2WD (Two-Wheel Drive) vehicle for Arduino Nano is a small robot car that you can assemble and operate using an Arduino Nano board. It usually consists of the following essential parts:

  • Chassis: This is like the car's body, where you attach all the other components.
  • Wheels: These are the two wheels that make the car move. They are connected to two DC motors.
  • Motors: Two DC motors are used to drive the two wheels.
  • Motor Driver: The motor driver board is a crucial component that connects the Arduino Nano to the motors. It takes signals from the Arduino Nano and gives power and control to the motors.
  • Arduino Nano Board: This is the car's brain. It reads inputs from sensors and user commands and controls the motors accordingly.
  • Power Source: The 2WD car needs a power source, usually batteries and a battery holder, to provide power to the motors and the Arduino Nano board.
  • Wireless Receiver: This is an infrared, Bluetooth, or WiFi module that enables wireless communication with a remote control or smartphone.
  • Optional Components: Depending on how advanced you want your project to be, you can add various optional parts like sensors (for example, ultrasonic sensors for avoiding obstacles or line-following sensors), and more.

In this tutorial, to make it simple, we will use:

  • 2WD Car kit (including Chassis, wheels, motors, battery holder)
  • L298N Motor Driver
  • IR infrared kit (including IR controller and IR receiver)

Check the hardware list at the top of this page.

How It Works

Arduino Nano 2WD car how it work
  • Arduino Nano connects to the DC motors of the robot car through L298N motor driver module.
  • Arduino Nano connects to an IR receiver.
  • The battery powers Arduino Nano, DC motors, motor driver, and IR receiver.
  • Users press the UP/DOWN/LEFT/RIGHT/OK keys on the IR remote controller.
  • Arduino Nano receives the UP/DOWN/LEFT/RIGHT/OK commands through the IR receiver.
  • Arduino Nano controls the car to move FORWARD/BACKWARD/LEFT/RIGHT/STOP by controlling the DC motor via the motor driver.

Wiring Diagram

The wiring diagram between Arduino Nano and 2WD car

This image is created using Fritzing. Click to enlarge image

Usually, we require two sources of power:

  • One for the motor (indirectly through the L298N module).
  • Another for the Arduino Nano board, L298N module, and IR receiver.

However, there's a way to simplify this and use only one power source for everything. You can achieve this by using four 1.5V batteries (totaling 6V). Here's how:

  • Connect the batteries to the L298N module following the diagram above.
  • Remove all of three jumpers on the L298N module.

Arduino Nano Code

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-car */ #include <DIYables_IRcontroller.h> // DIYables_IRcontroller library #define IR_RECEIVER_PIN 13 // The Arduino Nano pin connected to IR receiver #define ENA_PIN 7 // The Arduino Nano pin connected to the ENA pin L298N #define IN1_PIN 6 // The Arduino Nano pin connected to the IN1 pin L298N #define IN2_PIN 5 // The Arduino Nano pin connected to the IN2 pin L298N #define IN3_PIN 4 // The Arduino Nano pin connected to the IN3 pin L298N #define IN4_PIN 3 // The Arduino Nano pin connected to the IN4 pin L298N #define ENB_PIN 2 // The Arduino Nano pin connected to the ENB pin L298N DIYables_IRcontroller_17 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms void setup() { Serial.begin(9600); irController.begin(); pinMode(ENA_PIN, OUTPUT); pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); pinMode(IN3_PIN, OUTPUT); pinMode(IN4_PIN, OUTPUT); pinMode(ENB_PIN, OUTPUT); digitalWrite(ENA_PIN, HIGH); // set full speed digitalWrite(ENB_PIN, HIGH); // set full speed } void loop() { Key17 key = irController.getKey(); if (key != Key17::NONE) { switch (key) { case Key17::KEY_UP: Serial.println("MOVING FORWARD"); CAR_moveForward(); break; case Key17::KEY_DOWN: Serial.println("MOVING BACKWARD"); CAR_moveBackward(); break; case Key17::KEY_LEFT: Serial.println("TURNING LEFT"); CAR_turnLeft(); break; case Key17::KEY_RIGHT: Serial.println("TURNING RIGHT"); CAR_turnRight(); break; case Key17::KEY_OK: Serial.println("STOP"); CAR_stop(); break; default: Serial.println("WARNING: unused key:"); break; } } } void CAR_moveForward() { digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW); digitalWrite(IN3_PIN, HIGH); digitalWrite(IN4_PIN, LOW); } void CAR_moveBackward() { digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH); digitalWrite(IN3_PIN, LOW); digitalWrite(IN4_PIN, HIGH); } void CAR_turnLeft() { digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW); digitalWrite(IN3_PIN, LOW); digitalWrite(IN4_PIN, LOW); } void CAR_turnRight() { digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW); digitalWrite(IN3_PIN, HIGH); digitalWrite(IN4_PIN, LOW); } void CAR_stop() { digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW); digitalWrite(IN3_PIN, LOW); digitalWrite(IN4_PIN, LOW); }

Detailed Instructions

  • Install DIYables_IRcontroller library on Arduino IDE by following this instruction
  • Do the wiring as the diagram shown above.
  • Disconnect the wire from the Vin pin on the Arduino Nano because we will power Arduino Nano via USB cable when uploading code.
  • Flip the car upside down so that the wheels are on top.
  • Connect the Arduino Nano to your computer using the USB cable.
  • Copy the provided code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to transfer the code to the Arduino Nano.
  • Use the IR remote controller to make the car go forward, backward, left, right, or stop.
  • Check if the wheels move correctly according to your commands.
  • If the wheels move the wrong way, swap the wires of the DC motor on the L298N module.
  • You can also see the results on the Serial Monitor in the Arduino IDE.
COM6
Send
MOVING FORWARD MOVING BACKWARD TURNING LEFT TURNING RIGHT STOP
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • If everything is working fine, disconnect the USB cable from the Arduino Nano, and connect back the wire to Vin pin to power the Arduino Nano from the battery.
  • Flip the car back to its normal position with the wheels on the ground.
  • Have fun controlling the car!

Code Explanation

Read the line-by-line explanation in comment lines of code!

You can learn more about the code by checking the following tutorials:

You can extend this project by:

  • Adding obstacle avoidance sensors to immidilately stop the car if an obstacle is detected.
  • Adding function to control the speed of car (see Arduino Nano - DC motor tutorial). The provided code controls car with full speed.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!