Bluetooth Steering Wheel
Posted: January 19th, 2011 | Author: Karel | Filed under: Uncategorized | 83 Comments »For my next project I want to take the Android controlled robot one step further. In my previous post, I used the ability of the Blueterm program to send characters over Bluetooth. So, I didn’t had to do any Android programming. The Blueterm program had all the functionality I needed. But since I’d like to learn how to build an Android app, here is my next challenge. The want to hold the phone in front of me as if I’m holding a steering wheel. If I tilt the phone forward I want the robot drive forward. If I tilt the phone backward the robot needs to drive backward. If I turn left the robot turns left and so on … Therefore, I want to build an Android app that sends the data of its acceleration sensor over Bluetooth to the Dwengo bot. From this data the orientation of the phone with respect to earth can be determined, and from this orientation it must be possible to make the robot behave as I just described.
It’s working!
Ok guys its working. The prove can be found below.
The Source Code
The code is available for download on the bottom of this page. The file contains both the code for the android app and the code for the Dwengo board. The android project can simply be imported (File> Import> General> Existing Projects into Workspace) in eclipse if you have installed the ADT plugin.
Building an android app is not very difficult. If it’s your first time you should visit the android developer website. It contains a lot of good information on how to build an android app. Just take some of the the tutorials and you’re on your way.
How does it work?
On the highest level, the robots requests the next command by sending the character ‘r’ to the phone over the Bluetooth connection. The phone replies by either sending the character ‘s’ (stop) when driving is deactivated or the character ‘c’ followed by the x, the y and the z components of the gravity vector. The vector is normalized and rescaled so that each component fits in the range -128 to 127. In this way each component can be send as one byte. For the details please download the code and take a look at the comments. If things stay unclear you should leave a comment on the bottom of the page.
I spend most of my effort on the BluetoothSPPConnection class in the hope that it can be easily reused in one of your bluetooth experiments. You simply create an object of the class and open a connection with a device using the open(device) method. Next, you create an object that implements the BluetoothSPPConnectionListener interface. The interface contains methods that are called when the status of the connection changes (onConnecting(), onConnected(), onConnectionFailed() and onConnectionLost()). To send characters from the phone to the module you simply call the write(byte[] buffer) method, where buffer contains the data that should be send. When data is send by the module to the phone, the bluetoothWrite(nmbrBytes, buffer) method is called by the system, where nmbrBytes is the number of bytes that is received and buffer contains these bytes. If every thing is right, all thread and message passing stuff is handled by the BluetoothSPPConnection object.
If you discover a bug please leave a comment on this page. I will fix it as soon as possible.
That’s it for now. Hope to see you soon!
Karel
PS: If you like this post, please click one (or all ) of the social network buttons below. Thanx!
BluetoothSteeringWheel.zip (3506) (Please like before you download? Thanx!)

i want to do same project using bluetooth mate +arduino2009 from (sparkfun.com) can you help me
Sure, I think there should be no problem to do this. The bluetooth mate should do the same thing my bluetooth modules does, and the arduino board can take over from the dwengo board. I don’t have much experience with the arduino board though. The andoid app stays the same.
thank you so much
Karel,
1. thank you for putting this up. Been looking all over the net
2. downloaded your app and ran under Eclipse. The file opens up, but it’s keep on force closing, do you have any idea why?
thank you
Hi Sam,
Have you imported the project into the workspace like this?
(File> Import> General> Existing Projects into Workspace)
Which OS are you using?
You can also try the following dirty thing:
* Create an andoid project
* Copy my files into it.
Regards
Karel
Wait, does eclipse force close or the app?
Are you running on a real phone or on an emulator?
Hi Karel,
yes, import>general>existing projects….
vista in 32bit is my OS
I don’t know why the import does not work. What I do know is that the project is made on OS X and you are using Windows. I will try to import the project on my windows VM and see what happens.
thank you!
looking at your “AndroidManifest.xml” file,
“.BluetoothSteeringWeelService”
and “TermPreferences” classes are missing.
That’s just because I forgot to clean up the AndroidManifest.xml. I will do that soon.
Thanks!
Done!
Hi Karel, I’d like to ask you some technical question. Would it be possbile for me to send you an email with project note I am working on?
Thank you
Hi
This is GREAT!
I was wondering why you send back “r” if you’re not using the feedback buffer to anything else then kicking of a new command to the device?
Probably I was planning to implement some handshaking to catch communication errors but it was not necessary in the end, so don’t pay any attention to it
.
hi karel,
nice post, thanks for share. i just want to know how you connect the bluetooth module on robot and and androit module. they are using serial communication?
if it is correct, it should use baudrate, how about that?
Hi Deli
This is explained in the previous post: http://www.karelbruneel.com/blog/2011/01/android-controlled-robot/
Please let me know if this does not help.
Karel
hai,
as my last question, i had problem like this…
i design the mobile robot control over
android phone (Samsung galaxy, lg) —- EB500 — AVR (Atmega32)
when i used bluetooth dongle i can discover my EB500 but when i used android phone i can’t discover the EB500. if used another handset like GSM (Nokia, Sony Ericson) not android OS, i could connect to the EB500.
what seems problem with my android phone — EB500 connection?
note : i had 5 android phone to discover the EB500
thanks
dedi
Karel
I noticed there is a UUID specified in the Android code. Where/how did you determine what value to use and is there a corresponding value somewhere in the robot code?
-BillK
Hi BillK
That’s the Serial Port Service Class UUID. I cant remember exactly where I found it, but it is the standard UUID used by bluetooth modules that have a serial interface, like the one I have and many others. The code on the Dwengo board does not need to know about the UUID because it uses the serial interface to communicate with the bluetooth module. The bluetooth module uses the UUID somehow, but thats totally hidden from the user so you don’t have to worry about it.
Hope this helps!
Karel
hi karel,
i really need your help….
i still can’t discover my EB500 (bluetooth) why android phone can’t found that?
is that a different with other handset?
except android phone, for bluetooth working?
thanks
dedi
Hi Dedi,
I don’t know the answer to this one. Have you tried updating your phone? Which version of adroid is on it?
Karel
i have test using android Froyo 2.1, i don’t uderstand to what seems to be problem on it. is it the problem from the bluetooth type that android phone use ?
but if use GSM phone there is no problem
thanks
dedi
hi sir,
at last i think its because my android version. now i have update to ginger bread…. work perfectly…. i use samsung galaxy mini
thanks
dedi
Hey Dedi
I’m glad you fixed the problem. Sorry I couldn’t help.
Karel
I was wondering where you check that the received char is “r”? And can you please tell me how I simple can print the received char to the display like x,y,z-axis.
Hey Jason
Sorry for the long wait. I was on vacation.
In the code I don’t check wether the received character is ‘r’ so if you would change the code on the dwengo board so that it sends an other character everything will still work. If you would want to check if the character is really ‘r’ you would want to do that in the bluetoothWrite function.
You can’t write the receive character to the dwengo display since it is received by the phone and not the dwengo board.
Hope this helps!
Karel
Hey hi Karel,
What you have done is superb !! I want to do something like this only..Can u please send me a test mail at the provided email address..I want to know how to do this from scratch..especially the hardware stuff..please do reply..this is a project i have been assigned
What do you mean by test mail?
Ohh !! sorry , test mail would be just sending a “hi” or a “hello” message so that I could reply to it ..I couldn’t find your email-id on the blog so i asked..But please do send me a mail…I want to do a similar thing..can u just guide me regarding the hardware stuff, what are the things you purchased etc..i have many questions regarding hardware..Android application I understood properly ..Thanks !!
I think it’s better that I answer your questions here so that other people can also read the answers. Shoot!
ok fine here are my questions..These questions are very basic because I am a student and I don’t know much abt this so please bear with me:
1) assuming I know little about electronics and circuits and related stuffs..how do I first make the robot.. What I need a simple robot nothing fancy..just 4 wheels and a motor..also from where to do I get the Dwengo board ? What does the Dwengo board have on it ? Is it available everywhere in the world right now ? what’s the price in US $ ? 2) Are you using any sensors ..what is the name of those sensors,cost etc 3) after i get the board what do I need to do? How to test the Bluetooth on the board? 3) using ur android code i can use it as steering wheel..but if i want to add say 4 buttons on my android app for left,right,front,back can it be done too ?
That’s all for now !! I will shoot more as I learn more …Thanks !!
1a) You can build a robot using Lego for example.
1b) It’s better to build a robot that has two wheels each with a motor and then some third wheel to balance everything, like is shown here http://www.dwengo.org/tutorials/light-eating-robot
1c) The dwengo board has everything you need to build a simple robot. http://www.dwengo.org/board/functionality
1d) You can order the board from the site dwengo.org
1e) I don’t know the price in dollars
2) For this project I don’t use any sensors
3) just build the circuit on the breadboard as is shown in my first post http://www.karelbruneel.com/blog/2011/01/android-controlled-robot/ and load the code on the dwengo board and on your phone
4) It is certainly possible to add buttons to the android application.
I would suggest that you take a look at the tutorials at dwengo.org
I tried doing a Google Search but I don’t think I can get a Dwengo board in India ? More specifically in a city like Mumbai ..you know any other way to get it ?
I think you can buy a dwengo board on the dwengo.org site.
Thanks !! I feel the Dwengo board is way out of my budget..I won’t be able to buy it over here in India..I am trying to find something similar in the local electronic markets here..I feel if I can get a board or rather make one having a Bluetooth modem, necessary ports,get some wires , wheels and motors that should act as a replacement for the Dwengo board..As far as the bot is concerned I would just have those 4 wheels and a motor to drive them..What you feel should I do it this way? because I can’t find such a board over here
The only thing you need is a microcontroler with a UART port to communicate with Bluetooth module and a motor driver to control the motors. This should be easy to build on some experimentation board. You can base the circuit on the Dwengo board, just remove everything you don’t need. http://www.dwengo.org/nl/board/schematics You can also switch to a cheaper microcontroller you only need a UART port and 4 digital outputs to build the robot.
I don’t think that you will be able to build a Bluetooth module yourself, since you don’t have much electronics experience. I would recommend that you buy one.
Hi karel, I am just wondering why the application has to send the characters ‘s’ and ‘c’ to the bluetooth module? And also what size of bytes do I need to send if I will use buttons on the application?
When the s char is send to the robot it stops the motors. When the c char is send, this indicates that three bytes will be send x y z that represent the position of the phone relative to the earth.
I don’t really understand your second question. The size of a byte is always 8 bits.
Sorry about the second question, I only got confused, I am trying to use it on arduino. Great job on doing this, I am using your bluetooth classes as a lecture, thanks for your answer.
hi there, what a nice post you give =D
thanks before for sharing the code, your post really helps my college task…
i’m still a newbie in robotic things, and i make a project under your code guidance above, i still confuse with few parts in the code, so i’ll just make a few point of my question..
first, i tried to connect my project based on your code to hyperterminal, and it did connect (there are “s” character displayed)…
but…
*why the value of the accelerometer axis didn’t shown there?
*what should i do to display the axis there?
thanks before
-TJ-
Hey TJ
I guess you hooked up the bluetooth module to hyperterminal and installed the android program on your phone. The communication works as follows. Every time the robot sends a character to the phone, the phone reacts by sending either ‘s’ or ‘c’ followed by the three bytes for the x, y and z component of the gravitation vector. The ‘s’ char is send when the phone is set to stop mode. After you push the ‘start driving’ button the phone starts responding with c followed by the coordinates.
So the first thing you should do if you want to see the coordinates is to push the ‘start driving’ button. Afterwards if you write a character in hyperterminal the phone should replay with ‘c’ and the coordinates.
However, you will not see the coordinates as nice numbers. Each coordinate is send as one character this is a number between 0 and 255 this number is displayed as a character by hyperterminal. So if the coordinate 75 is send this is displayed by hyperterminal as ‘K’. the mapping between coordinate numbers and characters can be found in the ASCII table. see http://www.asciitable.com/
Cheers
Karel
hi, again…
thanks before the explanation above, i really appreciate it =)
I thought, this function[
public void bluetoothWrite(int bytes, byte[] buffer)] should send the value of accelerometer axis(x,y,z) to the hyperterminal when the
“Start Driving” button is pushed, am i right?
to make it simple, i just want to display the value of 3 accelerometer axis periodically to the hyperterminal,that’s all, could you help me with that?
thank you very much
Regards
-TJ-
The bluetoothWrite function is called every time the phone receives a character from the robot. In that way you get a some kind of handshake protocol.
For what you want to do you need to put the sending of the accelerometer axis in the onSensorChanged function. This function is called periodically, so the acceleration components will be send periodically and not in response to a request from the robot.
Hey Karel,
I am almost done with the my robot ..thought will let u know first !
Thanks for all the help !!
Great work! I’d love to see the result when you’re finished.
Already solved the problem, thanks for the explanation…
i want to know that can i use the code on any other board or by making a seprate board for the interfacing the moters and BT and keeping the same code
if u have a algorithm for the code then plz share it
If you have a board with the same microcontroller, it should be easy to use the same code. You can download the code on this page.
my idea is that using a same micro and make the connection according to the dwengo board
means only the required poarts and the pins that r connected to the BT and the moter
can i use any other BT modual for communication
That is also possible.
can i impliment the BT module and the interfacing to the moters by a seprate ckt
what is a ckt?
hey karel, I’m asking for your permission to use the bluetoothlistener classes for my school project (which is a robot & android trainer which will be used by students for free), I will also mention this site as one of my resources. I hope it’s all right with you.
Hey Gizmo
The software is under apache license. That basically means that you can do anything with it except remove the license and the copyright from the files. So go ahead and use it. The more people that use it the happier I become
.
If you have any website about the project. I would be very glad to take a look at it.
Cheers
Karel
Thanks a lot karel, I really appreciate this, this will be a great help for a lot of students. Cheers.
Good evening, I’am student and I have a project with android.
I like know if x, y, z is the command to robot.
Sorry my english, I from Brazil.
Hey Fernando,
Sorry for the late reply. x, y and z are the components of the gravitation vector with respect to the position of the phone. These components are send to the robot and the robot uses them to determine which action it should take (how to drive the motors).
Hope this helps
Karel
hi karel,
this is my project result using android like yours….. thanks for guide me….
http://www.youtube.com/watch?v=GiOR3ltu7HU
Cool!!!
Very well done!!!
can i implement it on arduino UNO board
for this what changes i must do to make it work
Hi!
This information has been great, but I have one question regarding data sent from the robot/terminal to the app, what should I change to NOT make the app answer with “s” directly but maybe wait for a termination-letter ( “;” for example). I would like to implement reading of speed, voltage etc and send it back to the application but I cannot resolve the direct “s”-reply issue.
My thought:
App: “s”
Rob: “r00120;” (ex. 00km/h 12.0 V)
App: “cjki”
Rob: “r03119;” (ex. 03km/h 11.9 V)
Best Regards Robert
Hi Robert
I think the key here is that you let the app only react to the termination-character, so in the bluetoothWrite-method you need something like:
mybuffer.append(buffer); //This is pseudocode. I don’t know how to do this right away.
if (mybuffer.last ==’;') {
// Do whatever you need to do with the data you send from the robot, which is in myBuffer
// The original code which sends the command to the robot.
} else {
// Do nothing. Wait for more data from the robot.
}
In the robot code you need to send the data
while(PIR1bits.TXIF==0) {}
TXREG = ‘r’;
for (ch in datastring[]) {
while(PIR1bits.TXIF==0) {}
TXREG = ch;
}
while(PIR1bits.TXIF==0) {}
TXREG = ‘;’;
Hope this helps.
Karel
Hello again!
Thank you very much for the help! It worked!
Here is how i solved it if someone is interested:
* In begining of file:
private String a = new String(“”);
* In public void bluetoothWrite(in…:
String b = new String(new byte[] {buffer[0]});
a += b;
if(a.charAt(a.length()-1)==’;'){
My code reading the string a, ex.”r00120;”
a = “”; //resets the temporary string a.
THE ORIGINAL CODE
}else{
//Do nothing!
}
Thanks again!
Best regards
Robert
I’m glad it helped!
Good luck with your project!
Hi Karel,
Wanted to say thanks for making your code available. I recently used much of it to build a Bluetooth Android to Arduino interface, and I learned a lot in the process. You can see a video of my project here: http://youtu.be/gJjkVGUha_M
Hi Greg
Wow!!! Extremely nice project!
Glad the code helped!
Karel
Hi Karel,
It’s working but ……..
How can I change the dwengo C sourcecode?
If I tilt the phone forward, the robot turns left. I want the robot drive forward.
If I tilt the phone backward,the robot turns left, the robot needs to drive backward.
If I turn left the robot, the robot drive backwards,eft and so on …
I think it must be possible to make the robot behave as I just described.
How can I change the code?
Theo
Karel,
I have find the answer and changed the code.
It’s working now !
My project RobotMowAndroidControl
Theo
Hey Theo
You probably just had to add a minus sign for one of the motor speeds. Right?
Karel
Hi Karel,
I thought also, but it didn’t work.
I replaced character z with y , and y with z.
in commands below:
int handelingMotor1(char x, char z, char y) and int handelingMotor2(char x, char z, char y).
This solved my problem.
Theo
Hi Theo
That is probably because the accelerometer in your phone is oriented differently in your phone. That is strange because I thought android OS normalises this. What kind of phone do you have?
Karel
Karel,
I have a HTC Desire HD.
Theo
Hi, I’m trying to use your x, y, z numbers to create a threshold to send values, 0,1,2,3,4 (since my robot will only have 1 speed). 0 = stop, 1 = reverse, 2 = forward, 3 = left, 4 = right. I’m using the max32 (pretty much the arduino) board to control my robot. I get no response from the robot. I tested this on LED’s as well and still no response. I am new to Android programming so please help me.
Thanks
K. Parikh
Hi,
Sorry for the late reply.
I would send the x, y and z to the robot and do the tresholding there.
Since you are using a different microcontroller, you will have to rewrite the robot code. I can’t help you with that since I have never worked with a max 32 before. Sorry
Good luck!
Karel
hi…
i think i am making the same project by developing an android application….but i am using Aubtm-22(bluetooth module) and AT mega32(microcontroller) for the robot…but i am facing problem in interfacing these two….so please, i would be very garetful to u….if u can send me the interfacing circuit diagram…of it..
thanks
karel
Hi Kundan
Sorry for the late reply.
I can’t help you with this, I don’t know your bluetooth module and also I don’t really know the mega32. The interface should be simular to what I’m doing in my previous post. Don’t forget to connect TX of the microcontroler to RX of the module and visa versa.
Good luck!
Karel
Hi Kundan,
you can connect your bluetooth module use serial port RX – TX,
connection like this :
MIKRO Bluetooth Module
Rx ——- Tx
Tx ——- Rx
done
hi karel
really nice app.
Thank you!
Hi Karel,
I have installed again android steering wheel. It’s working driving forward, left and right but ……..
but I can’t drive backwards, if I tilt the phone backward.
I have used original dwengo code.
Can you help me?
Problem has been solved