Bluetooth HC 05 between Arduino and Raspberry Pi 3

Wiring HC 05 to Arduino

RPi GPIO pin BT module pin
3.5V (Pin #2) VCC
GND (Pin #6) GND
TXD (Pin #8) RXD
RXD (Pin #10) TXD

Code on Arduino

void setup() {
  Serial.begin( 9600 );    // 9600 is the default baud rate for the serial Bluetooth module
void loop() {
  // listen for the data
  if ( Serial.available() > 0 ) {
    // read a numbers from serial port
    int count = Serial.parseInt();
     // print out the received number
    if (count > 0) {
        Serial.print(“You have input: “);
        // blink the LED
void blinkLED(int count) {
  for (int i=0; i< count; i++) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin, LOW);

Install Bluetooth library on Raspberry

There are 2 ways to controll bluetooth connection on Pi.
Control by console command (bluetoothctl) and GUI (blueman).

Manually connect using  Bluetoothctl console command.

sudo apt-get install bluetooth bluez-utils
sudo apt-get install bluetooth-pi
sudo apt-get install bluez bluez-firmware

How to Setup Wi-Fi and Bluetooth on the Raspberry Pi 3

Enter bluetoothctl on terminal and use below command:

power on
agent on
scan on
pair xx:xx:xx:xx:xx:xx
connect xx:xx:xx:xx:xx:xx
trust xx:xx:xx:xx:xx:xx

Remember to use trust command to let PI auto establish this connection next time.

Connect by GUI

sudo apt-get install blueman
sudo reboot

Source code by Python on PI

There are 2 ways of reading data from Arduino using bluetooth connection. One is using socket and other is using serial connection.

Using socket:

import bluetooth
import time

bd_addr = “20:16:02:25:92:39”
port = 1
sock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)

while 1:
tosend = raw_input()
if tosend != ‘q’:
buffer = sock.recv(4096)
print buffer

More handy reading/writing data to socket could be refer from here :

Bluetooth communication between Raspberry PI and Arduino

Python on Pi:
import bluetooth
bd_addr = “98:D3:31:50:0A:CE” //The address from the HC05 sensor
port = 1
sock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)
data = “”
while 1:
data += sock.recv(1024)
data_end = data.find(‘\n’)
if data_end != 1:
rec = data[:data_end]
print data
data = data[data_end+1:]
except KeyboardInterrupt:
Arduino code:
//This sketch will use bluetooth to send temperature and humidity
// to a Raspberry PI using bluetooth
// Arduino -> bluetooth
#include <SoftwareSerial.h> //Serial library
#include <DHT.h>
#define DHTPIN 2  //Where is DHT connected
#define DHTTYPE DHT11  // Type of sensor used
* 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() {
  /* Since we run out of 5v PIN
  * and don’t wanna use a breadboard – the VCC of the DHT11
  * is connected to PIN 8
  * And we just use digitalWrite to put it HIGH
  pinMode(8, OUTPUT); //explained above why PIN 8
void loop() {
    float hh = getHumid();
    float tt = getTemp();
    bt.print (String(hh) + “,” + String(tt));
    delay (2000); //prepare for data (2s)
float getHumid() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
  return (float)dht.readHumidity();
float getTemp() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
  return (float)dht.readTemperature();

Using serial:

This requires addition step to setup connection point on PI. It looks like below but I haven’t make it success.

sudo nano /etc/bluetooth/rfcomm.conf

rfcomm1 {
    bind yes;
    device xx:xx:xx:xx:xx:xx;
    channel 1;
    comment “Connection to Bluetooth serial module”;
Here xx:xx:xx:xx:xx:xx is the address of Bluetooth module.
sudo rfcomm bind all
Source code:
import serial
from time import sleep
bluetoothSerial = serial.Serial( “/dev/rfcomm1”, baudrate=9600 )
count = None
while count == None:
        count = int(raw_input(Please enter the number of times to blink the L$
        pass    # Ignore any errors that may occur and try again
bluetoothSerial.write( str(count) )
print bluetoothSerial.readline()

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s