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.

  1. Fix the 2 DC gear motors to the chassis side slots using the provided brackets and screws.

  2. Push the wheels onto the motor shafts; ensure both sit at equal height.

  3. Attach the caster wheel at the opposite side (front or rear) to keep the car balanced.

  4. 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:

  1. Install Arduino IDE and select Board: Arduino Uno and the correct COM port.

  2. In setup(), set motor pins as OUTPUT, TRIG as OUTPUT, ECHO as INPUT.

  3. Write helper functions:

    • forward(), backward(), turnLeft(), turnRight(), stopCar() that set IN1–IN4 accordingly.

  4. To measure distance:

    • Make TRIG low, then high for 10 microseconds, then low.

    • Use pulseIn(ECHO, HIGH) to get echo time, convert to cm by distance = duration * 0.034 / 2.

  5. In loop():

    • Read distance in front.

    • If distance > some threshold (e.g., 20 cm) → forward().

    • Else → stopCar(), small backward(), then turn left or right for a short time, then move forward again.

  6. 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 Monitor to 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:

Motor Drivers

Multiple driver options listed for controlling motors:

QuantityItemPrice (₹)Use
2L298N dual H-bridge200–250Main motor driver for 2WD car 
1–2L293D drivers150–200Alternative 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:

  1. Mount motors and wheels to chassis.

  2. Wire L298N: motors to OUT1–4, Arduino pins 8–11 to IN1–4.

  3. Add HC-SR04 to pins 2 (trig), 3 (echo).

  4. Power: batteries to L298N 12V/GND, share 5V to Arduino.


/*  Code to test the motors on the Elegoo Robot Car 4
    Martin H.
    Southern Illinois University, for Automotive Technologies
    
    Download, 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 car
void setup() {
  pinMode(PWMA, OUTPUT);     //set IO pin mode OUTPUT
  pinMode(PWMB, OUTPUT);
  pinMode(BIN, OUTPUT);
  pinMode(AIN, OUTPUT);
  pinMode(STBY, OUTPUT);
  digitalWrite(STBY, HIGH);  //Enable Motors to run
  digitalWrite(PWMA, LOW);  // Fully on 
 // digitalWrite(PWMA, HIGH);  // Fully on
}

//main loop
void loop() {
  while (digitalRead(modeSwitch) == 1)  // wait for mode switch to be pressed (go to 0)
    ;  // Do nothing

  delay(2000);

  // RIGHT WHEELS
  // Drive Right Wheels forward for 2 second  
  digitalWrite(AIN, HIGH);    // Forward direction
  digitalWrite(PWMA, HIGH);   // Full power
  delay(2000);              //delay 2000mS

  // Stop for 2 seconds
  stopTime(2000);

  // Drive Right Wheels Backwards for 2 second  
  digitalWrite(AIN, LOW);    // Backwards direction
  digitalWrite(PWMA, HIGH);   // Full power
  delay(2000);              //delay 2000mS

  // Stop for 2 seconds
  stopTime(2000);

  // Turn off Right Motor's Power
  digitalWrite(PWMA, LOW);

  // DRIVE LEFT WHEELS
  // Drive LEFT Wheels forward for 2 second  
  digitalWrite(BIN, HIGH);    // Forward direction
  digitalWrite(PWMB, HIGH);   // Full power
  delay(2000);              //delay 2000mS

  // Stop for 2 seconds
  stopTime(2000);

  // Drive Left Wheels Backwards for 2 second  
  digitalWrite(BIN, LOW);    // Forward direction
  digitalWrite(PWMB, HIGH);   // Full power
  delay(2000);              //delay 2000mS

  // Stop for 2 seconds
  stopTime(2000);

  // Drive car Forward for 1 second
  digitalWrite(AIN, HIGH);    // Forward direction on Right
  digitalWrite(BIN, HIGH);    // Forward direction on Left
  digitalWrite(PWMA, HIGH);   // Full power on Right
  digitalWrite(PWMB, HIGH);   // Full power on Left
  delay(1000);              //delay 2000mS

  // stop for 2 seconds
  stopTime(2000);

  // Drive car Backwards for 1 second1
  digitalWrite(AIN, LOW);    // Reverse direction on Right
  digitalWrite(BIN, LOW);    // Reverse direction on Left
  delay(1000);

  // stop for 2 seconds
  stopTime(2000);
  
  // All motor power off
  digitalWrite(PWMA, LOW);   // No power on Right
  digitalWrite(PWMB, LOW);   // No power on Left
}

// Function - accepts the time in milli-Seconds to go into standby for
void stopTime(int mS){
  digitalWrite(STBY, LOW);   // Go into standby
  delay(mS);                //  Wait defined time
  digitalWrite(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

Popular posts from this blog

COBOT with GenAI and Federated Learning

Self-contained Raspberry Pi surveillance System Without Continue Internet

AI in Education: Embracing Change for Future-Ready Learning