This post is part I of a III part series of posts related to Arduino and Raspberry PI Wireless Bluetooth communications.

For a project I’m working on, I was searching for a method of sharing information between the Arduino and a Raspberry PI and Bluetooth came to mind. So, I start exploring.

Here’s a small example of a Wireless connection between Arduino and an Android Phone using Bluetooth.

Hardware

1x Arduino (I’m using UNO but any other will work)

1x Android phone with BlueTerm app installed

1x HC-05 or HC-06 bluetooth sensor

HCs
HC-06 and HC-05

Connections

arduino Bluetooth

The connections are straight forward. Bare in mind that, we need to switch the RX and TX. It doesn’t matter which is which in the Arduino PINs, as long as we switch them in the code.

HC-05 or HC-06 Arduino PIN
VCC VCC 5v
GND GND
RX 6 (will be the TX in the code)
TX 5 ( will be the RX in the code)

The connections
The connections

Code

To program a bluetooth sensor with the Arduino is nothing more than use a serial connection where we send and receive commands. The bluetooth sensor will take care of sending and receiving the data, forwarding it to the serial connection.

//This sketch shows how to connect to a bluetooth device
// Arduino -> bluetooth
// Used with Android BlueTerm app

#include <SoftwareSerial.h> //Serial library

/**
 * Arduino connection HC-05 connection: 
 * HC-05  | Arduino
 * TX     | 5
 * RX     | 6
*/
// Here, we exchange them -
SoftwareSerial bt (5,6);  //RX, TX (Switched on the Bluetooth - RX -> TX | TX -> RX)
int LEDPin = 13; //LED PIN on Arduino
int btdata; // the data given from the computer

void setup() {
  bt.begin(9600); //Open the serial port
  bt.println ("Bluetooth ON. Press 1 or 0 to blink LED.."); //write to serial
  pinMode (LEDPin, OUTPUT);
}

void loop() {
  if (bt.available()) { //if serial is available
    btdata = bt.read(); //read from the serial connection
    if (btdata == '1') {
      //if 1
      digitalWrite (LEDPin, HIGH); //if we get 1, turn on the LED
      bt.println ("LED ON!");
    }
    if (btdata == '0') { //if we received 0, turn off the led
      //if 0
      digitalWrite (LEDPin, LOW);
      bt.println ("LED OFF!");
    }
  }
  delay (100); //prepare for data
}

Upload the code to the Arduino.

After uploading/connecting the Arduino, the LED on the Bluetooth Module will be blinking fast. After pairing with the phone, the LED will blink slowly.

On the Android phone, please install the BlueTerm application. This application will allow serial communication using the Bluetooth.

Now, turn on the bluetooth and pair with the module. Either is the HC-05 or HC-06 modules, the code will be 1234.

Open the application and connect to the paired device.

Once paired, start sending 0s or 1s to see the LED turning on or off.

Bluetooth-1
BlueTerm window – not connected

Connecting to the module
Connecting to the module – HC-06 appears as BT UART

Bluetooth-3
After connecting

Sending commands
Sending commands

ArduinoLED13
The LED will turn on or off according to the commands given by the phone

More information

Arduino Playground

Instructable by Mohannad Rawashdeh