Difference between revisions of "Arduino Bluetooth Controlled Multi-modal Robotic Car (Android and iOS Compatible)"

From ArduinoInfo
Jump to: navigation, search
Line 82: Line 82:
 
* To configure Motor B, Connect pin B-1A of the L9110 motor driver (or IN3 of L293N motor driver) to MB_dir pin on the Arduino and pin B-1B of the L9110 motor driver (or IN4 of L293N motor driver) to MB_spd pin on the Arduino which we have defined in the code. If Motor B moves in the desired front direction (i.e. in an anti-clockwise direction if the front of the motor wheel is facing you), then motor one is all set; else, interchange the red and black wires of the motor that go into Motor B slots of the motor driver.
 
* To configure Motor B, Connect pin B-1A of the L9110 motor driver (or IN3 of L293N motor driver) to MB_dir pin on the Arduino and pin B-1B of the L9110 motor driver (or IN4 of L293N motor driver) to MB_spd pin on the Arduino which we have defined in the code. If Motor B moves in the desired front direction (i.e. in an anti-clockwise direction if the front of the motor wheel is facing you), then motor one is all set; else, interchange the red and black wires of the motor that go into Motor B slots of the motor driver.
  
 +
After confirming the front direction (which also confirms the back direction), it is important to also test the left direction (which would also confirm the right direction).
 +
Upload this code to the Arduino. To turn the robot in the left direction, the left motor should rotate backwards (i.e. in a clockwise direction if the front of the motor wheel is facing you) while the right motor rotates forward (also in a clockwise direction if the front of the motor wheel is facing you). This is known as <b>Differential Steering</b><syntaxhighlight lang="cpp">const byte MA_dir = 4;  // Motor Control Pins Direction and PWM Speed
 +
const byte MB_dir = 7;
 +
const byte MA_spd = 5;
 +
const byte MB_spd = 6;
 +
 +
void setup()  /**************************** SETUP: RUNS ONCE *******************/
 +
{
 +
  pinMode(MA_dir, OUTPUT);
 +
  pinMode(MB_dir, OUTPUT);
 +
  pinMode(MA_spd, OUTPUT);
 +
  pinMode(MB_spd, OUTPUT);
 +
 +
}//--(end setup )---
 +
 +
void loop()  /******************** LOOP: RUNS CONSTANTLY *******************/
 +
{
 +
  left(); // move the wheel in the forward direction
 +
}
 +
 +
void left()
 +
{
 +
  digitalWrite(MA_dir, LOW);
 +
  digitalWrite(MB_dir, HIGH);
 +
  analogWrite(MA_spd, 80);
 +
  analogWrite(MB_spd, 80);
 +
}</syntaxhighlight>
 +
 
<b>NOTE:</b> The speed of the motor increases as the PWM (analogWrite) value reduces. The range is from 0 - 255 (fastest to slowest speed)
 
<b>NOTE:</b> The speed of the motor increases as the PWM (analogWrite) value reduces. The range is from 0 - 255 (fastest to slowest speed)
  

Revision as of 12:03, 26 September 2019

Introduction

The Arduino micro-controller is capable of running only one sketch at a time. Therefore, it is generally thought that the Arduino can only perform one task or achieve one objective per-sketch. This project shows the implementation of a multi-modal bluetooth controlled robotic car, capable of achieving different tasks and objectives, and is controlled with an Android or iOS mobile application. Everything happens in real-time, with little to no delay.

Specifications

The communication with the robot is made possible via Serial-over-Bluetooth protocol. A bluetooth module is connected to the Arduino compatible board, and the mobile phone which the application has been installed on can connect to the bluetooth module.

This robot has been built to have 5 modes:

  1. Line-follower Mode
  2. Object-follower Mode
  3. Light-follower Mode
  4. Ramble Mode
  5. Manual-control Mode

Hardware Components and Setup

  1. HM10 Bluetooth Module x 1
  2. Yourduino RoboRed Board x 1
  3. IR Edge Detection/Line Tracker Sensor x 1
  4. Photo-resistor x 1
  5. Buzzer x 1
  6. Ultrasonic sensor x 1
  7. L9110S Motor driver x 1
  8. Geared DC motors x 2
  9. Micro servo motor kit x 1
  10. Robot car chassis with 2 wheels x 1

Configuring the HM10 BLE 4 Module

The HM10 module was chosen for this project due to it's compatibility with the common BLE 4.0 (Bluetooth Low Energy) protocol, used on Apple Iphones as well as Android phones. Before the module can be used, the baudrate, device name, and preferred pin have to be set. Below are steps for setting up the HM10 Module using the RoboRED board:

  1. The schematic of Arduino shows that the RX and TX pins are connected to the USB Interface chip (on Arduino board as pin 0 and pin 1), which means that those pins can directly access the USB Interface chip itself. We generally use TX and RX pins for communication. Upload the empty sketch below to the RoboRED board in order to enable it serve as USB-TTL converter for communicating with the HM10 module:
    void setup() {
    }
    
    void loop() {
    }
    
  2. Disconnect the Arduino USB connection
  3. Connect HM10-Tx --> RoboRED-Tx(Pin1) and HM10-Rx --> RoboRED-Rx(Pin0) (i.e. Tx --> Tx and Rx --> Rx of each board), then HM10-Vcc --> RoboRED-5V line (any of the pins with a red base) on the RoboRED and HM10-GND --> RoboRED-GND (any of the pins with a blue base)
  4. Plug in the RoboRED's USB to the PC and open the Arduino IDE
  5. Open the Serial Monitor and set the baudrate to 9600
  6. Type AT and press enter. If the response is OK, then the module is ready to be configured using AT commands
  7. Type AT+VERR? command to check the firmware version that runs on your HM10 module. V5** and above is good
  8. Type AT+NAME? to see current name of the module, otherwise, type AT+NAMEpreferredName to set a preferred Bluetooth name for the module. Kindly note that there should be NO space between the AT+NAME and the preferredName chosen
  9. Next, set the passcode that would be required for connecting to the module; AT+PASSpreferredPasscode. The preferred passcode must be 6 numbers. To check the passcode, use AT+PASS?
  10. The HM10 module is configured to connect without passcode (i.e. AT+TYPE0). Set it to request a passcode on pairing by using this command: AT+TYPE2

The module is now configured and ready to be used. To save your settings, use AT+RESET in other to save and reset the module

Setting up the Edge Detection/Line Tracker Sensor

This Sensor is really a module which has an IR receiver and transmitter. IR Sensors work by using a specific light sensor to detect a select light wavelength in the Infra-Red (IR) spectrum. By using an LED which produces light at the same wavelength as what the sensor is looking for, you can look at the intensity of the received reflected light. When an object is close to the sensor, the light from the LED bounces (reflects) off the object and into the light sensor. This results in a voltage being sensed on the output pin of the module. As long as the sensor receives the reflected IR light, it sends a HIGH from the output of the IR module. When the Robot gets to an edge where the IR module no longer receives the reflected IR light, it sends a LOW from the output of the module. This is the principle used for edge detection. For line tracking, a beam of infrared light is transmitted from the IR transmitter of the module onto the object, then the receiver measures how much light is reflected back. A dark object bounces off less light than a bright object (i.e. dark objects absorb more light than bright objects). This is the fundamental principle behind a line tracking robot. Hence, in a line tracking mode, the IR sensor detects the difference between white (a surface) and black (a line). Since black reflects little or no light, the module sends a LOW signal off its output, and a HIGH output signal when it detects white. This is then used to adjust the speed of the motors to track a black line.


Setting up the Motor and Motor Driver

In order to get the desired functionality of the motors and motor drivers, it is necessary to know what pins of the motor drivers control what direction. This is a tricky, but easy process and the steps are outlined:

  • First, start with one motor (Motor A). Connect pin A-1A of the L9110 motor driver (or IN1 of L293N motor driver) to MA_dir pin on the Arduino and pin A-1B of the L9110 motor driver (or IN2 of L293N motor driver) to MA_spd pin on the Arduino which we have defined in the code.
    const byte MA_dir = 4;  // Motor Control Pins Direction and PWM Speed
    const byte MB_dir = 7;
    const byte MA_spd = 5;
    const byte MB_spd = 6;
    
    void setup()   /**************************** SETUP: RUNS ONCE *******************/
    {
      pinMode(MA_dir, OUTPUT);
      pinMode(MB_dir, OUTPUT);
      pinMode(MA_spd, OUTPUT);
      pinMode(MB_spd, OUTPUT);
    
    }//--(end setup )---
    
    void loop()   /******************** LOOP: RUNS CONSTANTLY *******************/
    {
      front(); // move the wheel in the forward direction
    }
    
    void front()
    {
      digitalWrite(MA_dir, HIGH);
      digitalWrite(MB_dir, HIGH);
      analogWrite(MA_spd, 0);
      analogWrite(MB_spd, 80);
    }
    
  • If Motor A moves in the desired front direction (i.e. in a clockwise direction if the front of the motor wheel is facing you), then motor one is all set; else, interchange the red and black wires of the motor that go into Motor A slots of the motor driver.
  • To configure Motor B, Connect pin B-1A of the L9110 motor driver (or IN3 of L293N motor driver) to MB_dir pin on the Arduino and pin B-1B of the L9110 motor driver (or IN4 of L293N motor driver) to MB_spd pin on the Arduino which we have defined in the code. If Motor B moves in the desired front direction (i.e. in an anti-clockwise direction if the front of the motor wheel is facing you), then motor one is all set; else, interchange the red and black wires of the motor that go into Motor B slots of the motor driver.

After confirming the front direction (which also confirms the back direction), it is important to also test the left direction (which would also confirm the right direction).

Upload this code to the Arduino. To turn the robot in the left direction, the left motor should rotate backwards (i.e. in a clockwise direction if the front of the motor wheel is facing you) while the right motor rotates forward (also in a clockwise direction if the front of the motor wheel is facing you). This is known as Differential Steering
const byte MA_dir = 4;  // Motor Control Pins Direction and PWM Speed
const byte MB_dir = 7;
const byte MA_spd = 5;
const byte MB_spd = 6;

void setup()   /**************************** SETUP: RUNS ONCE *******************/
{
  pinMode(MA_dir, OUTPUT);
  pinMode(MB_dir, OUTPUT);
  pinMode(MA_spd, OUTPUT);
  pinMode(MB_spd, OUTPUT);

}//--(end setup )---

void loop()   /******************** LOOP: RUNS CONSTANTLY *******************/
{
  left(); // move the wheel in the forward direction
}

void left()
{
  digitalWrite(MA_dir, LOW);
  digitalWrite(MB_dir, HIGH);
  analogWrite(MA_spd, 80);
  analogWrite(MB_spd, 80);
}

NOTE: The speed of the motor increases as the PWM (analogWrite) value reduces. The range is from 0 - 255 (fastest to slowest speed)

Setting up the Servo

It is necessary to determine the actual angle of rotation of the servo motor each direction (left, right, and front) represents. Upload the code below to the Arduino and change the servo.write(<angle>) angle till the desired values for left, right and front positions of the servo which controls the ultrasonic sensor is achieved. The datatype of the variable should be a byte.

#include <Servo.h>      //Import the Servo library

Servo servo;          //create servo object to control a servo, max = 8 servos
byte servoPin = 11;

void setup()   /**************************** SETUP: RUNS ONCE *******************/
{
  pinMode(servoPin, OUTPUT);
  servo.attach(servoPin);
  servo.write(95); //change this value till you achieve the desired value for left, front, and right directions 

}//--(end setup )---

void loop()   /******************** LOOP: RUNS CONSTANTLY *******************/
{

}

Testing the Ultrasonic Range Sensor

To test if the ultrasonic range sensor has no faults, upload the code below into the Arduino and open the Serial Monitor at baudrate 9600 to see if you get correct values. Try to place an object in front of the sensor and vary the distance to see if there is a corresponding change in the values displayed on the Serial Monitor.

/*-----( Import needed library )-----*/
#include <SimpleKalmanFilter.h>


/*-----( Declare Constants and Pin Numbers )-----*/
//ultrasonic sensor parameters
const byte trigPin = 9;
const byte echoPin = 8;

SimpleKalmanFilter simpleKalmanFilter(2, 2, 0.01);        //create an object for the simpleKalmanFilter library

float dist = 0.00;
float estimated_value = 0.00;


void setup()   /**************************** SETUP: RUNS ONCE *******************/
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);

}//--(end setup )---


void loop()   /******************** LOOP: RUNS CONSTANTLY *******************/
{
  Serial.println(obstacle());
}


float obstacle()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(12);
  digitalWrite(trigPin, LOW);
  dist = pulseIn(echoPin, HIGH);
  delayMicroseconds(10000);
  dist = dist / 58;
  
  //smoothing of ultrasonic sensor readings
  estimated_value = simpleKalmanFilter.updateEstimate(dist);

  return estimated_value;

}//END obstacle

Testing the Light Dependent Resistor (LDR)

This sketch is use to confirm that the ultrasonic range sensor is working fine. Use a torch light and vary its distance from the LDR to see the different readings on the Serial Monitor. This can also be used to set a desired lightThreshold in the main code.
const byte ldrPin = A4;

void setup()   /**************************** SETUP: RUNS ONCE *******************/
{
  pinMode(ldrPin, INPUT);

  Serial.begin(9600);

}//--(end setup )---

void loop()   /******************** LOOP: RUNS CONSTANTLY *******************/
{
  // display light  intensity values on the Serial Monitor 
  Serial.println(analogRead(ldrPin));
}

Using the Mobile Application

In order to control the robot car, a Mobile App has been designed with bluetooth capabilities to be able able to interface with the car via its HM10 BLE 4.0 module. BLE 4.0 protocol was chosen as it is compatible with both Android and iOS devices, and it consumes low energy.