Make your All-in-One Smart Car with Arduino UNO | One Code Four Modes

Hello and welcome back. In this project, we will learn how to make an all in one smart robot car using the Arduino UNO board. Specifically, I used only one program for all the modes in this robot car. We can easily change the mode using just a push button. So, there’s no need to upload different programs one by one for each mode, just press the button and switch the function.

This smart robot car is simple to make and also low-cost. For the chassis, I used a 5mm piece of foam sheet. But you can use cardboard, a plastic sheet, or any other material you have. The design is fully DIY-friendly. This smart robot car mainly includes four modes:

  1. Obstacle avoidance mode – the car automatically avoids objects using an ultrasonic sensor.
  2. Bluetooth manual remote control – you can control the car using buttons in the app.
  3. Bluetooth gesture remote control – control the car by tilting your phone.
  4. Voice control – give voice commands to move the car.

For Bluetooth communication, I used the HC-05 Bluetooth module. Also, I used the SriTu Hobby Android app to control this smart robot car. You can download the app from the Play Store using this link. You can also adjust the obstacle detection distance value as you like. For that, please check the program. This is a really fun and educational project. You can learn about sensors, motor control, Arduino programming, and app communication all in one robot car.

Ok, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, let’s create the smart car chassis. For that, use the following dimensions. You can use any suitable material like foam board, cardboard, or a plastic sheet, based on what you have.

Step 3

Thirdly, solder the wires to the gear motors properly. After that, install the motors on the chassis in the correct position.

Step 4

After that, drill two small holes near the motors and pass the wires through them. This will help keep the wiring neat and organized.

Step 5

Next, connect the motor driver shield to the Arduino UNO board. Then, place the whole setup on top of the chassis and secure it properly. After that, connect the gear motors to the motor output terminals on the shield. For wiring reference, you can use the circuit diagram below.

All-in-One Smart Car with Arduino UNO | One Code Four Modes

Step 6

Now, install the servo motor at the front center of the chassis and fix it properly. Then, connect the servo motor to the motor driver shield as shown in the circuit diagram. After that, use a servo motor tester to set the servo to 90 degrees before attaching any sensor.

Step 7

Afterward, install the ultrasonic sensor on the servo motor horn carefully. Make sure it’s centered and tightly fixed. Then, connect the ultrasonic sensor to the motor driver shield as shown in the circuit diagram.

Step 8

Next, place the Bluetooth module, push button, and resistor on the breadboard. Then, install this breadboard setup on the chassis. After that, connect all these components to the motor driver shield according to the circuit diagram.

Step 9

Now, install the battery holder on the chassis and connect the power wires to the motor driver shield. After that, attach the robot wheels to the gear motors firmly.

Step 10

Afterward, connect the Arduino UNO board to your computer using a USB cable. Then, open the Arduino IDE and copy and paste the following program. Also, make sure to add the AFMotor library to the Arduino IDE before uploading the code.

All-in-One Smart Car with Arduino UNO | One Code Four Modes
#include <Servo.h>
#include <AFMotor.h>

#define Echo A0
#define Trig A1
#define button A2
#define motor 10
#define Speed 150
#define spoint 85

char value;
int distance;
int Left, Right;
int L = 0, R = 0;

// Mode flags
bool isObstacleMode = false;
bool isBluetoothMode = false;

Servo servo;
AF_DCMotor M1(1);
AF_DCMotor M2(2);
AF_DCMotor M3(3);
AF_DCMotor M4(4);

void setup() {
  Serial.begin(9600);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(button, INPUT);
  servo.attach(motor);
  M1.setSpeed(Speed);
  M2.setSpeed(Speed);
  M3.setSpeed(Speed);
  M4.setSpeed(Speed);
  servo.write(spoint);
  delay(1000);
  Serial.println("Robot Started. No mode selected. Press button to choose mode.");
}

void loop() {
  checkButton();

  if (isObstacleMode) {
    Obstacle();
  } else if (isBluetoothMode) {
    Bluetoothcontrol();
  } else {
    Stop(); // no mode selected - stop motors
  }

}

void checkButton() {
  if (digitalRead(button) == LOW) {
    while (digitalRead(button) == LOW) delay(10);
    delay(100);

    // Cycle modes: none -> obstacle -> bluetooth -> none ...
    if (!isObstacleMode && !isBluetoothMode) {
      isObstacleMode = true;
      isBluetoothMode = false;
      Serial.println("Switched to Obstacle Mode");
    } else if (isObstacleMode) {
      isObstacleMode = false;
      isBluetoothMode = true;
      Serial.println("Switched to Bluetooth Mode");
    } else if (isBluetoothMode) {
      isObstacleMode = false;
      isBluetoothMode = false;
      Serial.println("Stopped: no mode selected");
    }

    Stop();  
  }
}


// Obstacle Avoidance Mode
void Obstacle() {
  distance = ultrasonic();
  if (distance <= 12) {
    Stop();
    backward();
    delay(200);
    Stop();
    L = leftsee();
    delay(300);
    R = rightsee();
    delay(300);
    servo.write(spoint);
    delay(300);

    if (L < R) {
      right();
      delay(500);
      Stop();
    } else {
      left();
      delay(500);
      Stop();
    }
  } else {
    forward();
  }
}

void Bluetoothcontrol() {
  if (Serial.available() > 0) {
    value = Serial.read();
    Serial.print("BT Command: ");
    Serial.println(value);
    
    if (value == 'U') forward();
    else if (value == 'D') backward();
    else if (value == 'L') left();
    else if (value == 'R') right();
    else if (value == 'S') Stop();  
  }
}


// Motor control functions
void forward() {
  M1.run(FORWARD);
  M2.run(FORWARD);
  M3.run(FORWARD);
  M4.run(FORWARD);
}

void backward() {
  M1.run(BACKWARD);
  M2.run(BACKWARD);
  M3.run(BACKWARD);
  M4.run(BACKWARD);
}

void right() {
  M1.run(BACKWARD);
  M2.run(BACKWARD);
  M3.run(FORWARD);
  M4.run(FORWARD);
}

void left() {
  M1.run(FORWARD);
  M2.run(FORWARD);
  M3.run(BACKWARD);
  M4.run(BACKWARD);
}

void Stop() {
  M1.run(RELEASE);
  M2.run(RELEASE);
  M3.run(RELEASE);
  M4.run(RELEASE);
}

// Ultrasonic sensor distance function
int ultrasonic() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  long t = pulseIn(Echo, HIGH);
  return t / 29 / 2;
}

int rightsee() {
  servo.write(20);
  delay(500);
  return ultrasonic();
}

int leftsee() {
  servo.write(180);
  delay(500);
  return ultrasonic();
}
  • Now, select the correct board type and port from the Tools menu in the Arduino IDE. Then, unplug the Bluetooth module to avoid any uploading issues, and click the Upload button to upload the code to the Arduino UNO board.
  • After uploading is complete, reconnect the Bluetooth module. Then, open the Serial Monitor from the Arduino IDE. Now, press the push button on the car chassis. Then you will see the smart car modes displayed on the Serial Monitor one by one as you press the button.

Step 11

Next, remove the USB cable from the Arduino board and insert the batteries into the battery holder. If you don’t have Li-ion batteries and a holder, you can also use a 9V battery to power this smart robot car.

Step 12

Now, change the smart car mode to Obstacle Avoidance mode and check the motor directions. If the wheels don’t rotate forward, simply switch the motor wires and test again until all four wheels move in the correct direction.

Step 13

You can change between Obstacle Avoidance and Bluetooth modes using the push button. When the car is powered on, no mode is selected by default. Press the button once to activate Obstacle Avoidance mode. Press it a second time to switch to Bluetooth mode. Press it a third time to stop the car.

Step 14

Next, change the smart car to Bluetooth mode using the push button. Then, download and install the SriTu Hobby app from the Play Store.

  • SriTu Hobby App — Download
  • First, open the app and go to the Controllers page. Then, select the Bluetooth Car Remote button. Now you will see the manual remote.
  • Now, tap the gear wheel icon to open the Bluetooth device list. Find and select your Bluetooth module (like HC-05). At this point, make sure Location is turned on and the app has location permission enabled, this is required for Bluetooth scanning to work properly on Android devices.
  • Then, you will see a green indicator on the remote, which means the Bluetooth connection is successful. Now, you can control the smart car using the manual remote in the app.
  • Now, switch the remote to gesture mode by pressing the arrow icon in the app. Then, connect the smart car to the remote just like before using the Bluetooth connection.
  • You can control the smart car by tilting your phone like a remote. Tilt forward to move forward, backward to reverse, and left or right to turn.
  • Next, switch the remote to voice control mode by pressing the arrow icon again. Then, reconnect the smart car to the app just like before.
  • Now, press the voice command button in the app and say commands like “forward”, “backward”, “left”, “right”, and “stop” to control the smart car using your voice.

Okay, enjoy your smart robot car! The full video guide is given below to help you step by step. We hope to see you in the next project. Have a great day and happy making!

Make your All-in-One Smart Car with Arduino UNO | One Code Four Modes

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *