Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

In the Raspberry Pi Bluetooth tutorial, we are going to control the GPIO pins of Raspberry Pi through the Android App over the bluetooth. I am going to use Raspberry Pi 3 because it have inbuilt bluetooth and you won’t need an external bluetooth device.

We are going to use the RFCOMM Bluetooth protocol for the communication between the Raspberry Pi and the Smart Phone. The RFCOMM (Radio Frequency Communication) bluetooth protocol is made on top of the L2CAP (Logical link control and adaption protocol) protocol, Provides emulated RS-232 serial ports and is also known as the serial port emulation. This protocol is widely used because it is simple to use and also because it uses the serial port to communicate with other devices.

Before going further, install the BlueTerm app from the play store in your smart phone.

BlueTerm

Installing Required Packages

First you need to update your Raspberry Pi. To update, type the below commands

sudo apt-get update
sudo apt-get upgrade

Then you need to install the packages related to Bluetooth. Blueman provides you the desktop interface to manage and control Bluetooth devices. Bluez is the official Bluetooth protocol stack and it supports all the core Bluetooth protocols.

sudo apt-get install bluetooth blueman bluez

Then install the python library for Bluetooth communication.

sudo apt-get install python-bluetooth

In the end, reboot your Raspberry Pi.

sudo reboot

Pairing the Device

  1. From the menu, go to preference and click on Bluetooth Manager. A new window will open up.
  2. Then from the Bluetooth options on your mobile, make your device visible.
    C:\Users\Muhammad\Downloads\WhatsApp Image 2018-01-20 at 11.07.10 PM (1).jpeg
  3. In the Bluetooth devices window, click on search. It will show all the devices in the range.
  4. Then right click on the device you want to connect and click on pair. It will ask for the permission on both the devices (Raspberry Pi and Smart Phone). Select yes on both sides.

Now your devices are paired and it’s time to make them communicate with each other.

Circuit Diagram

Connect the LED with the raspberry pi between the GPIO 21 and ground through the 220 ohm resistor. The positive of LED (longer leg of LED) should be connected to the GPIO 21 and connect the negative of led to the 220 ohm resistor and then connect the other end of resistor to the ground.

Raspberry Pi GPIO

Python Code

The python code is as follows

# Importing the Bluetooth Socket library
import bluetooth
# Importing the GPIO library to use the GPIO pins of Raspberry pi
import RPi.GPIO as GPIO

led_pin = 21	# Initializing pin 40 for led
GPIO.setmode(GPIO.BCM)	# Using BCM numbering
GPIO.setup(led_pin, GPIO.OUT)	# Declaring the pin 40 as output pin

host = ""
port = 1	# Raspberry Pi uses port 1 for Bluetooth Communication

# Creaitng Socket Bluetooth RFCOMM communication
server = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print('Bluetooth Socket Created')

try:
	server.bind((host, port))
	print("Bluetooth Binding Completed")
except:
	print("Bluetooth Binding Failed")

server.listen(1) # One connection at a time
# Server accepts the clients request and assigns a mac address. 
client, address = server.accept()
print("Connected To", address)
print("Client:", client)

try:
	while True:
		# Receivng the data. 
		data = client.recv(1024) # 1024 is the buffer size.
		print(data)
		
		if data == "1":
			GPIO.output(led_pin, True)
			send_data = "Light On "
		elif data == "0":
			GPIO.output(led_pin, False)
			send_data = "Light Off "
		else:
			send_data = "Type 1 or 0 "

		# Sending the data.
		client.send(send_data) 
except:
	# Making all the output pins LOW
	GPIO.cleanup()
	# Closing the client and server connection
	client.close()
	server.close()

After running this code in the Raspberry Pi, open the BlueTerm App, it will show you that no device is connected.

C:\Users\Muhammad\Downloads\WhatsApp Image 2018-01-20 at 11.07.10 PM.jpeg

Now touch the option button and connect with the Raspberry Pi.

C:\Users\Muhammad\Downloads\WhatsApp Image 2018-01-20 at 11.07.09 PM.jpeg

After connecting, if you will type ‘1’ or ‘0’ from the smart phone then the led on the Raspberry Pi will light up or light down.

If you have any questions, feel free to ask in the comment section.

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

4 Responses

  1. FRANK DELATORRE

    many thanks for this great tutorial. It worked the first time for me. I used it to test my BT connection. Next step is to create an App to replace the BT-term app.

    Reply
      • FRANK DELATORRE

        Aqib,
        How do I alter the python code to get the Bluetooth connection to persist even after the client is out of range (which disconnects) or the user disconnects on purpose? Otherwise I currently need to restart the script once my phone is out of range. I’d like the script to allow the disconnect but “reset” to be listing for another client to connect.

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar