While working on my Arduino based robot vehicle project the idea came to me that the Bluetooth module that I’m using as a remote control receiver for my robot would make a great addition to my Raspberry Pi.
In this article I describe how to enable the Raspberry Pi’s serial port to talk to other devices over Bluetooth using this module.
The nice aspect of this project is that it requires very little hardware, most of the action happens in the software side. But of course we do need some hardware, which I list below:
- A Raspberry Pi.
If you have been living under a rock or in another planet for the last year or two and never heard of the Raspberry Pi then you should know that this is a credit card sized Linux computer created in the UK that sells for just $35 USD. I’ve got mine through Element 14.
- A Bluetooth slave module
This is a small and inexpensive Bluetooth to serial adapter that is mainly targeted to Arduino users. I’ve got mine from the Virtuabotix store at Amazon.com for $14.95. The Chinese electronics stores on Ebay sell it for less, but of course you have to wait longer to get it.
- Four female-to-female breadboard wires
If you buy the Bluetooth module from the Virtuabotix store at Amazon (link above) then you get the wires included with it. If you are buying from another place you may want to ask if wires are included or sold separately.
- A computer or device with a Bluetooth terminal software
To establish a communication with the Raspberry Pi over Bluetooth you need another device that can speak Bluetooth. If your computer has a Bluetooth adapter then you just need to find a terminal software that you can use to send and receive data, like HyperTerminal on Windows, or
screenon OS X and Linux. A computer is not the only choice, though. For example, I will use my Android cell phone with the free BlueTerm app installed.
The wiring is very simple and is better explained with a diagram:
|RPi GPIO pin||BT module pin|
|5V (Pin #2)||VCC|
|GND (Pin #6)||GND|
|TXD (Pin #8)||RXD|
|RXD (Pin #10)||TXD|
Note that the
RXD connections between the RPi and the Bluetooth module are crossed, this is what makes one end receive what the other end sends.
Raspberry Pi configuration
I’m going to assume you are running a recent release of Raspbian on your Raspberry Pi. If you are running another OS then you will need to find out how the changes below are done in your system.
By default the Raspberry Pi is configured to write boot time messages to the serial port, and also to start a login console on it. Unfortunately, the default baud rate that the RPi uses for its serial port is 115200 bps, while the Bluetooth module comes preconfigured from factory to 9600 bps.
It is easier to configure the RPi to use 9600 bps so we will try that first. There are two config files that need to be updated.
/boot/cmdline.txt contains the kernel options that are used to boot the system. In my Raspbian based system this file contains the following options:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
The interesting options are
kgdboc, because these configure the serial port device
/dev/ttyAMA0 to 115200 bps. You need to change these two configurations to 9600 bps. After you make these changes the file should read:
dwc_otg.lpm_enable=0 console=ttyAMA0,9600 kgdboc=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
The second configuration file is
/etc/inittab. Inside this file you have to locate the following line:
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
This tells the system to start a terminal on the serial port, and again it uses 115200 bps to configure the port. You have to change this line to use 9600 bps:
T0:23:respawn:/sbin/getty -L ttyAMA0 9600 vt100
Remeber that these are configuration files so they are not writable to the default
pi user, to edit these files you have to use
sudo. As far as text editors, Raspbian provides two,
pico. If you don’t know either then you will probably be more confortable using
pico. For example, to edit
pico you would run the following command:
$ sudo pico /boot/cmdline.txt
The system will ask for your password and after that you will be able to make changes to this file. I recommend that you save original copies of these config files in case you make a mistake.
With those changes made the RPi is configured to talk to the Bluetooth module. If you now power up your Raspberry Pi you will notice that the LED in the Bluetooth module blinks rapidly. This is the sign that the Bluetooth module is ready and waiting to be paired with another device.
Connecting from a Bluetooth terminal
Now leave the RPi running with the Bluetooth module in its blinking state and go to the Bluetooth enabled computer or smartphone that you will connect to it. Your device should now find the Bluetooth module with the name
linvor when you set it to discover devices.
If you are using an Android device with BlueTerm then start the app and from the app menu select “Connect device”. Android does the baud selection automatically so you don’t have to configure it. From a terminal software running in a computer it is likely that you will need to configure the speed, number of data bits per character, parity, and number of stop bits per character. The values you need to use are:
- Speed: 9600 bps
- Data bits: 8 bits
- Parity: None
- Stop bits: 1 bit
The Bluetooth module comes preconfigured with a PIN number. To complete the connection your computer or smartphone will ask you to enter this PIN. The factory default PIN is
The LED in the Bluetooth module will now stop blinking and remain lit, indicating that it has made a connection.
And here comes the fun part. You need to reboot the Raspberry Pi so that the new serial port settings take effect. To reboot the RPi run the following command in a local or network shell:
$ sudo reboot
Now watch the Bluetooth terminal on your PC or smartphone while the Pi reboots. Boot messages should be appearing on your terminal, and as soon as the RPi is up you should get a login prompt there as well.
You can now login from your Bluetooth terminal and use the command line prompt as you normally would over a local or network shell.
Below is a short video that shows my Raspberry Pi connected to my Android smartphone:
For more detail: A cheap Bluetooth serial port for your Raspberry Pi