Smart Car
Arduino Smart Car
Arduino Smart Car
Components
Components
Arduino Uno Board
Sensor Shield
Servo Motor
DC Motors
L298N H-Bridge
Ultrasonic Sensor
Line Tracking Sensor
Jumper Wires
Lithium Battery
Battery Pack
Chassis
Wheels
Assembly Instructions
Assembly Instructions
Instructions_Comments.pdf
Wiring the Robot
Wiring the Robot
robot-car-instructions.pdf
Programming the Robot
Programming the Robot
Moving the Servo Motor
Moving the Servo Motor
//Setup for Servo Motor
#include <Servo.h>
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(11);
}
void loop() {
myservo.write(90);
delay(500);
myservo.write(0);
delay(1000);
myservo.write(180);
delay(1000);
}
Using the Ultrasonic Sensor
Using the Ultrasonic Sensor
//Setup for Ultrasonic sensor
#include <SR04.h>
#define trig_pin 13
#define echo_pin 12
SR04 sr04 = SR04(echo_pin,trig_pin);
int pos = 0;
int leftDis = 0;
int rightDis = 0;
int forward = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
myservo.write(80);
delay(500);
forward = sr04.Distance();
Serial.println(forward);
myservo.write(0);
delay(1000);
rightDis = sr04.Distance();
Serial.println(leftDis);
delay(1000);
myservo.write(180);
delay(1000);
leftDis = sr04.Distance();
Serial.println(rightDis);
delay(1000);
}
Moving the Wheels
Moving the Wheels
//Setup for Wheel Motors
#define enA 6
#define in1 7
#define in2 5
#define enB 3
#define in3 4
#define in4 2
void go() {
analogWrite(enA, 175);
analogWrite(enB, 175);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void left() {
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void right() {
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void stopp() {
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
}
void setup() {
Serial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
go();
delay(1000);
stopp();
delay(1000);
}
Moving the Wheels Simplified
Moving the Wheels Simplified
//Setup for Wheel Motors
#define enA 6
#define in1 7
#define in2 5
#define enB 3
#define in3 4
#define in4 2
void go(int t) {
analogWrite(enA, 175);
analogWrite(enB, 175);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(t);
stopp(250);
}
void left(int t) {
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(t);
stopp(250);
}
void right(int t) {
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(t);
stopp(250);
}
void stopp(int t) {
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
delay(t);
}
void setup() {
Serial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
go(1000);
right(500);
}
Stationary Robot
Stationary Robot
//Ultrasonic Setup
#include <SR04.h>
#define echo_pin 12
#define trig_pin 13
SR04 sr04 = SR04(echo_pin,trig_pin);
int distance = 0;
int rightDis = 0;
int leftDis = 0;
//Setup for Servo Motor
#include <Servo.h>
Servo myservo;
//Setup for Wheel Motors
#define enA 6
#define in1 7
#define in2 5
#define enB 3
#define in3 4
#define in4 2
void go() {
analogWrite(enA, 175);
analogWrite(enB, 175);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void left() {
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void right() {
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void stopp() {
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
}
void setup() {
Serial.begin(9600);
myservo.attach(11);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
myservo.write(90);
distance = sr04.Distance();
if (distance < 50)
{
Serial.println("Stop");
myservo.write(0);
delay(1000);
rightDis = sr04.Distance();
Serial.print("Right: ");
Serial.println(rightDis);
else
{
Serial.println("Go");
}
delay(500);
}
Obstacle Avoidance
Obstacle Avoidance
//Setup for Ultrasonic sensor
#include <SR04.h>
#define trig_pin 13
#define echo_pin 12
SR04 sr04 = SR04(echo_pin,trig_pin);
//Setup for Servo Motor
#include <Servo.h>
Servo myservo;
int leftDis = 0;
int rightDis = 0;
int forwardDis = 0;
//Setup for Wheel Motors
#define enA 6
#define in1 7
#define in2 5
#define enB 3
#define in3 4
#define in4 2
void go() { //Forward Movement
analogWrite(enA, 175);
analogWrite(enB, 175);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void back() { //Reverse Movement
analogWrite(enA, 175);
analogWrite(enB, 175);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void left() { //Turning Left
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void right() { //Turning Right
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void stopp() { //Stops robot
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
}
void setup() {
Serial.begin(9600);
myservo.attach(11);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
myservo.write(80);
delay(500);
forwardDis = sr04.Distance();
Serial.println(forwardDis);
if (forwardDis < 75) { //Object is in front of robot
stopp(); //Stops robot movement
delay(250);
back(); //backs away from object slightly
delay(500);
stopp();
delay(1000);
myservo.write(0); //Moves US Sensor to the right
delay(1000);
rightDis = sr04.Distance(); //Measures Distance
Serial.println(leftDis); //Optional print to Serial Monitor
delay(1000);
myservo.write(180); //Moves US Sensor to the left
delay(1000);
leftDis = sr04.Distance(); //Measures Distance
Serial.println(rightDis); //Optional print to Serial Monitor
delay(1000);
if(rightDis > leftDis){ //Path is clearest to the right
right(); //turns robot to the right 90ish degrees
delay(15);
}
else if(rightDis < leftDis) { //Path clearest to the left
left(); //turns robot to the left 90ish degrees
delay(15);
}
myservo.write(80); //Moves US Sensor back to the forward position
delay(250);
}
else{ //While there is not objects in the way
go(); //Moves robot forward
}
}
Bluetooth
Bluetooth
unsigned char Bluetooth_val; //defining variable val
#define Lpwm_pin 6 //adjusting speed
#define Rpwm_pin 3 //adjusting speed //
int pinLB=7; // defining pin2 left rear
int pinLF=5; // defining pin4 left front
int pinRB=4; // defining pin7 right rear
int pinRF=2; // defining pin8 right front
unsigned char Lpwm_val = 255;
unsigned char Rpwm_val = 255;
int Car_state=0;
void M_Control_IO_config(void)
{
pinMode(pinLB,OUTPUT); // IN1
pinMode(pinLF,OUTPUT); // IN2
pinMode(pinRB,OUTPUT); // IN3
pinMode(pinRF,OUTPUT); // IN4
pinMode(Lpwm_pin,OUTPUT); // pin 6 (PWM)
pinMode(Rpwm_pin,OUTPUT); // pin 3 (PWM)
}
void Set_Speed(unsigned char Left,unsigned char Right)
{
analogWrite(Lpwm_pin,Left);
analogWrite(Rpwm_pin,Right);
}
void advance() // going forward
{
Set_Speed(200,200);
digitalWrite(pinRB,HIGH); // making motor move towards right rear
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,HIGH); // making motor move towards left rear
digitalWrite(pinLF,LOW);
Car_state = 1;
}
void turnR() //turning right(dual wheel)
{
Set_Speed(150,150);
digitalWrite(pinRB,HIGH); //making motor move towards right rear
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,LOW);
digitalWrite(pinLF,HIGH); //making motor move towards left front
Car_state = 4;
}
void turnL() //turning left(dual wheel)
{
Set_Speed(150,150);
digitalWrite(pinRB,LOW);
digitalWrite(pinRF,HIGH ); //making motor move towards right front
digitalWrite(pinLB,HIGH); //making motor move towards left rear
digitalWrite(pinLF,LOW);
Car_state = 3;
}
void stopp() //stop
{
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,HIGH);
Car_state = 5;
}
void back() //back up
{
Set_Speed(200,200);
digitalWrite(pinRB,LOW); //making motor move towards right rear
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,LOW); //making motor move towards left rear
digitalWrite(pinLF,HIGH);
Car_state = 2;
}
void setup()
{
M_Control_IO_config();
Set_Speed(Lpwm_val,Rpwm_val);
Serial.begin(9600); //initialized serial port , using Bluetooth as serial port, setting baud at 9600
stopp();
}
void loop()
{
if(Serial.available()) //to judge whether the serial port receives the data.
{
Bluetooth_val=Serial.read();
Serial.print(Bluetooth_val);//reading (Bluetooth) data of serial port,giving the value of val;
switch(Bluetooth_val)
{
case 'U':advance(); //UP
break;
case 'D': back(); //back
break;
case 'L':turnL(); //Left
break;
case 'R':turnR(); //Right
break;
case 'S':stopp(); //stop
break;
case 'I':stopp(); //stop
break;
}
}
}
Learn More
Learn More