Arduino Smart Robot Car-Part 2

This is the second part tutorial of the smart robot car from banggood. In this video, I gonna cover all the electronic connections and test codes to make sure everything works.


Updates: please watch the part 3 tutorial for some of the connection updates here: Arduino Smart Robot Car from Banggood: PART3 Updates for the full CodesBasically you need to connect the servo pins from pin 0 tp pin 2 if you want to read ultrasonic data through serial monitor. The digital pin 0 and 1 need to be blank for serial monitor.Also the L298N controlling pins we were using(5 to 10) has conflict with the servo.h library. The library explicily mentioned the pin 9 and 10 need to be blank. So we have to mov e the pins on 8,9,10 to 11,12,13.

Smart Car Motor Test

/* Arduino DC Motor L298N Module H-bridge DC Motor Test
By Chen The Design Maker

//Define all the connections maps to the L298N
#define enA 10
#define in1 9
#define in2 8
#define in3 7
#define in4 6
#define enB 5

class Motor{

int enablePin;
int directionPin1;
int directionPin2;

//Method to define the motor pins
Motor(int ENPin,int dPin1,int dPin2){
enablePin = ENPin;
directionPin1 = dPin1;
directionPin2 = dPin2;

//Method to drive the motor 0~255 driving forward. -1~-255 driving backward
Drive(int speed){
digitalWrite(directionPin1, LOW);
digitalWrite(directionPin2, HIGH);
digitalWrite(directionPin1, HIGH);
digitalWrite(directionPin2, LOW);
speed = - speed;
analogWrite(enablePin, speed);
Motor leftMotor = Motor(enA, in1, in2);
Motor rightMotor = Motor(enB, in3, in4);

void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Set initial direction and speed
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

void loop() {










Servo Test:

/* Sweep
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object

void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position

* HC-SR04 Demo
* Demonstration of the HC-SR04 Ultrasonic Sensor
* Date: August 3, 2016
* Description:
* Connect the ultrasonic sensor to the Arduino as per the
* hardware connections below. Run the sketch and open a serial
* monitor. The distance read from the sensor will be displayed
* in centimeters and inches.
* Hardware Connections:
* Arduino | HC-SR04
* -------------------
* 5V | VCC
* A0 | Trig
* A1 | Echo
* License:
* Public Domain

// Pins
const int TRIG_PIN = A0;
const int ECHO_PIN = A1;

// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;

void setup() {

// The Trigger pin will tell the sensor to range find
digitalWrite(TRIG_PIN, LOW);

// We'll use the serial monitor to view the sensor output

void loop() {

unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
float cm;
float inches;

// Hold the trigger pin high for at least 10 us
digitalWrite(TRIG_PIN, HIGH);
digitalWrite(TRIG_PIN, LOW);

// Wait for pulse on echo pin
while ( digitalRead(ECHO_PIN) == 0 );

// Measure how long the echo pin was held high (pulse width)
// Note: the micros() counter will overflow after ~70 min
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1);
t2 = micros();
pulse_width = t2 - t1;

// Calculate distance in centimeters and inches. The constants
// are found in the datasheet, and calculated from the assumed speed
//of sound in air at sea level (~340 m/s).
cm = pulse_width / 58.0;
inches = pulse_width / 148.0;

// Print out results
if ( pulse_width > MAX_DIST ) {
Serial.println("Out of range");
} else {
Serial.print(" cm \t");
Serial.println(" in");

// Wait at least 60ms before next measurement

Parts list:

Smart Robot Car kit:

M3 *10mm Standoffs:

Extra servo for pan and tilt camera mount:

Speed encoder kit(Will cover in later video):

3 in 1 Jumper cable set:

Let me know if you have any kit that need help with!

My Website:
My Instagram:
My Dribbble:
My Instructable:

DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!

DISCLAIMER: This post contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!

9 thoughts on “Arduino Smart Robot Car-Part 2”

  1. Chen,

    This was very helpful! The Banggood kit does not include any instructions and it is tough to figure out a lot of this. Your videos and diagrams are great to have!

    1. Thanks Richard! That’s exactly why I’m making these video to fill the blanks. Banggood is great for affordable kits but some of them could be a bad experience. I guess my super power is that I can track instructions from China where banggood is sourcing from. So let me know if you run into a kit like this in the future.

  2. thanks, man u helped a lot was so worried I couldn’t figure anything out but then u came to the rescue thanks once again

  3. Hello Chen, thank you very much for your nice and clear Youtube instruction films and Arduino test sketches. But ………. 😉 please be so kind to check the Smart Car Motor test sketch. I miss the “in2” definition to 8 and the compiler from Arduino IDE version 1.8.5 has lots off problems with the instructions on line 8. Regards Jan

    1. Hey Jan,
      Sorry there was some copying problem, for some weird reason it copied some html tag…I fixed it now and let me know if it’s working.


  4. Sooo, load your code into my UCTRONICS smart-robot car and is keeps failing with ‘redefinition of void loop()’ and ‘redefinition of void Setup()’. I’m totally new to this stuff however I do have coding experience, albeit many moons ago. I’m helping my grandsons get into this stuff. Interesting how all the how-to videos never focus on trouble-shooting. It appears that you can’t have multiple Void Loop() ? That seems odd when loops are so vital to programming overall.
    Thoughts welcome.

    1. Hi Robert,

      As far as I know a typical arduino code should have only one void Setup() and one void loop(). Arduino is in general a one thread device that executes one loop again and again(Could have advanced multi thread with interruptions). Let me know what happens if all the duplicated setup and loop are deleted.


Leave a Reply

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