bclose

Controlling a 4×4 Rover via Bluetooth

Objectives

 

 

    • We keep on playing with DC motors and the Adafruit Motor Shield V1.
    • We will control the Rover with our smartphone or tablet via bluetooth
    • We will program the basic movements of the rover.
    • We will use an Android app to control the Rover via bluetooth.

 

 

Bill of materials

Arduino Mega 2560 Arduino MEGA or equivalent.
Motor shield  An Adafruit  Motor Shield V1.
  Rover chasis
rover chassis with 4 motors.
Battery holder A battery holder. 

Bluetooth module
A HC-05 bluetooth module  or similar.
Voltage converter Voltage converter from 5V to 3.3V.
Power bank A 2200 mAh Powerbank.
 

Introduction

 

We have devoted several chapters in our website to the 4×4 Rover, specifically the  chapters:

And since we spend all day hooked to the mobile phones and that nowadays the trend is to use them for everything, we think we must devote a tutorial to handle the rover directly via our smartphone or  tablet.

In previous chapters we have seen the foundations of DC motors and how to use a Motor Shield to handle multiple motors simultaneously. We have also discussed how to control the rotation of a four-wheel drive rover by applying different speeds to the wheels.

The whole theory is already in the previous tutorials, so if you have not seen them yet or you don’t remember them very well, you can review them and refresh concepts.

The only things we will have to add are a small bluetooth module, in this case the HC-05, a  voltage converter from 5V to 3.3V for the module and a powerbank, like the one we use to charge the mobile phone, with which we will power our Arduino Mega.

Let’s see the wiring diagram.

 

 

Wiring diagram

 

The wiring of the circuit is really simple, we just need to stick the Motor Shield v1 into the Arduino Mega (look for the pin 0 of the Motor Shield to match the RX0 of the Arduino Mega and that the A5 of the Motor Shield matches the A5 of the Arduino Mega ), connect the wires for data transmission and reception, RX and TX, and the bluetooth module power.

The Fritzing schematic of the Adafruit v1 Motor Shield it is not available, so the one we are going to use is a little bit different, although it doesn`t matter because it uses the same pins, despite the arrangement of the components in the shield is something different. We will explain later how to make the connections based on a photograph.

In the Arduino Mega we will use the serial port number 1 for the transmission and reception of data with the bluetooth module, i.e., the pins RX1 and TX1. As always, remember that the bluetooth RX cable goes with the Arduino TX and vice versa.

As it happens in many modules we are going to use a voltage converter to power the bluetooth with 3.3 V instead of the 5V that gives the output of the Arduino pins. In our case it’s a YP-6, but there are plenty of them available. We connect the Arduino wires to the Vin and GND pins of the converter and the Vout and GND to our bluetooth module.

 

Rover bluetooth wiring diagram

 

As for the connection of the motors, it is necessary to take into account that they are numbered from M1 to M4, turning counterclockwise, with M1 and M2 being the front wheel motors and M3 and M4 The rear wheels. Following the color code, the red is for the positive, Vcc, and the black for the negative, GND. As we use 6 AA batteries of 1,5 V we will be feeding the motors with 9V in total and the Motor Shield will be in charge to feed individually each motor.

You have to pay attention to the yellow square on the right where the letters PWR, Power,  are printed, it is very important. In the square we can see a pair of pins that can be joined by a jumper. If the jumper were joining the two pins it would mean that we would be using the same power from the motors to feed our Arduino Mega as well, which is a bad idea because we are supplying 9V when our Arduino only needs 5V. Apart from possible erratic behavior we could run the risk of burning out our Arduino Mega, so we will leave the pins in the air, indicating that we will use independent power through the USB port of the Arduino via the powerbank to operate the board.

In the yellow square on the left side, the letters EXT_PWR (External Power) and +M (Positive Power Supply) are also printed. In this connector is where we must connect the power wires of our battery holder.

 

Motor shield

 

We only have to connect the powerbank with an A-B USB cable, like the printers cables, to our Arduino to feed it. It is a very economical solution, versatile, easy to recharge and not too heavy to be used in projects in which we want to move our Arduinos without having to use heavier, acid and lead batteries, for example.

¡Voilá! The bluetooth controlled Rover  is ready to upload our sketch.

 

The control sketch

 

We should first download  the library adafruit-Adafruit-Motor-Shield-library-8119eec and install it following the standard procedure.

Since we are going to use the Motor Shield, the first thing we must do is to import the library that will allows us to use it:

#include <AFMotor.h>

Now the following is to define a motor object for each of the motors that we want to control:

AF_DCMotor Motor1(1); // Forward right
AF_DCMotor Motor2(2); // Forward left
AF_DCMotor Motor3(3); // Reverse left
AF_DCMotor Motor4(4); // Reverse right

Then we define the speed and the proportion of rotation that we will use to control the motors:

int speed = 180 ;  // Defines the base speed of the Rover
float P = 0.25 ;   // Proportion of rotation

Now let’s go with the setup() function, in which we define the pin 49 of the Arduino Mega as an output (which will be used to power our Bluetooth module HC-05) and then we set it HIGH. The only thing left is to activate the serial communication of port 1 of the Arduino Mega, setting  a speed of 38400 bps, which is the default speed in the bluetooth modules.

void setup()
{
    pinMode(49, OUTPUT);    // Through this pin we drive the bluetooth module
    digitalWrite(49, HIGH);
    Serial1.begin(38400);   // Default speed in HC-05 modules 
}

Within the loop() function, what we do is to check if data is available through serial port 1 using the available() function, Serial1.available(). If there is any available data we define a char type variable, c,  that stores its value and then we analyze the chain using the function analyzeString(c).

void loop()
{
    	if (Serial1.available()){
      	   char c;
      	   c = Serial1.read();     
           analyzeString(c); 
	}	   
}

Here begins the interesting part, the function analyzeString(). This function receives as an input the data the Arduino Mega receives from the bluetooth module, char c, that is, a character data. What we must do is analyze it to know what orders we should send to the motors.

We use a trick to convert a char type to a string type, defining an empty string, String s = “”, and then concatenating it with the received character to form a string with the data that we have received through the bluetooth. This example is very simple and it would not be necessary, but depending on the chain that you receive it could be useful to be able to use the available functions for handling the strings.

In our case we have used a free app called Bluetooth Electronics that allows us also to modify the control panel to control the bluetooth module. It wouldn’t be necessary in our case since the only thing we are going to use is what the program calls a pad to handle the Rover in 9 directions, but it may be interesting for future projects.

If you use another program to control the Rover you should find out what sends the program to the Arduino via the bluetooth module, to be able to send the orders to the motors.

The modification that would have to be made to see what is sent is minimal. These instructions should be included in the setup() function to initialize the serial port and view the output on the serial console monitor:

Serial.begin(9600);
Serial.println("Waiting for commands");
Serial.println("--------------------");
Serial.println("");

And then inside the loop() function,  we add an statement to see what we get, Serial.write(c):

void loop()
{
    	if (Serial1.available()){
      	   char c;
      	   c = Serial1.read(); 
	   Serial.write(c);    
           analyzeString(c); 
	}	   
}

 

This way we would know what data the application sends every time we press a key and we can program the answer from the Arduino sketch.

In this case the program sends the following data:

 

Movements

 

Actually while holding the keys of the pad it sends a number from 1 to 8 and when we release it sends a zero. When we detect that a zero has been sent, we will use it to send the Rover the stop command, since there will be no key pressed at that moment.

The good thing about this app is that you can edit the values you want to send when the keys are pressed. We’ll see it later when we take a look at how the program works.

We use a multiple if loop with the different options and analyze the string using the startsWith() function. Depending on the value we get,  we will give the corresponding orders to the Rover.

 

void analyzeString(char c){
  String s="";
  s += c;
  if(s.startsWith("1")){
     // Forward 
     Fordward();
  }
  else if(s.startsWith("5")){
      // Forward right
      Forward();
      TurnLeft();
  }
  else if(s.startsWith("2")){
      // Right 
      TurnRight();	
  }
  else if(s.startsWith("6")){
      // Reverse right
      ReverseRight();
  }
  else if(s.startsWith("3")){
      // Reverse 
      Reverse();
  }   
  else if(s.startsWith("7")){
      // Reverse right
      ReverseLeft();
  }
  else if(s.startsWith("4")){
      // Left       
      TurnLeft();
  } 
  else if(s.startsWith("8")){
      // Fordward Left
      Fordward();
      TurnLeft();
  } 
  else if(s.startsWith("0")){
      // Stop
      Stop();
  }
}   

The only thing left is to include the functions that will govern the forward, reverse, turn, stop or speed control of the motors.

 

void SetSpeed(int v1, int v2, int v3, int v4)
{
     Motor1.setSpeed(v1);
     Motor2.setSpeed(v2);
     Motor3.setSpeed(v3);
     Motor4.setSpeed(v4);         
}

void Fordward()
{ 
  SetSpeed(speed,speed,speed,speed);
  Motor1.run(FORWARD) ;
  Motor2.run(FORWARD);
  Motor3.run(FORWARD);
  Motor4.run(FORWARD); 
}

void Reverse()
{ 
  SetSpeed(speed,speed,speed,speed);
  Motor1.run(BACKWARD) ;
  Motor2.run(BACKWARD);
  Motor3.run(BACKWARD);
  Motor4.run(BACKWARD);
}


void Stop()
{ 
  Motor1.run(RELEASE);
  Motor2.run(RELEASE);
  Motor3.run(RELEASE);
  Motor4.run(RELEASE); 
}

void TurnLeft()
{  
    int s = speed * P ;   
    SetSpeed( s, speed, s, speed) ;    
    Motor1.run(FORWARD) ;
    Motor2.run(FORWARD);
    Motor3.run(FORWARD);
    Motor4.run(FORWARD);    
}
         
void TurnRight()
{  
    int s = speed * P ;
    SetSpeed( speed, s, speed, s) ;    
    Motor1.run(FORWARD) ;
    Motor2.run(FORWARD);
    Motor3.run(FORWARD);
    Motor4.run(FORWARD);    
}

void ReverseRight()
{  
    int s = speed * P ;   
    SetSpeed( s, speed, s, speed) ;
    Motor1.run(BACKWARD) ;
    Motor2.run(BACKWARD);
    Motor3.run(BACKWARD);
    Motor4.run(BACKWARD);
}
         
void ReverseLeft()
{  
    int s = speed * P ;
    SetSpeed( speed, s, speed, s) ;    
    Motor1.run(BACKWARD) ;
    Motor2.run(BACKWARD);
    Motor3.run(BACKWARD);
    Motor4.run(BACKWARD);
}

 

 

The bluetooth app

 

We have been testing different applications and this one is quite simple to use, although the control panel is going to be a little empty  this time… just a pad with the nine directions. But they say beauty lies in simplicity, so let’s download the application.

 

The first thing we have to do is go to Google Play and search the Bluetooth Electronics app.

Bluetooth electronics

Once we have downloaded and installed it, we will open the application. We will have to have the bluetooth of the phone or tablet activated, otherwise a window will notify us that an application is trying to activate it, we will say “Allow” and we will see the following screen:

 

Discover devices

 

If we click the Discover button, discover bluetooth devices within reach, we will see a screen very similar to this. On the right we can seee the devices that have been paired with our tablet or mobile device, under Paired Devices, and on the left the ones that have been discovered now, under Discovered Devices.

 

Paired devices

 

In the list of discovered devices it will appear the name or names of the discovered devices and their MAC address. In the image we are already connected to the device, but if not we will click the Connect button and the application would connect to the device. At the top it will appear again the name of the device to which we are connected and its MAC address. Click on the Done button and we will advance to the next screen.

 

In the next screen we have a series of panels with which to be able to play. Some are already predefined, but through the Edit button, edit, we can modify them at our whim.

 

Connected to

 

We will move to panel number 9, which is an empty panel and we will give the Edit button to insert our control pad. Inside this new window we find a list of controls on the right side below the panel name that we are editing.

We will move to the Pads option and select the second from the left. Using the finger select the control, drag and drop it inside the panel deployment area. There is a lot of room and we will not use any other, so you can put it where you want, although it should be centered a little.

 

Panel screen

 

Note one thing, if we select the pad,  below to the right there appear some values for up, down, left and right and also an Edit button. These are the values that the program sends to the Arduino bluetooth module according to which key we press, but if you remember, we said that they can be changed so that we can choose the ones we want. If we click the Edit button, the following window will appear:

 

Edit screen

 

In the “Direction strings to send” section you can find all the values that are sent according to each of the directions that are pressed.

The string for the stop command is what it calls “Release String”, that is, the value that it sends when we release any key.

 

Later we have two options :

  • Only send the string when the key is pressed, “Send on change”
  • Continuously send the chain as long as the key is pressed, “Send Continuously while pressed”. If we select this option we could also define every how many milliseconds we want the string to be sent, inside the textbox “Repeat every (ms)”.

We will leave the default “Send on Change” option selected so that it only sends the string every time the key is pressed.

As an optional feature, we could add some initialization or completion characters to the string in the “Start with” or “End with” boxes, respectively, although we do not see this as necessary.

We would press the OK button and save the changes.

Now we just have to give the return button to return to the previous screen but this time, we will select panel 9 and press the Run button.

Now comes the best part, we can use the pad and start to play with our Rover, at least until the batteries run out.

We will do more things with the Rover but it is not thatt bad to start.

 

 

 

Note:

This is the version 2.0 of the sketch that enables to use a control slider to modify the speed:

bt_rover_v2

Summary

 

 

    • We keep on using the Adafruit Motor Shield v1.
    • We have seen how to control the rover via bluetooth through our mobile phone or tablet.
    • We have learnt how to use an Android app to control the rover via bluetooth: Bluetooth Electronics.
    • We have learnt a new use for our mobile phone’s powerbank: to power our Arduino.
 
 
 

 

 

 

No Comments

Give a Reply