0% found this document useful (0 votes)
129 views

Project 1 Arduino Smart Car

This project involves building an Arduino smart car equipped with an ultrasonic sensor and Bluetooth module. The car can be controlled remotely via Bluetooth to move forward, backward, left, right, or stop. The ultrasonic sensor allows the car to detect obstacles and measure distances, enabling autonomous navigation experiments. Code is provided to control the car motors and receive sensor data via Bluetooth to build an interactive self-driving car.

Uploaded by

Knews Skopje
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Project 1 Arduino Smart Car

This project involves building an Arduino smart car equipped with an ultrasonic sensor and Bluetooth module. The car can be controlled remotely via Bluetooth to move forward, backward, left, right, or stop. The ultrasonic sensor allows the car to detect obstacles and measure distances, enabling autonomous navigation experiments. Code is provided to control the car motors and receive sensor data via Bluetooth to build an interactive self-driving car.

Uploaded by

Knews Skopje
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Project 1

(Arduino Smart Car)

An Arduino car equipped with a Bluetooth module and an ultrasonic sensor


offers an exciting blend of technology and functionality. The Bluetooth
module allows wireless communication between the car and a controlling
device, such as a smartphone. The ultrasonic sensor enhances the car's
capabilities by enabling it to detect obstacles and measure distances
accurately. With this setup, users can control the Arduino car remotely and
receive real-time data from the ultrasonic sensor, making it an excellent
platform for autonomous navigation experiments and interactive projects.
This combination opens up a world of possibilities for exploring robotics,
automation.

Required components

Microcontroller: Arduino UNO


Motor driver: L298N

Bluetooth module: HC-05

Servo motor: SG90


Ultrasonic sensor: HC-SR04

4 DC morots

Electric circuit for HC-SR04 ultrasonic sensor and servo motor


Electric circuit for HC-05 bluetooth module

Code

Code for HC-SR04 ultrasonic sensor


#include <SoftwareSerial.h>

// Pin declarations for the L298N motor driver


const int ENA = 5; // PWM signal for speed control of left motor
const int IN1 = 7; // control signal for direction of left motor
const int IN2 = 8; // control signal for direction of left motor
const int ENB = 6; // PWM signal for speed control of right motor
const int IN3 = 9; // control signal for direction of right motor
const int IN4 = 10; // control signal for direction of right motor

// Pin declaration for the HC-05 Bluetooth module


const int bluetoothTx = 0;
const int bluetoothRx = 1;

// Bluetooth serial object


SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
// Set up the serial communication for debugging purposes
Serial.begin(9600);

// Set up the L298N motor driver pins as outputs


pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

// Set the Bluetooth module baud rate to 9600


bluetooth.begin(9600);
}

void loop() {
// Read the data sent by the Bluetooth module
if (bluetooth.available() > 0) {
char command = bluetooth.read();

// Process the command received from the Bluetooth module


switch (command) {
case 'F': // Move forward
forward();
break;
case 'B': // Move backward
backward();
break;
case 'L': // Turn left
left();
break;
case 'R': // Turn right
right();
break;
case 'S': // Stop
stop();
break;
default:
break;
}
}
}

// Move the car forward


void forward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA, 255);
analogWrite(ENB, 255);
}

// Move the car backward


void backward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, 255);
analogWrite(ENB, 255);
}

// Turn the car left


void left() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA, 200);
analogWrite(ENB, 200);
}

// Turn the car right


void right() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, 200);
analogWrite(ENB, 200);
}

// Stop the car


void stop() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

Code for HC-05 bluetooth module


#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo

int Echo = A0;


int Trig = A1;

#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 10
#define carSpeed 142
int rightDistance = 0, leftDistance = 0, middleDistance = 0;

void forward(){
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("Forward");
}

void back() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("Back");
}

void left() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("Left");
}

void right() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("Right");
}

void stop() {
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
Serial.println("Stop!");
}

//Ultrasonic distance measurement Sub function


int Distance_test() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance / 58;
return (int)Fdistance;
}

void setup() {
myservo.attach(3); // attach servo on pin 3 to servo object
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
stop();
}

void loop() {
myservo.write(90); //setservo position according to scaled value
delay(500);
middleDistance = Distance_test();

if(middleDistance <= 39) {


stop();
delay(500);
myservo.write(10);
delay(1000);
rightDistance = Distance_test();

delay(500);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
leftDistance = Distance_test();

delay(500);
myservo.write(90);
delay(1000);
if(rightDistance > leftDistance) {
right();
delay(360);
}
else if(rightDistance < leftDistance) {
left();
delay(360);
}
else if((rightDistance <= 20) || (leftDistance <= 20)) {
back();
delay(180);
}
else {
forward();
}
}
else {
forward();
}
}

Final Result

You might also like