Build A Simple Arduino robot car
You can build a simple 2‑wheel Arduino robot car with a motor driver and basic sensors in a few clear stages: gather parts, assemble chassis, wire electronics, upload code, and test.
1. Decide car type
First choose what you want the car to do, as this affects sensors and code.
Common beginner options:
Bluetooth control from phone – needs HC‑05 module.
Obstacle avoiding – needs ultrasonic sensor (HC‑SR04) + small servo (optional for scanning).
Line follower – needs 2–5 IR line sensors pointing to the floor.
Below steps assume a basic 2WD obstacle‑avoiding car (easiest to generalize).
2. Collect hardware
Typical low‑cost parts for a 2WD Arduino car:
Controller: Arduino Uno (or compatible).
Motor driver: L298N module or L293D motor driver shield.
Chassis kit: 2 geared DC motors with brackets, 2 wheels, acrylic/metal base, 1 caster wheel.
Power:
4x AA battery holder (6 V) or 2S Li‑ion/LiPo (7.4 V) for motors.
Optionally separate 9 V / USB for Arduino, or power Arduino from motor battery via driver 5 V/“+5V” pin (if available and safe on module).
Sensors: HC‑SR04 ultrasonic sensor; SG90 micro servo if you want the “radar” that turns.
Misc: Jumper wires (male–male, male–female), switch, screws/nuts/spacers, double‑sided tape or glue gun.
3. Build chassis and mount motors
Mechanical assembly is mostly straightforward; follow your kit diagram.
Fix the 2 DC gear motors to the chassis side slots using the provided brackets and screws.
Push the wheels onto the motor shafts; ensure both sit at equal height.
Attach the caster wheel at the opposite side (front or rear) to keep the car balanced.
Mount Arduino Uno and motor driver board on top of the chassis with screws or tape, leaving space for the battery pack.
4. Wire motors to motor driver
Use the motor driver to drive the two DC motors.
For an L298N module (example):
Connect left motor wires to OUT1 and OUT2 of L298N.
Connect right motor wires to OUT3 and OUT4.
If a motor spins backwards later, simply swap its two wires at the driver.
5. Connect motor driver to Arduino
Choose 4 digital pins to control the motors; many tutorials use pins 8–11.
Example mapping for L298N:
IN1 → Arduino D8
IN2 → Arduino D9
IN3 → Arduino D10
IN4 → Arduino D11
ENA, ENB → either tie to 5 V (full speed) or connect to PWM pins (e.g., D5, D6) for speed control.
GND of L298N → GND of Arduino (common ground is mandatory).
6. Power wiring
Power safely to avoid burning the board.
Connect battery pack positive to L298N 12V (or “+12V/VMOT”) terminal.
Connect battery pack negative to L298N GND.
Bridge L298N GND to Arduino GND.
If your L298N has a regulated 5V OUT and the battery is 7–12 V, you can connect this 5 V to Arduino Vin (or 5 V if the board/module explicitly supports that) to power Arduino from same battery.
If unsure, simplest and safest beginner setup is:
Motors on separate 6–12 V pack via driver.
Arduino powered by USB or 9 V battery via barrel jack.
7. Connect the ultrasonic sensor (HC‑SR04)
Typical pins: VCC, GND, TRIG, ECHO.
VCC → Arduino 5 V.
GND → Arduino GND.
TRIG → Arduino digital 2.
ECHO → Arduino digital 3 (use a pin that supports interrupt in some examples).
If sensor is on a servo:
Servo signal wire → Arduino D9 (or another PWM pin).
Servo VCC → 5 V (better from driver 5 V or separate 5 V regulator if current is high).
Servo GND → Arduino GND.
8. Basic obstacle‑avoiding code (steps)
Most online projects give you a ready sketch; workflow is similar:
Install Arduino IDE and select Board: Arduino Uno and the correct COM port.
In
setup(), set motor pins asOUTPUT, TRIG asOUTPUT, ECHO asINPUT.Write helper functions:
forward(),backward(),turnLeft(),turnRight(),stopCar()that set IN1–IN4 accordingly.
To measure distance:
Make TRIG low, then high for 10 microseconds, then low.
Use
pulseIn(ECHO, HIGH)to get echo time, convert to cm bydistance = duration * 0.034 / 2.
In
loop():Read distance in front.
If distance > some threshold (e.g., 20 cm) →
forward().Else →
stopCar(), smallbackward(), then turn left or right for a short time, then move forward again.
Upload the sketch and watch the wheels; if “forward” is reversed, swap motor leads or invert logic of the pins.
You can copy a working full sketch from a public obstacle‑avoiding car project and only adjust the pin numbers to match your wiring.
9. Test and iterate
Debug in small steps to avoid confusion.
First, upload a simple sketch that runs both motors forward for 2 seconds, then backward, then stop, to check wiring.
Next, test the HC‑SR04 alone by printing distance to
Serial Monitorto confirm sensor reading.Finally, combine both pieces into the main obstacle‑avoiding sketch and tune threshold distance and motor speed for smoother movement.
If you tell what exact hardware you already have (L298N vs shield, obstacle vs Bluetooth vs line follower), a concrete wiring diagram and ready‑to‑upload code can be tailored for that setup.
Core Chassis Parts
These form the basic robot structure:
2x L-Type BO motors (geared DC motors).
2x Wheels (likely rubber tires).
Caster wheel for balance.
Motor Drivers
Multiple driver options listed for controlling motors:
| Quantity | Item | Price (₹) | Use |
|---|---|---|---|
| 2 | L298N dual H-bridge | 200–250 | Main motor driver for 2WD car |
| 1–2 | L293D drivers | 150–200 | Alternative or shield version |
Sensors Available
Your list covers obstacle avoidance, line following, and more:
Ultrasonic sensor (HC-SR04) – distance measurement.
IR line tracking sensors (2–3x modules).
Temperature/humidity sensor (DHT11).
Buzzer and LEDs for feedback.
Wiring and Power
Essential connectors and power items:
Jumper wires (male-female, 20–40cm lengths, multiple colors).
Battery holders (AA or 18650 Li-ion).
Switches and fuses.
Next Build Steps
With this kit, assemble a 2WD obstacle-avoiding car first:
Power: batteries to L298N 12V/GND, share 5V to Arduino.
/* Code to test the motors on the Elegoo Robot Car 4Martin H.Southern Illinois University, for Automotive TechnologiesDownload, disconnect cable, place on floor with room to drive (about 4 feet clearance in all directions)Press the mode button, wait 2 seconds for it to begin*/// define IO pin#define PWMA 5 // Controls power to right motor#define PWMB 6 // Controls power to left motor#define AIN 7 // Controls direction of right motor, HIGH = FORWARD, LOW = REVERSE#define BIN 8 // Controls direction of right motor, HIGH = FORWARD, LOW = REVERSE#define STBY 3 // Place H-Bridge in standby if LOW, Run if HIGH#define modeSwitch 2 // Mode Switch input//init the carvoid setup() {pinMode(PWMA, OUTPUT); //set IO pin mode OUTPUTpinMode(PWMB, OUTPUT);pinMode(BIN, OUTPUT);pinMode(AIN, OUTPUT);pinMode(STBY, OUTPUT);digitalWrite(STBY, HIGH); //Enable Motors to rundigitalWrite(PWMA, LOW); // Fully on// digitalWrite(PWMA, HIGH); // Fully on}//main loopvoid loop() {while (digitalRead(modeSwitch) == 1) // wait for mode switch to be pressed (go to 0); // Do nothingdelay(2000);// RIGHT WHEELS// Drive Right Wheels forward for 2 seconddigitalWrite(AIN, HIGH); // Forward directiondigitalWrite(PWMA, HIGH); // Full powerdelay(2000); //delay 2000mS// Stop for 2 secondsstopTime(2000);// Drive Right Wheels Backwards for 2 seconddigitalWrite(AIN, LOW); // Backwards directiondigitalWrite(PWMA, HIGH); // Full powerdelay(2000); //delay 2000mS// Stop for 2 secondsstopTime(2000);// Turn off Right Motor's PowerdigitalWrite(PWMA, LOW);// DRIVE LEFT WHEELS// Drive LEFT Wheels forward for 2 seconddigitalWrite(BIN, HIGH); // Forward directiondigitalWrite(PWMB, HIGH); // Full powerdelay(2000); //delay 2000mS// Stop for 2 secondsstopTime(2000);// Drive Left Wheels Backwards for 2 seconddigitalWrite(BIN, LOW); // Forward directiondigitalWrite(PWMB, HIGH); // Full powerdelay(2000); //delay 2000mS// Stop for 2 secondsstopTime(2000);// Drive car Forward for 1 seconddigitalWrite(AIN, HIGH); // Forward direction on RightdigitalWrite(BIN, HIGH); // Forward direction on LeftdigitalWrite(PWMA, HIGH); // Full power on RightdigitalWrite(PWMB, HIGH); // Full power on Leftdelay(1000); //delay 2000mS// stop for 2 secondsstopTime(2000);// Drive car Backwards for 1 second1digitalWrite(AIN, LOW); // Reverse direction on RightdigitalWrite(BIN, LOW); // Reverse direction on Leftdelay(1000);// stop for 2 secondsstopTime(2000);// All motor power offdigitalWrite(PWMA, LOW); // No power on RightdigitalWrite(PWMB, LOW); // No power on Left}// Function - accepts the time in milli-Seconds to go into standby forvoid stopTime(int mS){digitalWrite(STBY, LOW); // Go into standbydelay(mS); // Wait defined timedigitalWrite(STBY, HIGH); // Come out of standby}
Upload the code from the above response.
Links:
https://www.instructables.com/Smart-Robot-Car/
https://www.instructables.com/Line-Follower-Car/
https://forum.arduino.cc/t/how-to-code-arduino-car/914284/6
Comments