The purpose of this tutorial is to cover the basics of setting up a connection between Arduino Uno and an Android phone via Bluetooth. Smart phones pack a ton of cool features (camera, accelerators, speakers, microphone, a nice screen to show data from your sensors, wi-fi adaptor…) that will make an excellent addition to a robot, or any other Arduino project.
I will use Blueterm a basic free Android terminal emulator app to send a single digit (zero or one) from a phone to a BlueTooth module connected to the Arduino Uno via a serial connection. The Arduino Uno will read the data and will turn an LED on, or off accordingly. After that it will send a status message back, which will be displayed on the phone’s screen. This will essentially confirm we have a two way communication between the Arduino and the Android phone over Bluetooth. The same approach can be used to interact with anything connected to your Arduino (like motors, servos and sensors) or the cool goodies in your smartphone (camera, accelerometer etc.). So let’s get stared!
This is what you will need:
- Arduino Uno (or compatible board)
- A computer with latest version of the Arduino IDE installed
- An Android smartphone with Bluetooth capability
- JY-MCU Bluetooth Module. It is a cheap Bluetooth module that can be purchased from many retailers online, or from eBay. (I got mine from here)
- Some way of shifting down the voltage of the Arduino signal line from 5V to 3.3V. A couple of resistors will work fine. I use one 20K Ohm and one 10K Ohm, but other combinations will work as well. More on that below…
- The Blueterm free Android app so we have a simple way of sending commands to the Arduino from our phone.
Step 1: The Arduino Sketch
We need a simple sketch that will do the following:
- Establish a serial connection between the Arduino and the Bluetooth module
- Listen for input on the serial port and process it
- Turn the LED on pin 13 on, if it reads 1 (one) as serial input
- Turn the LED on pin 13 off, if it reads 0 (zero) as serial input
- Send “LED: on” or “LED: off” back to the Bluetooth module depending on the status of the LED
For simplicity, we will just be sending just the integers 1 or 0 from the phone as commands. If any other character is sent, it will be ignored. Upload the sketch below to your Arduino. By default the Bluetooth module I am using is configured to use the 9600 bps data rate. Here is link to our basic sketch
Step 2: hooking up the Bluetooth module to the Arduino Uno
The JY-MCU module communicates with the Arduino via a serial connection. It has four pins that we will be using:
- VCC is used to power the module. It needs to be connected to the Arduino 5v pin.
- GND is the ground pin. It needs to be connected to the Arduino Ground pin
- TXD is used to send data from the module to the Arduino. It needs to be connected to the serial receive pin (RX) of the Arduino, which is pin 0 in case of the Uno. If you are using a different Arduino board, check its schematics to make sure you have the right pin.
- RXD is used to receive data from the Arduino. It needs to be connected to the the Arduino serial transmit pin (TX) , which is pin 1 in the case of Arduino Uno.
- We will not be using the “STATE” and the “KEY” pins of the Bluetooth module, so they will not be connected.
Here is how my Bluetooth module looks like. There are several variations out there that work pretty much the same:
On my module, as you can see from the photo above, the TX line is rated at 3.3V. This means that even though we can power the module with 5 volts from the Arduino, the communication lines from and to the module are supposed to be at 3.3 volts.
Sending data from the Bluetooth module (via the module’s TX pin) will not be an issue, as the Arduino’s RX line will interpret the 3.3V signal from the Bluetooth module correctly.
Receiving data from the Arduino is where we need to do more work. The RX line of the Arduino Uno is running at 5V, so we will be sending a higher voltage on the Bluetooth’s module RX line, than suggested by the label. This may damage the Bluetooth module permanently!
Many online tutorials suggest that this is not an issue and the TX / RX lines of the JY-MCU Bluetooth module can be connected directly to the Arduino safely. I did not want to risk burning my Bluetooth module though in the first seconds of usage and to have to wait several weeks for a replacement to arrive in the mail. To be on the safe side, I used two resistors to create a voltage divider and drop the voltage of the Arduino TX line signal from 5 volts to approximately 3.3 volts.
I used a 20K Ohm and a 10K Ohm resistors, but you can substitute those for other values, as long as you get a Vout of about 3.3 volts based on the voltage divider equation below:
In the diagram above we have R1 = 10K Ohm and R2 = 20K Ohm and Vin = 5v. Solving for Vout we get: Vout = 5 x 20k Ohm / (20k Ohm + 10K Ohm) = 5v x 20k Ohm / 30k Ohm = 5V x 2 / 3 = 10V / 3 = 3.33 V.
Essentially, you can use any combination of resistors, as long as R2 is twice the value of R1.
If you do not have resistors handy, you can use a positive 3.3v voltage regulator. Connect the Input pin of the voltage regulator to the Arduino TX line, the Ground pin to the Arduino Ground and the output pin of the voltage regulator to the JY-MCU RX line.
The third option is to use a dedicated logic level converter board, like this one, by Sparkfun.
Step 3: Preparing the Android phone
- Install the Blueterm free Android app to your phone.
- Make sure that Bluetooth is active and enabled on your phone. Follow the settings menus on your phone, the Bluetooth settings should be under “Wireless and Networks” category.
Step 4: The fun part!
Finally it is time to test our set-up!
- Power up the Arduino Uno (and the Bluetooth module). My Bluetooth module has a red status LED that is blinking when the module is powered up, but not connected.
- Pair the module with your phone. The defaults for the Bluetooth module are:
Default Name: linvor
Default Pairing code: 1234
- Once you have successfully paired your phone and the BlueTooth module, open Blueterm.
- Connect to the Bluetooth module (default name linvor) from the Blueterm menu. You should see a message that the connection was successfully established. The red status LED on my Bluetooth module switches from a blinking to steady on, when it is connected.
- Type “1” within the Blueterm console. The built in LED on the Arduino Uno should turn on and you should see “LED: on” on the Blueterm console screen. Now type ‘0’ (zero). The LED should turn off and you should see the message “LED: off”.
Congratulations, you are done!
Next: Use SoftwareSerial with Bluetooth in order to make it easier to program your Arduino and debug your code when using the JY-MCU Bluetooth module.
- The first thing to check is the wiring between the Bluetooth module and the Arduino.
- Make sure that you have loaded the sketch from Step 1 on the Arduino, before you connected the Bluetooth module to your phone. You will not be able to use the Arduino IDE serial monitor, or upload sketches when the Bluetooth module is connected as it uses the same RX/TX lines. You can use the SoftwareSerial library to connect the Bluetooth module to any digital Arduino pin and keep the RX/TX line open.
- Confirm that Bluetooth is enabled and active on your phone by checking the settings.
- I had Blueterm crash on me a couple of times. Not really much to do here, except to open it up again and reconnect!