Published on

Bluetooth Controlled Bot

Table of Contents

Assignment 1

As the world is moving towards Autonomous Vehicles obstacle detection is a basic requirement for the vehicle to move safely without bumping into objects. Obstacle Avoider is an intelligent device that can automatically sense the obstacle in front of it and avoid them by slowing down or turning in the other direction.

Problem Statement

  • The speed of the motor involved in rotating the wheel of the obstacle avoidance bot should be linearly dependent on the distance(between 0-100).
  • If distance is 0 then speed 0.
  • If distance is 100cm speed full.

(Hint: Search for the map() function on Google)

Assignment 2

IR controlled motors. As we have controlled the directions of motor using basic code. Now we want our motors to be controlled by a touchless switch.

Problem Statement

Motors should run in any one direction. If IR sensor detects your hand, the motor should change the direction. If it detects your hand again, it goes back to spinning in original direction and so on.

  • Assemble the chassis that is included in your kit
  • Make all necessary connections with the relevant electronic components
  • Program your bot!

Circuit connections for Bluetooth Module

  • VCC - Connected to 5V pin on Uno
  • GND - Connected to GND pin on Uno
  • Tx - Connected to Rx of Uno
  • Rx - Connected to Tx of Uno

In the demonstration, we shall be connecting the bluetooth module with Arduino. Here we are using SoftwareSerial library to setup pins 8 and 9 on Uno as RX and TX pins respectively.

Take a look at the below image.

Front view

front-view

Back view

back-view

Explanation

HC05 Bluetooth module:

  • VCC - Connected to 5V on UNO using Red jumper wire.
  • GND - Connected to GND on UNO using Black jumper wire.
  • TXD - Connected to 8 on UNO using any jumper wire.
  • RXD - Connected to 9 on UNO using any jumper wire.

Connections on the chassis

  • The module is preferred to be connected directly onto the breadboard since it eases up the space and can be connected easily to the Arduino.
  • The Arduino and breadboard are fixed onto the chassis to have proper stability of the bot, since the connections need to be intact as it moves around.

It should look something like this.

Circuit connections for L293D

The Motor Driver included in the kit is a mini shield, with the following pinout

Case 1: If the L293d model is as follows:

case1

In the demonstration, we shall be connecting

  • Right Motor: For the Right Motor, connect its top terminal to the MA2 of the motor driver and its bottom terminal to the MA1.
  • Left Motor: For the Left Motor, connect the top terminal to the MB2 of the motor driver and the bottom terminal to the MB1.
  • Power Connections: Connect the 9V power supply to the V+ and V- on motor driver.
  • Input Pins of Right Motor:
    • connect the A1 and A2 pins to pins 2 and 3 on Arduino.
  • Enable Pins: En A , En B go to 5V of Arduino.
  • Connect Vcc and GND to 5V and GND on Arduino respectively.
  • Input Pins of Left Motor:
    • B1 and B2 go to 4 and 5 pins on Arduino respectively.

Case 2: If the L293d model is as follows:

IR
  • Right Motor: For the Right Motor, connect its top terminal to the M1 of the motor driver and its bottom terminal to the M2.
  • Left Motor: For the Left Motor, connect the top terminal to the M3 of the motor driver and the bottom terminal to the M4.
  • Power Connections: Connect the positive terminal of 9V power supply to the 12V on motor driver. To the GND on the motor driver near the 12V, connect the negative terminal of the 9V battery. Also, connect the GND of the Arduino to the same GND on the motor driver, meaning, both the wires(black of battery and GND of Uno) to be connected to GND on motor driver.
  • Input Pins of Right Motor:
    • connect the In1 and In2 pins to pins 3 and 2 on Arduino.
  • Enable Pins: En A , En B go to 5V of Arduino.
  • Connect 5V pin to 5V on Arduino respectively.
  • Input Pins of Left Motor:
    • In3 and In4 go to 5 and 4 pins on Arduino respectively.

Case 3: If the L293d model is as follows:

case3
  • Right Motor: For the Right Motor, connect its top terminal to the MOTOR2 hole of the motor driver that is away from the voltage regulator IC and its bottom terminal to the MOTOR2 hole towards the voltage regulator IC.
  • Left Motor: For the Left Motor, connect the top terminal to the MOTOR1 hole of the motor driver that is towards the voltage regulator IC and its bottom terminal to the MOTOR1 hole away from the voltage regulator IC.
  • Power Connections: Connect the 9V power supply to the Vin+ on motor driver. Now connect both the negative terminal of the 9V battery and GND of the Arduino to the Vin- hole in the motor driver.
  • Input Pins of Right Motor:
    • connect the MC6 pin that is away from the voltage regulator IC to pin 3 on Arduino and MC6 pin that is towards the voltage regulator IC to pin 2 on Arduino.
  • Input Pins of Left Motor:
    • connect the MC5 pin that is away from the voltage regulator IC to pin 4 on Arduino and MC5 pin that is towards the voltage regulator IC to pin 5 on Arduino.

NOTE - The connections between the two IR sensors, ultrasonic sensor, the Arduino Uno and the breadboard have been removed to avoid confusion. You may keep the connections shown previously as it is.

The finished bot looks something like this: Top view

top-view

Front view

front-view

Right view

right-view

Left view

left-view

Schematics

TYPE-1

IR

TYPE-2

IR

TYPE-3

IR

Logic

Program your Bot

The idea here is to get the input from the app which we will use to drive the bot. So, first let us see the code and then the explaination:

Code:

#include<SoftwareSerial.h>
// this library is used to setup a dummy COM port on Arduino which allows for bluetooth
//communication

SoftwareSerial abc(8,9);
//Setting 8 and 9 pins on Arduino Uno as RX and TX pins instead of 0 and 1 pins

char val;
//8RX on arduino
//9TX on arduino

void setup() {
  Serial.begin(9600);//setting up Serial monitor
  abc.begin(9600);//setting up Serial communication with the App over bluetooth
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  //Setting up the direction pins on the motor driver as output pins
}

void loop() {
  while(abc.available()==0){}
  //to wait till there is any input from the App

  val=abc.read();
  //once the input is received, we gotta read it into a variable

  if(val=='F'){
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
  }//to move the bot forward

  else if(val=='B'){
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
  }//to move the bot backwards

  else if(val=='L'){
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  }//to turn bot left

  else if(val=='R'){
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
  }//to turn bot right

  else if(val=='S'){
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  }//to stop the bot

  Serial.println(val);
}

Code Explaination:

  • We are performing Serial communication between the app and the Aduino over bluetooth. Serial Communication means data transfer bit by bit. So, it is similar to the serial communication which we used to do earlier between Aduino and the laptop.
  • We used to connect a usb to Arduino whose other end would be connected to the laptop. So here, we use a COM port to do this. So, if u are using bluetooth communication also, we need a COM port. But, 0 and 1 pins on Arduino are RX and TX pins so, they interrupt the serial communication with the bluetooth app while uploading the code since, at the same time Arduino and laptop are also communicating serially.
  • That's why, we need to disconnect the jumper wires from the TX and RX pins from the bt module while uploading. Once this is done, u can connect back these jumper wires.
  • Inorder to prevent these issues, let us use another set of pins on Uno as RX and TX pins.
  • So, inorder to solve these issues, we are using SoftwareSerial library.
  • Pins 2,3,4 and 5 are direction pins which are set as output pins.
  • Now, if we select the following options in the app,
    • up arrow: sends 'F' to the serial monitor indicating forward motion. Until you leave the button, it will not change, i.e., the F will remain as it is and the bot keeps moving forward. On unclicking the top arrow, 'S' appears on the serial monitor and the bot stops.
    • down arrow: sends 'B' to the serial monitor indicating backward motion. Until you leave the button, it will not change, i.e., the B will remain as it is and the bot keeps moving backwards. On unclicking the bottom arrow, 'S' appears on the serial monitor and the bot stops.
    • left arrow: sends 'L' to the serial monitor indicating to turn left. Until you leave the button, it will not change, i.e., the L will remain as it is and the bot keeps turning towards left. On unclicking the left arrow, 'S' appears on the serial monitor and the bot stops.
    • right arrow: sends 'R' to the serial monitor indicating to turn right. Until you leave the button, it will not change, i.e., the R will remain as it is and the bot keeps turning towards right. On unclicking the right arrow, 'S' appears on the serial monitor and the bot stops.
    • If no arrow button is clicked, the bot won't move and Arduino sends 'S' to Serial Monitor.

We can observe the values sent to the Arduino from the app (value of the variable 'val') using Serial.println(val) command at the end of "void loop()".

IR

In the above picture, I used each button on the app once( clicked them and unclicked them one after the another) and we can see the result.

So, did it work?

  • If yes, then GG!
  • If not, then review your code and check for any loose connections. Where there is a line, there is a way! So hope for it to work the next time :)