Archive for August, 2011


Android arduino communication

Now to the fun part!

In the previous post I explained how to interface and configure a Bluetooth Module with a Bus Pirate.

Here I will show how to toggle the Arduinos LED through an Android app!

Note that I’m using a Arduino Duemilanove.

You’ll need to connect the module to the Arduino. The +5V and GND pins are already there. The digital 0 and 1 pins are reserved for the PC/Arduino interface through the FTDI.
I used the digital pins 2 and 3 for the communication with the module.

The rxPin(2) is connected to the tx pin of the module.
The txPin(3) is connected to the rx pin of the module.

Arduino Code
Available here:

I could have implemented it without the use of
NewSoftSerial but didn’t. The delay when reading was added because of the baud rate configuration problem I was having, at 1200 bauds. At 9600 it is perhaps not needed. There is no need to actually return if the message was understood, I added it to experiment on possible information polling events.

Here is a short python code I wrote for my Ubuntu to test the communication.

Python test code
Available here:
Ubuntu will automatically ask if you want to pair with the device. It usually is called linvor and has the pin 1234 as default.

I based my code on the examples found here: also finds your device by name and gives you it’s mac address)

As for the Android code, you must add the permissions for bluetooth connections to the Manifest.

<uses-permission android:name=”android.permission.BLUETOOTH” />
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN” />

BLUETOOTH_ADMIN is only needed if you are to set your device visible or manage paired devices.

My main and only java class is available here:
(wasn’t going to get indented here in WordPress)

It should be noted that after creating a socket with

UUID mUUID = UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);
BluetoothSocket mBTSock = device.createRfcommSocketToServiceRecord(mUUID);

which uses the default device UUID, you must still connect the socket.
It seems a socket cannot be reused after closing it. Probably would have to open a new socket.
Note that this implementation is far from being totally bug and idiot proof. I do the most of the error checks which probably would go wrong, but there are always more to add.

I used the DeviceListActivity from the BluetoothChat example available in the Android SDK. This class needs the res/layout/device_list.xml and res/layout/device_name.xml. I used it so the user can select a paired device from the list and it returns the Bluetooth address(mac) to the calling activity, so it knows to which device to connect.

Here is the whole project:

So, I hope this works for you. Will not run on Android Emulator(I think, since Bluetooth is not available) and was built/tested for android 2.1(SDK 7)

Sorry for the embedded images for the code. WordPress without decent plugins does not allow code to be displayed properly. And the android code was too long to paste together.


Bluetooth TTL module and Murphy

I bought a new Bluetooth Serial TTL module at eBay for a project of mine, which I will probably documenting here.

It is from MDFLY, as can be seen in the pictures below. The model is RF-BT0417CB.

Since at the time I had not yet received my Arduino 2009, a friend of mine suggested I test it with his Bus Pirate v3.5(BP for short).

He had bought one but never really used it, so there were the two of us, trying to get the BP to work. It is accessed through serial through USB, so to talk to it we discovered that screen does emulate serial connections!

screen /dev/ttyUSB0 115200

(BAUD rate does not need to be 115200, could be any other probably, and device may not be /dev/ttyUSB0… check your dmesg)

Obs.: screen is a Linux program, not Windows. Deal with it or install Cygwin. ‘Ctrl+a, k, y’ kills the screen so you can close it if you need to(probably will).

After connecting, screen is black. Hit ENTER to have ‘HiZ>’ appear. Now we need to set the mode that the BP will operate, since it has many available features.

1. HiZ
2. 1-WIRE
4. I2C
5. SPI
6. 2WIRE
7. 3WIRE
8. LCD
9. DIO
x. exit(without change)

Set serial port speed: (bps)
1. 300
2. 1200
3. 2400
4. 4800
5. 9600
6. 19200
7. 38400
8. 57600
9. 115200
10. BRG raw value

Data bits and parity:
1. 8, NONE *default
2. 8, EVEN
3. 8, ODD
4. 9, NONE
Stop bits:
1. 1 *default
2. 2
Receive polarity:
1. Idle 1 *default
2. Idle 0
Select output type:
1. Open drain (H=Hi-Z, L=GND)
2. Normal (H=3.3V, L=GND)

UART is so the BP can comunicate through serial, 9600 is normally the default speed, most of the rest are default options, and I prefer to use the ‘Normal’ output type because my Bluetooth module is TTL.

You will also need to connect the pins of the BP to the module:


Now we need to set the BP to power the module:

Power supplies ON

The modules LED should begin to blink. If it stays on steadily it means that the module is connected through Bluetooth to another device. If it is off.. recheck the previous steps.


This sets the BP to listen to incoming serial data. If there is a stream of data that should not be, it usually means that the TX is not connected properly to the MISO pin or that the module is turned off.

To test the communication with the device, send “AT” to it with:

READ: 0x4F
READ: 0x4B

The AT command should return “OK”(O=0x4F, K=0x4B).
If this doesn’t happen, recheck the TX->MISO connection or the BAUD rate for the module may be wrong. Try setting the BAUD rate to other values by redoing the mode setup.
Note: the ‘b’ command for the BP sets the BAUD rate for the pc side communication, not the module side.

Now comes the ‘problem’. Somewhere I read that the command to set/see the BAUD rate of the device was “AT+BAUD1”. This actually sets the BAUD rate to 1200. Great!. The recommended setting is “AT+BAUD4”, which will set it to 9600.

Second problem. The Bluetooth module is kind of picky when sending a ‘complex’ command. “AT” should work at almost any place, “AT+…” will probably not.
As I showed above, sending “AT” will return “OK” but in hex, not as chars. When using “AT+BAUD4” or “AT+VERSION”(check the version), the hex numbers representing the string will be shown, and not characters, which is a pain.
Also, when sending and receiving data through other devices, the data will be shown as hex, which normally isn’t very practical.

You can run the BP in Macro mode.

0.Macro menu
1.Transparent bridge
2. Live monitor
3.Bridge with flow control

2 and 3 can be used on to read. When pressing a key the exit the mode. So pretty useless for normal applications.
The ‘Transparent bridge’ mode connects you ‘directly’ to the module. In this mode you receive the characters normally, not in hex, but everything you write does not appear for you AND every char you send, is sent as you type, not after you press enter. So while “A” and then “T” will work and you will receive “OK” as an answer; “A”, “T”, “+”, and then the command will not work since apparently it takes to long for each char to reach the module, so the command is discarded and you don’t even receive the “OK” for the “AT” part.

The Author

Older Ramblings


Linux User Sig