Arduino with L298N and Bluetooth

Contorl 2 DC motors via Bluetooth

Arduino with L298N and Bluetooth

This project is about controlling 2 DC motors using the L298N dual H Bridge / motor driver. The L298N is controlled using an Arduino UNO and a Bluetooth module like BlueSMiRF or HC-05/06.

Materials needed:
- L298N dual H Bridge / motor driver
- Arduino UNO (or similar) + USB cable for it
- BlueSMiRF module or similar
- DC adapter for Arduino
- wires
- Android phone with RoboRemo app installed

Introduction

The 2 classical projects with Bluetooth and 2 DC motors are RC car, and RC tank.
RC car uses one motor to move forward / backward, and another motor for steering (left / right).
The RC tank, however, uses both motors for moving. One moves the left track, the other moves the right track, so the tank can turn in place.

Steps:

1.
Upload this code to your Arduino:

// Control 2 DC motors (with L298N) via Bluetooth
// using RoboRemo app
// www.roboremo.com

// Hardware setup:
// BT module   Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1


// L298N   Arduino pin
int IN_1A = 12;
int EN_1  = 11;
int IN_1B = 10;
int IN_2A = 4;
int EN_2  = 3;
int IN_2B = 2;


char cmd[100];
int cmdIndex;

int m1speed=0, m2speed=0;
int m1speedOld, m2speedOld;


boolean cmdStartsWith(const char *st) {
  for(int i=0; ; i++) {
    if(st[i]==0) return true;
    if(cmd[i]==0) return false;
    if(cmd[i]!=st[i]) return false;;
  }
  return false;
}


void onConnectionLost() {
  
  // stop motors
  
  analogWrite(EN_1, 0);
  digitalWrite(IN_1A, LOW);
  digitalWrite(IN_1B, LOW);
  analogWrite(EN_1, 255);

  analogWrite(EN_2, 0);
  digitalWrite(IN_2A, LOW);
  digitalWrite(IN_2B, LOW);
  analogWrite(EN_2, 255);
}

unsigned long lastCmdTime = 0;

void exeCmd() {

  lastCmdTime = millis();

  if(cmdStartsWith("m1 ") ) {
    m1speedOld = m1speed;
    m1speed = atoi(cmd+3);
    if(m1speed>0) {
      if(m1speedOld<0) {
        analogWrite(EN_1, 0);
      }
      // forward
      digitalWrite(IN_1A, HIGH);
      digitalWrite(IN_1B, LOW);
      analogWrite(EN_1, m1speed);
    }
    if(m1speed<0) {
      if(m1speedOld>0) {
        analogWrite(EN_1, 0);
      }
      // backward
      digitalWrite(IN_1A, LOW);
      digitalWrite(IN_1B, HIGH);
      analogWrite(EN_1, -m1speed);
    }
    if(m1speed==0) {
      analogWrite(EN_1, 0);
    }
  }


  if(cmdStartsWith("m2 ") ) {
    m2speedOld = m2speed;
    m2speed = atoi(cmd+3);
    if(m2speed>0) {
      if(m2speedOld<0) {
        analogWrite(EN_2, 0);
      }
      // forward
      digitalWrite(IN_2A, HIGH);
      digitalWrite(IN_2B, LOW);
      analogWrite(EN_2, m2speed);
    }
    if(m2speed<0) {
      if(m2speedOld>0) {
        analogWrite(EN_2, 0);
      }
      // backward
      digitalWrite(IN_2A, LOW);
      digitalWrite(IN_2B, HIGH);
      analogWrite(EN_2, -m2speed);
    }
    if(m2speed==0) {
      analogWrite(EN_2, 0);
    }
  }
}

void setup() {
  
  delay(500); // wait for bluetooth module to start

  Serial.begin(115200);
  // My Bluetooth default baud is 115200
  // If your Bluetooth uses other baud rate,
  // then you must modify it here,
  // otherwise it will not work.
  // for HC-05/06 it is usually 9600
  
  pinMode(EN_1, OUTPUT);
  analogWrite(EN_1, 0);
  pinMode(EN_2, OUTPUT);
  analogWrite(EN_1, 0);

  pinMode(IN_1A, OUTPUT);
  pinMode(IN_2A, OUTPUT);
  pinMode(IN_1B, OUTPUT);
  pinMode(IN_2B, OUTPUT);
  
  cmdIndex = 0;
}


void loop() {

  // if nothing received for 500ms
  if( millis() - lastCmdTime > 500) {
    onConnectionLost();
  }
  
  if(Serial.available()) {
    
    char c = (char)Serial.read();
    
    if(c=='\n') {
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {      
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
    } 
  }
}

2.
Connect the L298N and Bluetooth module to the Arduino according to the schematic.
The control of the L298N input pins is described in the table below:

l298n-inputs.png

3.
Power the Arduino with the DC adapter.

4.
Download RoboRemo interface file.

L298N car.interface

L298N tank.interface

Copy the interface file to your Android device, then import in RoboRemo using menu -> interface -> import.

5.
Open RoboRemo, connect via Bluetooth and enjoy :)

6.
Make YouTube video about your DIY RC car / RC tank.