Project tutorial
Two Mode Robot Controlling through Android and Windowsphone8

Two Mode Robot Controlling through Android and Windowsphone8

Basically A Car which is controlled by 2 Modes: Accelerometer Sensor of the phone, and Autonomous (Moves in path and avoids object)

  • 17,929 views
  • 10 comments
  • 52 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
Can also use other versions such as Mega
×1
09670 01
SparkFun Dual H-Bridge motor drivers L298
×1
61pby065esl  sx679  tnr8syww5d
HC-05 Bluetooth Module
Bluetooth Module or any other similar model
×1
HC-SR04 Ultrasonic Sensor
×1
Servo Motor
to hold the ultrasonic sensor, In my case (Tower-Pro sg90)
×1
Tens70
9V battery (generic)
Good enough to Supply arduino+bluetooth+ultrasonic , I recommend to get 2-3 of these
×1
Battery (Motor Supply depending on your motors)
Depends on your motor rating , A greater mAh means better speed at some load. I used LIPO 7.2V 1500mAh which is rechargable
×1
Male/Male Jumper Wires
Need plenty of these for connections
×1
826 04
Male/Female Jumper Wires
×1
Gopigo base kit assembled 3
Dexter Industries GoPiGo Robot Base Kit
Any robot chasis will work, In my case I made my own from Aluminium sheet and attached 2 DC motors.
×1

Apps and online services

About this project

Hello there guys, My Initial Idea was to make a robot through scratch parts (the robotic Structure mainly) and making it controlled by a Windows phone 8 Application through Bluetooth, The Robot is controlled by the Accelerometer sensor of the Phone but however the development of the application is not covered here in this tutorial instead I have provided link to store. The project covers the Accelerometer controlling and also the autonomous part but you guys can seperate them if you're not willing to integrate anyone of them.  

The application is also compatible with other bluetooth projects and can be configured in the settings of the application (detailed explanation in the last).

App links:

Windows phone : https://www.microsoft.com/store/apps/9NBLGGH5J3WS

Android : https://github.com/Anas-siddiqui/Robocontrol

Ok Let's start making the project

  • Accelerometer Mode

Starting from the Arduino we will first connect the DC motors and the bluetooth module. Connect the HC-05 pins accordingly as shown in the Schematic diagram, As per logic the RXD pin of bluetooth module goes into the TXD of arduino Digital pin 11 ( Which we have assigned in our code) and TXD of the module into the RXD of arduino digital pin 10. The Vcc of the module is connected to arduino 5V pin, both the module and arduino are powered seperately in order to avoid any connection issues due to the current. Ground pin of module is connected to gnd of arduino (any of them, all are common). On powering the bluetooth module blinks its LED continuously.

Motor driver L289N is powered by another battery which I recommend must be powerful enough to drive the motors, Please keep in mind that the more load is applied on the motor the more it will drop the current from the battery. Connect the pins as described in the diagram, The IN1-IN4 pins are used to control the directions by making them HIGH and LOW from the code so the order described in the diagram is important otherwise the motors will work in opposite direction.

Please note that there is a 5V regulator pin in the driver which generates 5V when the driver is poweredup, You can utilize this pin to power your other components which require 5V to powerup, We will Utilize this pin later when we will attach servo and other components to it.

The First Part is done here and you can now connect the application

  • First pair the device in the Phone's setting- Bluetooth The passcode I dont remember correctly maybe 1234 or 12345
  • After pairing, the device will be Shown in the Application and then tap on it to connect
  • On connecting the application will show connected and LED of the Module will blink in intervals which will indicate that it is now connected
  • On the Control page press the Start button to start using the Accelerometer Mode, The video might help you guys more

Object Avoid Mode

For this mode we will just program the arduino a little bit more , We will connect an Ultrasonic and Servo motor.

Ultrasonic Sensor

The Vcc pin of ultrasonic is connected to to 5V regulator pin of driver , ground of the sensor to any ground of the circuit and Trig,echo pins as described in diagram. Because I was facing some error in distance reading that's why I connected this sensor to the driver's 5V pin otherwise if your arduino battery is good enough then you can connect it to the 5V pin of arduino too and the ground remains the same(any of the grounds).  

That's it all the components are connected and we are now ready to go for the mode 2

Code

Arduino CodeC/C++
Most of things are explained through comments , you must know C language in order to play with it
#include <SoftwareSerial.h> 
 #include <NewPing.h> //ultrasonic library
#include <Servo.h> //servo library
#define RightmotorF 7  //digitalpin 7 for right motor forward
#define RightmotorB 8  //digital pin 8 for right motor Backward
#define LeftmotorF 5 //digitalpin 5 for left motor forward
#define LeftmotorB 6 //digital pin 6 for right motor Backward
Servo myservo;  
int LeftDistance=0;
int RightDistance=0;
int distance=0;
int val;
String message="";

int FrontDistance=0;
NewPing sonar(2, 3, 400); //(trig,echo,maxdistance)
SoftwareSerial BT(10,11);  //Assigning arduino's (RXD,TXD)
void setup()
{
  // Setup LED

  pinMode(RightmotorF, OUTPUT);//declaring these pins as output to control them
    pinMode(RightmotorB, OUTPUT);
    pinMode(LeftmotorF,OUTPUT);
    pinMode(LeftmotorB,OUTPUT);

  Serial.begin(9600);
 myservo.attach(12);//telling the code that servo is at digital pin 12
  
  BT.begin(115200);
  
  BT.print("$$$");//Bluetooth stuff dont change
 
  delay(100);
  
  BT.println("U,9600,N");
 
  BT.begin(9600);
   serv(512);//setting the servo at initial position (Change this accordingly)
}

void loop()
{
 
 
  
  if(BT.available())
  {
 
  //Through comparing characters we will communicate, you can change this in app too
    char GetBT = (char)BT.read();
 
  
       if(GetBT=='d'){digitalWrite(RightmotorF, HIGH);//for forward movement both R  & L forwards are high and backward low
        digitalWrite(RightmotorB,  LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW); } 
   else if(GetBT=='s'){ digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);} 
  else if(GetBT=='a'){digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH); }
  else if(GetBT=='g'){ digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);}
   else if(GetBT=='f'){ digitalWrite(RightmotorF, HIGH);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
   } 
  
     
  if(GetBT=='o')
   {
     while(1)
    {
      
     
      scan();//checking the distance
   
      FrontDistance=distance;
      
     // if(FrontDistance>20|| FrontDistance==0)
        if(FrontDistance>20)
      {
      
       Forward(); 
      }
      else
       {
        movestop();
        navigate();
       
         
         
      }
        
    
    if(BT.available())
    {
          char newBT = (char)BT.read();
        
        if(newBT=='t'){movestop();break;}
    }
     }
   
   
  
   }
   else if(GetBT=='k')
   {
     scan();
   //  Serial.println(distance);
     BT.print(distance);
   }
   
   
    
   }
    
   
 
    

  

 

   
    
    
  
  
  if(Serial.available())
  {
 //this part can be used to test through serial monitor for the motors
    char al=(char)Serial.read();
      
       //  int uS = sonar.ping();
    //   Serial.print("Ping: ");
    //   Serial.print(uS / US_ROUNDTRIP_CM);
     //  Serial.println("cm");
     if(al=='d'){ digitalWrite(RightmotorF, HIGH);
        digitalWrite(RightmotorB,  LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);} 
   else if(al=='s'){digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);} 
  else if(al=='a'){digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);  }
  else if(al=='g'){ digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);}
   else if(al=='f'){ digitalWrite(RightmotorF, HIGH);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
      
   
   }
   
    
//    Serial.print(al);
   
   
   
  
    
  }
 
 
}
void serv(int a)
{
    val=map(a,0,1023,0,179);
  myservo.write(val);
  delay(1000);
}
void Forward()
{
  digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
}
void Backward()
{
 
        digitalWrite(RightmotorF, HIGH);
        digitalWrite(RightmotorB,  LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);
}
void Right()
{
  digitalWrite(RightmotorF, HIGH);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
}
void Left()
{
  
     digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);
}  
void movestop()
{
  digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);
   
}
void scan()
{
 int uS = sonar.ping();
      
  distance=(uS / US_ROUNDTRIP_CM); 
 delay(500);
}

  void navigate()
{
   
    serv(1023);                                //Move the servo to the left (my little servos didn't like going to 180 so I played around with the value until it worked nicely)
                                          //Wait half a second for the servo to get there
    scan();                                           //Go to the scan function
     LeftDistance = distance;                          //Set the variable LeftDistance to the distance on the left
    
 
   serv(10);                                  //Move the servo to the right
                                        //Wait half a second for the servo to get there
    scan();                                           //Go to the scan function
    RightDistance = distance;                         //Set the variable RightDistance to the distance on the right
    
    
    if(abs(RightDistance - LeftDistance) < 5)
    {
      Backward();                                  //Go to the moveBackward function
      delay(200);                                      //Pause the program for 200 milliseconds to let the robot reverse
     Right();                                     //Go to the moveRight function
      delay(100);       //Pause the program for 200 milliseconds to let the robot turn right
   serv(512);
  }
    else if(RightDistance < LeftDistance)                  //If the distance on the right is less than that on the left then...
    {
    Left();                                      //Go to the moveLeft function
     delay(100);       //Pause the program for half a second to let the robot turn
   serv(512);
  }
    else if(LeftDistance < RightDistance)             //Else if the distance on the left is less than that on the right then...
    {
     Right();                                     //Go to the moveRight function
     delay(100);                                      //Pause the program for half a second to let the robot turn
   serv(512);
  }
}

Schematics

Schematic Diagram
The diagram is simple, Starting from the Arduino I have connected 9V battery to give supply to arduino and through it's 5V pinout I'm giving supply to my ultrasonic and bluetooth module. For the HC-05 Bluetooth Module the RXD is connected to Digital pin 11 and TXD of module to Digital pin 10 as per logic the RXD of Module goes into the TXD of Arduino(assigning of pin 11 of arduino as TXD is done in code) and TXD of Module to RXD of Arduino. For the Motor Driver L298N the +ve terminal of Motor's battery is connected to +V of the L298N driver and -ve terminal to Gnd, The driver automatically generates 5V when you connect the battery, The IN1- IN4 pins are used to control the motor directions which are connected to arduino, EN pins are shorted to 5V to enable them and the motors are connected to the terminals labeled A & B. Please Note all the grounds are connected to each other that means they all will be common
Circuit
Schematic Diagram
The Circuit is simple
Starting from the Arduino I'm using a 9V battery to power the arduino+bluetooth. The +ve terminal of the battery is attached to the Vin pin of arduino and -ve terminal to ground(any of them doesn't matter) of the arduino.

- HC-05 Bluetooth module for which the Vcc of the module is connected to 5V regulator pin of arduino, ground of the module to gnd of arduino, the RXD pin of bluetooth module to Digital pin 11 of arduino and TXD of module to Digital pin 10 of arduino.

-Motor driver L298N which we are powering through another battery , The +ve terminal goes into the +V of the driver and -ve of the battery goes into the gnd of the driver . The driver regulates the voltage and give output of 5V in the 5Vout pin , The IN1-IN4 pins are used for controlling and are connected to arduino digital pins as described in the diagram and finally the motors are connected to Motor A and Motor B pins.

-UltraSonic Sensor , The Vcc is connected to the 5V regulator pin of Motor driver L298N, Ground pin of the sensor to any ground (All the grounds are common so it doesnt matter) , Trig of the sensor to arduino Digital pin 2 and Echo of the sensor to arduino Digital pin 3.
Circuit
Schematic Diagram
The Diagram is simple

Starting from the 9 V battery
-The positive terminal goes into the Vin of arduino
-The negative of battery goes into the ground

For the Bluetooth Module
- RXD goes into the digital pin 11
- TXD goes into the digital pin 10
- Vcc goes into the arduino 5V pin(to get the 5V for power)
- gnd goes to any of the grounds

For Motor Driver L298N

- The IN pins are for controlling and goes into digital pins of arduino(there order matter otherwise motor will work in opposite direction)
- +V of driver goes to the positive terminal of the other battery
- Ground is connected to negavtive of battery
- Motors are connected in Motor terminals A & B

For the Ultrasonic Sensor

- Vcc goes to the 5V pin of motor driver (to get 5V supply to power sensor)
- ground can be any of them
- Trig and echo pins as accordingly shown in the diagram

Other Notes
More details can be found in the story section and make sure to check all the grounds are connected to each other .
Circuit

Comments

Similar projects you might like

Make your first Arduino robot - The best beginners guide!

Project tutorial by Muhammed Azhar

  • 44,102 views
  • 15 comments
  • 98 respects

Controlling A Roomba Robot With Arduino And Android Device

Project tutorial by MJRoBot

  • 11,220 views
  • 4 comments
  • 12 respects

2WD Voice Controlled Robot with Arduino and BitVoicer Server

Project tutorial by Marcio Tamagushi

  • 10,208 views
  • 11 comments
  • 32 respects

Smart Face Tracking Robot Car

Project tutorial by Sebastian Hernandez

  • 5,995 views
  • 5 comments
  • 37 respects

Smartphone Controlled Arduino 4WD Robot Car

Project in progress by Andriy Baranov

  • 53,241 views
  • 43 comments
  • 98 respects

Control Arduino Robot Arm with Android App

Project tutorial by Slant Concepts

  • 9,003 views
  • 7 comments
  • 26 respects
Add projectSign up / Login