0% found this document useful (0 votes)
15 views7 pages

Code and Connection

The document outlines the wiring and code for controlling an RC car using an Arduino, L298N motor driver, IR sensor, and Bluetooth module. It details the connections between components and provides a code implementation for motor control, obstacle detection, and Bluetooth communication. The car can move forward, backward, turn, and stop based on commands received via Bluetooth, while also responding to obstacles detected by the IR sensor.

Uploaded by

thangaregav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Code and Connection

The document outlines the wiring and code for controlling an RC car using an Arduino, L298N motor driver, IR sensor, and Bluetooth module. It details the connections between components and provides a code implementation for motor control, obstacle detection, and Bluetooth communication. The car can move forward, backward, turn, and stop based on commands received via Bluetooth, while also responding to obstacles detected by the IR sensor.

Uploaded by

thangaregav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Connections:

[Battery +12V] - L298N +12V

[Battery GND] ----| Arduino GND

| L298N GND

Arduino Pin 3 - L298N IN1

Arduino Pin 4 - L298N IN2

Arduino Pin 5 (PWM) - L298N ENA

Arduino Pin 6 - L298N IN3

Arduino Pin 7 - L298N IN4

Arduino Pin 9 (PWM) - L298N ENB

Left motors connected in parallel to L298N OUT1 & OUT2

Right motors connected in parallel to L298N OUT3 & OUT4

IR Sensor VCC - Arduino 5V

IR Sensor GND - Arduino GND

IR Sensor OUT - Arduino Pin 2

Bluetooth VCC - Arduino 5V

Bluetooth GND - Arduino GND

Bluetooth TX - Arduino Pin 11 (Arduino RX)

Bluetooth RX - Arduino Pin 10 (Arduino TX, with level shifter if needed)

Code:

#include <SoftwareSerial.h>
#define IR_SENSOR_PIN 2

// Motor driver pins

#define ENA 5 // PWM speed control left motors

#define IN1 3

#define IN2 4

#define ENB 9 // PWM speed control right motors

#define IN3 6

#define IN4 7

// Bluetooth pins

#define BT_RX 11 // Bluetooth TX pin connected to Arduino RX

#define BT_TX 10 // Bluetooth RX pin connected to Arduino TX

SoftwareSerial Bluetooth(BT_TX, BT_RX); // RX, TX

// Motor speed (0-255)

const int motorSpeed = 200;

void setup() {

// Initialize motor pins

pinMode(ENA, OUTPUT);

pinMode(ENB, OUTPUT);

pinMode(IN1, OUTPUT);

pinMode(IN2, OUTPUT);

pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

// IR Sensor input pin

pinMode(IR_SENSOR_PIN, INPUT);

// Start serial for debugging and bluetooth communication

Serial.begin(9600);

Bluetooth.begin(9600);

stopCar();

Serial.println("Bluetooth Controlled RC Car Ready.");

void loop() {

if (Bluetooth.available()) {

char command = Bluetooth.read();

Serial.print("Received Command: ");

Serial.println(command);

// Check for obstacle

bool obstacleDetected = digitalRead(IR_SENSOR_PIN) == LOW; //


Assuming LOW means obstacle

if (obstacleDetected) {

Serial.println("Obstacle detected! Stopping car.");

stopCar();

return; // Skip processing movement commands

}
switch (command) {

case 'F': // Forward

moveForward();

break;

case 'B': // Backward

moveBackward();

break;

case 'L': // Left Turn

turnLeft();

break;

case 'R': // Right Turn

turnRight();

break;

case 'S': // Stop

stopCar();

break;

default:

stopCar();

break;

} else {

// Also constantly check obstacle while idle or moving

bool obstacleDetected = digitalRead(IR_SENSOR_PIN) == LOW;

if (obstacleDetected) {

Serial.println("Obstacle detected! Stopping car.");

stopCar();
}

void moveForward() {

// Left motors forward

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

analogWrite(ENA, motorSpeed);

// Right motors forward

digitalWrite(IN3, HIGH);

digitalWrite(IN4, LOW);

analogWrite(ENB, motorSpeed);

Serial.println("Moving Forward");

void moveBackward() {

// Left motors backward

digitalWrite(IN1, LOW);

digitalWrite(IN2, HIGH);

analogWrite(ENA, motorSpeed);

// Right motors backward

digitalWrite(IN3, LOW);

digitalWrite(IN4, HIGH);
analogWrite(ENB, motorSpeed);

Serial.println("Moving Backward");

void turnLeft() {

// Left motors backward

digitalWrite(IN1, LOW);

digitalWrite(IN2, HIGH);

analogWrite(ENA, motorSpeed / 2);

// Right motors forward

digitalWrite(IN3, HIGH);

digitalWrite(IN4, LOW);

analogWrite(ENB, motorSpeed / 2);

Serial.println("Turning Left");

void turnRight() {

// Left motors forward

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

analogWrite(ENA, motorSpeed / 2);

// Right motors backward

digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);

analogWrite(ENB, motorSpeed / 2);

Serial.println("Turning Right");

void stopCar() {

analogWrite(ENA, 0);

analogWrite(ENB, 0);

digitalWrite(IN1, LOW);

digitalWrite(IN2, LOW);

digitalWrite(IN3, LOW);

digitalWrite(IN4, LOW);

Serial.println("Car Stopped");

You might also like