RC Car using L298N, HC-06, and Arduino Uno

Introduction

With this article, we will create a remote control (RC) car with the use of HC-06 bluetooth module to make it wireless and L298N motor driver to control speed and direction of rotations of the motors.

Bill Of Materials

  1. Arduino Uno as the main board
  2. HC-06 bluetooth module to receive commands via bluetooth communication protocol.
  3. L298N motor driver module
  4. DC motors to move the RC car.

Video Demonstration

Source Code

  1#include "SoftwareSerial.h"
  2
  3#define IN3Br 2
  4#define IN4Br 4
  5#define PWMBr 3
  6
  7#define IN1Al 7
  8#define IN2Al 5
  9#define PWMAl 6
 10
 11#define HC_06_Rx 12
 12#define HC_06_Tx 11
 13
 14#define NORMAL_SPEED 60
 15#define TURNS_SPEED 10
 16
 17SoftwareSerial swSerial(HC_06_Rx,HC_06_Tx);
 18String cmd="";
 19bool isAlEnabled = false;
 20bool isBrEnabled = false;
 21byte PWMAl_val = 100;
 22byte PWMBr_val = 100;
 23
 24void setup() {
 25  // put your setup code here, to run once:
 26  Serial.begin(9600);
 27  swSerial.begin(9600);
 28
 29  pinMode(IN3Br, OUTPUT);
 30  pinMode(IN4Br, OUTPUT);
 31  pinMode(PWMBr, OUTPUT);
 32
 33  pinMode(IN1Al, OUTPUT);
 34  pinMode(IN2Al, OUTPUT);
 35  pinMode(PWMAl, OUTPUT);
 36
 37  digitalWrite(IN3Br, LOW);
 38  digitalWrite(IN4Br, LOW);
 39  digitalWrite(PWMBr, LOW);
 40
 41  digitalWrite(IN1Al, LOW);
 42  digitalWrite(IN2Al, LOW);
 43  digitalWrite(PWMAl, LOW);
 44
 45  swSerial.println("Setup Complete");
 46}
 47
 48void loop() {
 49  // put your main code here, to run repeatedly:
 50  checkSerial();
 51}
 52
 53void checkSerial() {
 54 //Read data from Serial
 55 while(Serial.available()>0){
 56   cmd+=(char)Serial.read();
 57 }
 58
 59 while(swSerial.available()>0){
 60   cmd+=(char)swSerial.read();
 61 }
 62 
 63 //Select function with cmd
 64 if(cmd!=""){
 65    cmd.trim();  // Remove added LF in transmit
 66    // We expect Command from bluetooth
 67    if (cmd.equals("S")) {
 68      swSerial.println("Braking");
 69      brakeVehicle();
 70    } else if(cmd.equals("R")) {
 71       swSerial.println("Right turn");
 72       turnVehicleRght();
 73    }else if (cmd.equals("L")){
 74       swSerial.println("Left turn");
 75       turnVehicleLeft();
 76    }else if(cmd.equals("F")){
 77       swSerial.println("Moving forward");   
 78       moveVehicleForward();
 79    }else if(cmd.equals("B")){
 80       swSerial.println("Moving backward");
 81       moveVehicleBackward();
 82    }
 83    cmd=""; //reset cmd
 84 }
 85}
 86
 87void brakeVehicle () {
 88  digitalWrite(PWMBr, LOW);
 89  digitalWrite(PWMAl, LOW);
 90}
 91
 92void turnVehicleRght() {
 93  analogWrite(PWMBr, TURNS_SPEED);
 94  analogWrite(PWMAl, NORMAL_SPEED);
 95}
 96
 97void turnVehicleLeft() {
 98  analogWrite(PWMAl, TURNS_SPEED);
 99  analogWrite(PWMBr, NORMAL_SPEED);
100}
101
102void moveVehicleForward() {
103  digitalWrite(IN3Br, HIGH);
104  digitalWrite(IN4Br, LOW);
105  analogWrite(PWMBr, NORMAL_SPEED);
106
107  digitalWrite(IN1Al, HIGH);
108  digitalWrite(IN2Al, LOW);
109  analogWrite(PWMAl, NORMAL_SPEED);
110}
111
112void moveVehicleBackward() {
113  digitalWrite(IN3Br, LOW);
114  digitalWrite(IN4Br, HIGH);
115  analogWrite(PWMBr, NORMAL_SPEED);
116
117  digitalWrite(IN1Al, LOW);
118  digitalWrite(IN2Al, HIGH);
119  analogWrite(PWMAl, NORMAL_SPEED);
120}

Call To Action

If you found this tutorial helpful, please consider supporting me by sharing this to your friends. You may leave your comments and suggestions in the comment box.

Thank you and have a good day.



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.