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.

Connections

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 chenthedesignmaker.com
*/

//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;
public:

//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){
if(speed>=0){
digitalWrite(directionPin1, LOW);
digitalWrite(directionPin2, HIGH);
}
else{
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() {

leftMotor.Drive(200);
rightMotor.Drive(200);

delay(500);

leftMotor.Drive(0);
rightMotor.Drive(0);

delay(250);

leftMotor.Drive(150);
rightMotor.Drive(-150);

delay(500);

leftMotor.Drive(0);
rightMotor.Drive(0);

delay(250);

}

Servo Test:

/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/

#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
* GND | GND
*
* 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
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);

// We'll use the serial monitor to view the sensor output
Serial.begin(9600);
}

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);
delayMicroseconds(10);
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);
Serial.print(" cm \t");
Serial.print(inches);
Serial.println(" in");
}

// Wait at least 60ms before next measurement
delay(60);
}

Parts list:

Smart Robot Car kit:

https://www.banggood.com/DIY-L298N-2WD-Ultrasonic-Smart-Tracking-Moteur-Robot-Car-Kit-For-Arduino-p-1155139.html?p=JH2514288496201309O3

M3 *10mm Standoffs:

https://www.banggood.com/50pcs-M3-10mm-Brass-Female-Threaded-Hex-Standoffs-Spacer-Nut-DIY-PCB-Parts-p-1007519.html?p=JH2514288496201309O3
Amazon: http://amzn.to/2Auwbzq

Extra servo for pan and tilt camera mount:

https://www.banggood.com/TowerPro-SG90-Mini-Gear-Micro-Servo-9g-For-RC-Airplane-Helicopter-p-1009914.html?p=JH2514288496201309O3
Amazon: http://amzn.to/2jO7BBE

Speed encoder kit(Will cover in later video):

https://www.banggood.com/HC-020K-Double-Speed-Measuring-Module-With-Speed-Encoder-Kit-p-970327.html?p=JH2514288496201309O3
Amazon: http://amzn.to/2kq7H6J

3 in 1 Jumper cable set:

https://www.banggood.com/3-IN-1-120pcs-10cm-Male-To-Female-Female-To-Female-Male-To-Male-Jumper-Cable-Dupont-Wire-For-Arduino-p-1054670.html?p=JH2514288496201309O3
Amazon: http://amzn.to/2D5QYuw

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

My Website: http://chenludesign.com
My Instagram:https://www.instagram.com/friedlc/
My Dribbble: https://dribbble.com/chenludesign
My Instructable: https://www.instructables.com/member/Friedlc/

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!

14 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.

      Thanks!

  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.

      Chen

  5. Hi Chen,

    I do not understand why you use the bridge between 12V and 5V on the L298N? If you feed the 6V from the batt-pack to the 12V of the L298N and connect the 5V from the L298N to the sensor shield…would that not be a better solution? 5V from the L298N is the output Voltage from the L298N voltage regulator. Why do you feed is with 6V?

  6. Thank you muchly Chen, I was wondering where the instructions are. You’ve made life a lot easier with your videos and info. keep up the good work. Don’t let your site/channel die.

Leave a Reply

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