Pi Pico Wireless Part 1

Firstly pressed on jalanjalantok.wordpress.com

We know that Pi Pico is completely an offline microcontroller. It does not have any onboard wifi/bluetooth capabilities when offered. To some enthusiast this might be a critical point knowing that ESP32 (or older brother ESP8266) has everything needed to cater any physical hardware programming and IoT projects at around the same price point.

In this first part of article, i would like to share how we could add wireless capability to the Pi Pico with Bluetooth module HC-05. The communication between both modules are made via UART (=Universal Asynchronous Receiver-Transmitter), which is a hardware device for asynchronous serial communication in which the data format and transmission speeds are configurable.

Pi Pico has two UART device. UART0 uses Pin 0 (TX) and Pin 1 (RX), and UART1 uses Pin 4(TX) and Pin 5 (RX). Use jumper cable to connect HC-05 to Pico UART 1 as illustrated below, you may use UART0 as well at your convenience.

The VCC uses VSYS although VBUS will deliver also the same 5V to the module. According to Pico datasheet VBUS value input voltage from the USB port, while VSYS is the system input that powers all GPIO pins. VSYS voltage ranges from 1.8-5.5 volts. Take note that HC-05 does not work with 3.3V input.

As the power flows into HC-05, the status led will blink rapidly as there is no connection made to the module. Therefore we need to pair HC-05 with another device. Bluetooth enables two devices to exchange data via radio communication. In this testing purpose, the other device I use is Raspberry Pi 4 with onboard bluetooth.

To initiate the connection type below line in the command line. Change the XX below with the MAC Address of the HC-05 module. If you work in desktop environment, MAC address of bluetooth can be easily recognized by scanning the bluetooth device. If you work in command line environment, make use of scan command inside bluetoothctl. Let’s assume you have populated the MAC address.

Note: Raspberry Pi might be able to pair and trust to the HC-05, but not getting the serial connection to it. Therefore we need to activate the rfcomm manually from the command line. Type command below to do it.

sudo rfcomm connect hci0 XX:XX:XX:XX:XX:XX

When asked, the default pair key of HC-05 is 1234 then press enter. Visually, an established connection is indicated by steady blink HC-05 led status. With the serial connection established, we need to write script to let Pico (client) and Pi 4 (host) to communicate wirelessly.

Script at Host-side

Create script at Pi 4 (host computer) to transmit message through rfcomm0 serial port where HC-05 is linked to.

import serial
import time

bts = serial.Serial("/dev/rfcomm0", baudrate=9600, timeout=0.5)

count=0
message='Message from Pi #'
while True:
    print(message,count)
    bts.write(str.encode('{} {}'.format(message,count)))
    count+=1
    
    time.sleep(1)

The output of the script is transmitted message which will be appear as the following:

Message from Pi # 0
Message from Pi # 1
Message from Pi # 2
Message from Pi # 3

Script at Client-side(Pi Pico)

Remember that HC-05 uses Pi Pico UART 1, thus make sure it is specified in the script.

from machine import Pin, UART
import time

uart = UART(1, 9600)

while True:
    print(uart.readline())
    time.sleep(1)

Run the code and message transmitted from the host will be captured.

b'Message from Pi # 0'
b'Message from Pi # 1'
b'Message from Pi # 2'
b'Message from Pi # 3'

Once the message gets across, it is then up to us to use it to control other device or to pull data from the sensor and send it back to the host.

The great thing about Python and Micropython is that they are exactly the same. If you want to reverse the direction (Pico transmit, Pi 4 receives) then we just need the same function from serial library to transmit the data and read the received lines.

Bluetooth is great for local wireless, low-power communication between host and client. If you have skill to develop an Android/IOS app, you could make one to control relays or collect sensor reading from a distance.

I hope this article is useful, thank you for reading, until next time.

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s