Android controlled robot

Android controlled robot?!? What the hell is he talking about? That’s impossible!… It’s not! Or at least I hope it’s not. The goal is to control a Dwengo robot with my HTC Desire, which runs the Google Android OS. The Android phone will connect to a Bluetooth module  using an SPP connection and the Bluetooth module then connects to the robot controller (a Dwengo board). This makes it possible to send ASCII characters from the phone to the Dwengo board which then reacts  by turning on the motors in the proper way , depending on the character and it’s done. You see, electronics doesn’t have to be difficult. But maybe you want some more details.


Step 1: Getting the bluetooth connection going

First I bought a WRL-08550 bluetooth module at sparkfun, but I destroyed it somehow. So, now I’m using a Dwengo bluetooth module (see picture). This one is a lot cheaper (18 euro with shipping included) because it has a range of only 10m while the WRL-08550 had a range of 100m. Another cool thing is that the new module uses a 5V power supply, which is a lot easier to interface with the Dwengo board.

As you can see in the picture, the module only has only 4 pins: 5V, GND, TX and RX. The 5V pin and the GND pin are used for power and the TX and RX pin implement a typical serial interface. The TX pin is used by the module to send information and the RX pin is used to receive information.

To test the module, I first hooked it up to my PC. This makes it easier to see whether the module is receiving characters or not. You simply use a terminal program like Hyperterminal or Tera Term to visualize what the module is sending from its serial interface. The picture below shows how I made the test setup using the Dwengo board.

The hardware

To connect the Bluetooth module to a PC the voltage levels of the RX and TX signal need to be changed. This is done by the MAX232 IC on the Dwengo board. Normally this IC is used to connect the microcontroller of the board to a PC but by temporary removing the microcontroller, the bluetooth module can easily take the place of the microcontroller. The Dwengo bord is connected to the PC using a cheap USB to serial cable.

The software

On my PC I use the Tera Term program. Just open the program and you will be asked to setup a new connection. Chose for a serial connection and select the correct COM port. If you are using a USB to serial cable like I’m doing, you can find the COM number in the device manager next to the entry of your cable. The only thing left to do is set the baud rate to 9600, which is the standard setting for my module (Menu: Setup>Serial Port). Other modules may use an other baud rate! If you don’t have a datasheet, you can find the baud rate by trial and error.

On my Android Phone I installed a free app called BlueTerm which works great. The first time you try to connect to the Bluetooth module you need to pair up the devices, but this is extremely easy. First chose “Connect device” from the menu, then tap “Scan for devices”. If the module is powered up it should be detected. Mine is called “linvor”. Tap the name and you get a Bluetooth pairing request in your notifications tap it and you are ask for a pin code. For my module this pin code is 1234 which I just tried. Tap Ok and the connection should be made.

At this point every thing you type on your android should appear on your PC and vice versa. If not, you’re in touble and should start looking where you went wrong.

Step 2: Bluetooth module/Dwengo board connection

In the second step I want to test the serial connection between the bluetooth module and the Dwengo board to see if the Dwengo board receives the characters send by the android phone and if the characters send by the Dwengo board are received by the android phone. The easiest way to do this is implementing an echo on the Dwengo board. This means that the board sends back every character it receives. If this works both directions of the connection work.

The first lets write the C code (See below) that implements the echo on the Dwengo board. To do this, we use the Dwengo library which makes our lives a lot easier. You need to download the lib and install it like is explained here. The code first initializes the board and opens a serial connection with a baud rate of 9600, which is the standard baud rate used by the Bluetooth module. After the initialization the program repeatedly waits until a character is received (line 11), then waits until the sending of the last character is completed (line 12) and finally the transmission of the received character is started by writing the received character (RCREG) to the transmission register (TXREG) on line 13.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <dwengoConfig.h>
#include <dwengoBoard.h>
#include <dwengoUsart.h>
 
void main(void) {
 
  initBoard();
  initUsartAdj(BAUD9600);
 
  while(TRUE){
    while(PIR1bits.RCIF==0) {}
    while(PIR1bits.TXIF==0) {}
    TXREG = RCREG;
  }
}

The hardware is almost the same as in step 1. Of course, I plugged the PIC back into its socked and reinstalled the LCD. We also need to swap the RX and TX signals because now we want the RX input of the bluetooth module to be connected to the TX output of the PIC and vice versa. Another important thing is to remove jumper JP1-4. In this way we disconnect the MAX232 IC from the RX input of the PIC. If we would not do this, there would be two sources for the RX signal of the PIC: the transmit signal of the bluetooth module and the transmit signal of the MAX232 IC. This would be a short circuit and obviously we don’t want that.

If everything is right, everything you send to the bluetooth module with the Blueterm app should be returned immediately . If you have local echo turned on in the Blueterm preferences every character you type will be printed twice. If you turn of local echo you see every character only once.

Step 3: Controlling the robot

The nice thing about the bluetooth program is that it also sends characters when the optical trackball is used. Every time the trackball is used 3 characters are send of which the first two are independent of the movement. The value of the last character equals 65 for up, 66 for down, 67 for right and 68 for left.

Now we know that, there’s not a lot more to do in order to control a robot at this point. We simply need to extend echo code found in step 2 so that it turns on the robots motors in the right way given a received character. The code can be found below. We simply added a switch statement that controls the motors of the Dwengo robot depending on the received character. When one of the trackball characters is received, the motors are turned on. For any other character the motors are stopped. Because of the connections and the orientation of the motors, there speed values need to be inverse in order to drive strait.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <dwengoConfig.h>
#include <dwengoBoard.h>
#include <dwengoMotor.h>
#include <dwengoUsart.h>
 
#define SPEED 1023
 
void main(void) {
  char c;
 
  initBoard();
  initMotor();
  initUsartAdj(BAUD9600);
 
  while(TRUE){
    while(PIR1bits.RCIF==0) {}
    while(PIR1bits.TXIF==0) {}
    c = RCREG;
    TXREG = c;
 
    switch (c) {
      case 65: //UP
        setSpeedMotor1(SPEED);
        setSpeedMotor2(-SPEED);
        break;
      case 66: //DOWN
        setSpeedMotor1(-SPEED);
        setSpeedMotor2(SPEED);
        break;
      case 67: //RIGHT
        setSpeedMotor1(SPEED);
        setSpeedMotor2(SPEED);
        break;
      case 68: //LEFT
        setSpeedMotor1(-SPEED);
        setSpeedMotor2(-SPEED);
        break;
      default:
        stopMotors();
    }
  }
}

The final result

That’s it!
Karel

Bookmark and Share

13 Comments on “Android controlled robot”

  1. coolouba says:

    Hi:
    I’m a university student in China,who loves developping hardware as you。Things in your passage are what i am really interested.
    My android phone doesn’t work well with your app,however my classmate’s does。It’s a HTC device too, HTC G3.Though it‘s ok to find the Bluetooth-serialport module(HC04),it just could not make the connect.
    Could it be a problem with phone ROM or the configuration of Bluetooth?Can you offer your advice?It’s of great importance to me.TKS!

    • Karel says:

      Hi Coolouba,

      Nice to hear from a hardware enthusiast from the other side of the world. ;-)

      Have you tried to connect to your module using the BlueTerm App? Does this work?

      Can you connect to your friends module? Or can your friend connect to your module?

      Cheers!
      Karel

      • coolouba says:

        hei,man,of couse I did these things. it’s OK to connect with others’ phone to transfer files
        , excpet a htc one.
        it can pair the bluetooth serial-port,however it could not make the connection.i think maybe i should abandon it,with my anger!
        could i build a android Bluetooth project to test what is wrong?i am worried it’s proble of the hardware,which means i could solve this unless i afford me new one!

  2. coolouba says:

    To make it clear,G3 is my phone .My Bluetooth-serialport module is almost the as yours.
    I’m looking forward your reply.

  3. lucky says:

    haw about via wifi?

    • Karel says:

      Hey Lucky

      Wifi is a possibility, but it is a lot harder than bluetooth. I have a wifi module, but I only did some small tests with it at this moment. I might write a blog post about it later.

      Karel

      • lucky silva says:

        alriht. it’s okay..
        I now make my final project about android application to control the camera ip via wifi.. I use a wireless router on the side of the camera and use a microcontroller to adjust the servo motor .. ip camera was mounted on a servo motor.. so camera can move follow the movement of the servo motor .. but I am confused to make program for android? if you can help me .. thanks

  4. Sunil says:

    hi, can u tel me which software i have to use for my android mobile for this application.

    can anyone give me the link for the software,

  5. lucky silva says:

    hi karel..i like this post..
    i want to ask about comunication beetwen android and robot..are communication beetwen android and robot can use wifi? thanks

  6. arturo says:

    I would like to know the elements of the car
    it takes?
    whether the PCB would have?
    greetings ..!

  7. Kush says:

    Hi Karel,

    How can I make your android code where the android does not need a value (in this case ‘r’) from the robot to send messages such as c, s, x, y, and z? How do I just constantly send out messages to the robot without having to receive a variable?

    Thanks,

    Kush

    PS. Your blog is amazing. Thank you for sharing your work.

    • Karel says:

      Hi Kush

      Sorry for the late reply.

      You should put the code that is in the bluetoothWrite function on the botom of the onSensorChanged function. This way the message is send to the robot every time the sensor is read. The reading of the sensor is periodical.

      Hope this helps!

      Good luck!
      Karel


Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">