DIY Simple Arduino Bluetooth controlled Robot

This is a detailed guide on how to build a Bluetooth controlled robot using an Arduino, the robot is controllable from any mobile device or laptop over Bluetooth, has lights, and a distance sensor to prevent it from crashing into the stuff. The Bluetooth can also be used to remotely program your Arduino and directly drive your car from the Arduino IDE console.

Requirements
  • A robot basis with 2 or 4 motors :
    DX.com #1 – #2 – #3 #4 – Ebay #1#2#3#4
  • Stepper Motor Driver for Arduino :
    DX.com #1#2#3 – Ebay #1#2#3
  • Arduino Duamilanove / Uno :
    DX.com #1 #2 #3Ebay
    – Batteries :
    DX.com #1#2#3 – Ebay #1#2

  • (Optional) 2x RGB Led (shows connection status/headlights)
    DX.com #1#2#3 – Ebay #1#2#3#4

Wiring the motors

I used the “L298N Stepper Motor Driver Controller Board” from dealextreme, but for most boards, the wiring should be about the same. The controller board has 8 connectors (+5V,GND,ENB,IN1,IN2,IN3,IN4,ENA). I will only be using the 4 IN ports to control the motors, I will be powering the board directly from the battery so +5V and GND are not needed from our Arduino, ENA, and ENB are optional so leave these open as well:

motordriverinputs

On the other side, there are 3 terminals, 2 are for the motors and the center terminal is for powering the driver board (don’t power this board only from your Arduino as the motors draw too much current)

motordrivermotorwiring

Motor(s) A   –   Power   –   Motor(s) B

The motor driver board is very easy to control, there are 4 pins…
IN1 and IN2 are for driving the “A” side of the motor board while IN3 and IN4 drive the “B” side of the board. You can try controlling the motors yourself or you could use my code below.

Wiring LED's

I used 2 RGB LED’s as front lights, these will show Bluetooth connection status & function as headlights when connected, the color can also be changed from your Phone or Tablet. RGB LED’s have 4 connection pins(R,G,B,GND or +5V)

arduinorobotrgbledbackside

Depending on what RGB LED’s you bought you may need resistors in front of your LED’s, never connect LEDs directly onto your Arduino. Any value from 300 Ω to 2KΩ is probably OK.(keep the value low for maximum LED brightness)

arduinorobotrgbleds

Connect the LED color pins to the following pins on your Arduino :

int Red1 = 9;
int Green1 = 8;
int Blue1 = 10;
int Red2 = 12;
int Green2 = 11;
int Blue2 = 13;
Wiring the Bluetooth Module

The Bluetooth module (HC-05 or HC-06) has 4 pins (+5V, GND, TX, RX).

+5V – Power the chip from the Arduino’s 5V output
GND – Connect to Arduino’s GND
TX – Connect to Arduino’s RX
RX – Connect to Arduino’s TX

arduinobluetoothmodule

That is all, you can now test your Bluetooth chip by connecting to your phone: (The default pin is 1234 or 0000)

arduinobtconnect3

For more information, I have a more detailed guide to Arduino Bluetooth communication HERE

Code your Arduino

Program following code in your Arduino and your robot should work right away.

/*
The application will send only one character that represents the button pressed, where:
0 = no key pressed;
1 = UP key pressed;
2 = DOWN key pressed;
3 = LEFT key pressed;
4 = RIGHT key pressed;
5 = X button pressed;
6 = O key pressed;
7 = SQUARE button pressed;
8 = TRIANGLE button pressed;
9 = SELECT key pressed;
A = START key pressed;


the app sends "0" before close the connection.

*/

int motor_A1=2; 
int motor_A2=3; 
int motor_B1=4;
int motor_B2=5;
//leds
int Red1 = 9;
int Green1 = 8;
int Blue1 = 10;
int Red2 = 12;
int Green2 = 11;
int Blue2 = 13;


char val; // variable to receive data from the serial port
int ledpin = 13; // LED connected to pin 48 (on-board LED)
void setup() 
  {  
  //Motor Pins:
  pinMode(motor_A1,OUTPUT);  
  pinMode(motor_A2,OUTPUT);  
  pinMode(motor_B1,OUTPUT);  
  pinMode(motor_B2,OUTPUT); 
  //Led Pins
  pinMode(ledpin, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  pinMode(Green1, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  pinMode(Green2, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  pinMode(Red1, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  pinMode(Red2, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  pinMode(Blue1, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  pinMode(Blue2, OUTPUT);  // pin 48 (on-board LED) as OUTPUT

//init leds
      digitalWrite(Green1,LOW);  
      digitalWrite(Green2,LOW);
      digitalWrite(Red1,HIGH);  
      digitalWrite(Red2,HIGH);
      digitalWrite(Blue1,LOW);  
      digitalWrite(Blue2,LOW);

  //init serial connection
  Serial.begin(9600);       // start serial communication at 9600bps
}


void loop()
{
  if( Serial.available() )       // if data is available to read
    {
       val = Serial.read();         // read it and store it in 'val'
    }

    //Input key switch
    switch (val) {
    case '0':    
        {
        digitalWrite(motor_A1,LOW);  
        digitalWrite(motor_A2,LOW); 
        digitalWrite(motor_B1,LOW);  
        digitalWrite(motor_B2,LOW);
      break;
    case '1':  
        digitalWrite(motor_A1,LOW);  
        digitalWrite(motor_A2,HIGH); 
        digitalWrite(motor_B1,LOW);  
        digitalWrite(motor_B2,HIGH); 
      break;
    case '2':    
        digitalWrite(motor_A1,HIGH);  
        digitalWrite(motor_A2,LOW); 
        digitalWrite(motor_B1,HIGH);  
        digitalWrite(motor_B2,LOW);
       break;
    case '3':    
        digitalWrite(motor_A1,LOW);  
        digitalWrite(motor_A2,HIGH); 
        digitalWrite(motor_B1,HIGH);  
        digitalWrite(motor_B2,LOW); 
      break;   
    case '4':    
        digitalWrite(motor_A1,HIGH);  
        digitalWrite(motor_A2,LOW); 
        digitalWrite(motor_B1,LOW);  
        digitalWrite(motor_B2,HIGH);  
      break;
    case '5':    
      //Code when SELECT key is pressed 
      digitalWrite(Green1,LOW);  
      digitalWrite(Green2,LOW);
      digitalWrite(Red1,LOW);  
      digitalWrite(Red2,LOW);
      digitalWrite(Blue1,HIGH);  
      digitalWrite(Blue2,HIGH);
      
      break;
    case '6':    
      //Code when Start key is pressed 
      digitalWrite(Green1,LOW);  
      digitalWrite(Green2,LOW);
      digitalWrite(Red1,LOW);  
      digitalWrite(Red2,LOW);
      digitalWrite(Blue1,LOW);  
      digitalWrite(Blue2,LOW);
      break;
    case '7':    
      //Code when TRIANGLE key is pressed
      digitalWrite(Green1,HIGH);  
      digitalWrite(Green2,HIGH);
      digitalWrite(Red1,LOW);  
      digitalWrite(Red2,LOW);
      digitalWrite(Blue1,LOW);  
      digitalWrite(Blue2,LOW);
      break;
    case '8':    
      //Code when SQUARE key is pressed
      digitalWrite(Green1,LOW);  
      digitalWrite(Green2,LOW);
      digitalWrite(Red1,HIGH);  
      digitalWrite(Red2,HIGH);
      digitalWrite(Blue1,LOW);  
      digitalWrite(Blue2,LOW);
      break;
    case '9':    
      //Code when X key is pressed 
   for (int i=0; i <= 13; i++){
      digitalWrite(Green1,LOW);  
      digitalWrite(Green2,LOW);
      digitalWrite(Red1,LOW);  
      digitalWrite(Red2,LOW);
      digitalWrite(Blue1,LOW);  
      digitalWrite(Blue2,LOW);
      delay(60);
      digitalWrite(Green1,HIGH);  
      digitalWrite(Green2,HIGH);
      digitalWrite(Red1,HIGH);  
      digitalWrite(Red2,HIGH);
      digitalWrite(Blue1,HIGH);  
      digitalWrite(Blue2,HIGH);
      delay(60);
   } 
      break;
    case 'A':    
      //Code when START key is pressed 
      break;
    case 'B': 
        digitalWrite(motor_A1,LOW);  
        digitalWrite(motor_A2,HIGH); 
        digitalWrite(motor_B1,LOW);  
        digitalWrite(motor_B2,LOW);   
      break;
    case 'C':    
        digitalWrite(motor_A1,HIGH);  
        digitalWrite(motor_A2,LOW); 
        digitalWrite(motor_B1,LOW);  
        digitalWrite(motor_B2,LOW); 
      break;
    case 'D':    
        digitalWrite(motor_A1,LOW);  
        digitalWrite(motor_A2,LOW); 
        digitalWrite(motor_B1,LOW);  
        digitalWrite(motor_B2,HIGH); 
      break;
    case 'E':    
        digitalWrite(motor_A1,LOW);  
        digitalWrite(motor_A2,LOW); 
        digitalWrite(motor_B1,HIGH);  
        digitalWrite(motor_B2,LOW); 
      break;
    default:
      // default code (should never run)
      //init leds
      break;
    }
delay(50);
  }
}
Android control App

I used the Arduino BT Joystick App, it has a free and pro version,

ArduinoBTJoystickFreePro

The app can be downloaded from the official google play store:
Download Here

Connecting via Bluetooth

Before connecting, make sure your device is connected and turned on,  a red LED should be flashing when nothing is connected via Bluetooth. On your Android device, open your Bluetooth settings:

arduinobtconnect1

Scan for Bluetooth devices until you find your device in the list of available devices. To connect click the device in the list and enter the default ppin code(usually 1234 or 0000)

arduinobtconnect2

When successfully connected, the device should say “Paired”:

arduinobtconnect3

Now open the BT Joystick app and click “Connect”, now fill in the exact name of your Bluetooth device and hit connect:

arduinobtconnect4

The dialog should close and when connected, the led on the Bluetooth module should stop flashing.

There are different modes available in this app, The best mode for robots is the “Drive” mode, however, the joystick mode allows the same controls in a different layout.

androidbtnormaljoystick

Share:

Facebook
Twitter
Pinterest
LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

On Key

Related Posts