How to connect raspberry pi with HC-05 bluetooth module + arduino programm

The hardware

  • Raspberry pi 3b+ (it has inbuilt Bluetooth)
  • Arduino micro
  • HC-05 Bluetooth module

System requirements

  • arduino-cli
  • picocom
  • python 3

Configurations for Bluetooth module HC-05

  • Configure the module as master
  • Set PIN

Configurations for Raspberry Pi

Configurations for arduino

  • Install avr boards
  • Make physical connections
  • Copy arduino code and upload to arduino board

HC-05 Setup

$ echo -e "AT\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0
OK
$ echo -e "AT+NAME=ARDUINOBT\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0
OK
$ echo -e "AT+PSWD=1379\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0
OK
$ echo -e "AT+UART=115200,1,0\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0
OK

Raspberry Pi Setup

$ sudo apt install -y pi-bluetooth bluetooth bluez picocom blueman python3-pip
ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now bluetooth
echo "rfcomm" | sudo tee -a /etc/modules-load.d/modules.conf

Pair with HC-05 module

$ sudo bluetoothctl
[bluetooth]# agent on
[bluetooth]# scan on
Discovery started
[NEW] Device 98:D3:31:50:4A:C1 98-D3-31-50-4A-C1
[CHG] Device 98:D3:31:50:4A:C1 LegacyPairing: no
[CHG] Device 98:D3:31:50:4A:C1 Name: ARDUINOBT
[CHG] Device 98:D3:31:50:4A:C1 Alias: ARDUINOBT
[bluetooth]# scan off
[CHG] Controller B8:27:EB:80:2D:06 Discovering: no
Discovery stopped
[bluetooth]# pair 98:D3:31:50:4A:C1
Attempting to pair with 98:D3:31:50:4A:C1
[CHG] Device 98:D3:31:50:4A:C1 Connected: yes
Request PIN code
[agent] Enter PIN code: 1379
[CHG] Device 98:D3:31:50:4A:C1 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device 98:D3:31:50:4A:C1 ServicesResolved: yes
[CHG] Device 98:D3:31:50:4A:C1 Paired: yes
Pairing successful
[CHG] Device 98:D3:31:50:4A:C1 ServicesResolved: no
[CHG] Device 98:D3:31:50:4A:C1 Connected: no
[bluetooth]# trust 98:D3:31:50:4A:C1
[CHG] Device 98:D3:31:50:4A:C1 Trusted: yes
Changing 98:D3:31:50:4A:C1 trust succeeded
[bluetooth]# exit
$ sudo rfcomm bind rfcomm0 <device's MAC>
$ sudo pip3 install pyserial
$ python pythonClient/client.py

Arduino Setup

Physical connections

  • GND -> GND Arduino
  • VCC -> VCC Arduino
  • RX -> D14 Arduino
  • TX -> D15 Arduino
  • EN -> N/C

Compile and upload the sketch

$ arduino-cli compile -b arduino:avr:micro -u -p /dev/ttyACM0 arduinoBT-HC05/

Test Communications

Raspberry pi output example

developer@raspberrypi:~ $ python pythonClient/client.py
Enter your message below.
Insert "exit" to leave the application.
You message >> hi from raspberry pi
Server Response >> hi
You message >> test connection
Server Response >> test connection from arduino
You message >> exit

Arduino Output example

$ picocom -b 115200 /dev/ttyACM0
Terminal ready
Server response: hi from raspberry pi
You response -> hi
Server response: test connection
You response -> test connection from arduino
Terminating...
Skipping tty reset...
Thanks for using picocom

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ivan Moreno

Ivan Moreno

Engineer || MSc student || DevOps in progress