Raspberry pi pico board which is based on rp2040 microcontroller is very good option to learn python programming for kids using micropython. This board comes with all the required peripherals like UARTS, I2C and SPI masters etc, but this doesn’t have connectivity module (No WIFI or Bluetooth). In this article we will show how to interface UART Bluetooth module HC-05 and OLED display with pi pico board. This enables communication to this board with Smart phones, PCs over Bluetooth makes easier. This board has on board temperature sensor, this blog also explains how to read the temperature sensor and display OLED display.
Bluetooth Module connections( HC-05):
Lets now look the connection diagram, pi pico has two UART ports. UART1 is used for connecting HC-05 module. We can configure multiple pins to configure as TX and RX of UART1. We are using GP4-TX pin and GP5-RX pin (pin 6 and 7 on Raspberry pi pico board) for connecting HC-05 Bluetooth module. VCC, GND from BT module are connected to VSYS and GND pins. Other pins on HC-05 state and key pins left unconnected as we dont use AT commands mode.
OLED Connections:
We are using 128×64 OLED display module with I2C interface. SCL and SDA are connected to I2C port1 of Raspberry pi pico.
Pin 31 — I2C1_SDA
Pin 32– I2C1_SCL
VSYS and GND pins are connected to VCC and GND pins of display module.:
Two LEDs:
Two LEDs are connected to GP12 and GP13 GPIOs which are turned ON/OFF based on the commands received over Bluetooth from Android smartphone application.
Micropython Program:
We are using ssd1306.py from Micropython python examples located at
https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py
from machine import Pin, I2C, UART, Timer
from ssd1306 import SSD1306_I2C
import time
i2c=I2C(1,sda=Pin(26), scl=Pin(27), freq=400000)
uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
oled = SSD1306_I2C(128, 64, i2c)
temp_value= machine.ADC(4)
conversion_factor= 3.3 / (65535)
led1=Pin(12,Pin.OUT)
led2=Pin(13,Pin.OUT)
t_timer= Timer()
def ReceiveCmd():
cnt=0
rxData = bytes()
while cnt < 2:
if uart1.any() > 0:
rxData += uart1.read(1)
cnt=cnt+1
return rxData
def time_tick(timer):
temp_voltage= temp_value.read_u16() * conversion_factor
temp_celcius = round(27 - (temp_voltage - 0.706)/0.001721,3)
print("Temperature in Celcius: ",temp_celcius )
displaystr="Temp :"+ str(temp_celcius)
oled.fill(0)
oled.text(displaystr, 0, 0)
oled.show()
def main():
oled.text("C2P Labs", 0, 0)
oled.show()
data_received=bytes()
led1.value(1)
led2.value(1)
t_timer.init(freq=2, mode=Timer.PERIODIC, callback=time_tick)
while True:
data_received=ReceiveCmd()
cmd_value=data_received.decode()
if cmd_value == 'AA':
led1.value(1)
elif cmd_value == 'aa':
led1.value(0)
elif cmd_value=='BB':
led2.value(1)
elif cmd_value=='bb':
led2.value(0)
if __name__ == "__main__":
main()