Step 1: Download the MicroPython UF2 file from the link below
https://www.raspberrypi.com/documentation/microcontrollers/micropython.html
Download the UF2 file which has Wi-Fi and Bluetooth LE support.
Step 2: Put the UF2 file in your raspberry pi pico w
- Push and hold the BOOTSEL button and plug your Pico into the USB port of your Raspberry Pi or other computer. Release the BOOTSEL button after your Pico is connected.
- It will mount as a Mass Storage Device called RPI-RP2.
- Drag and drop the MicroPython UF2 file onto the RPI-RP2 volume. Your Pico will reboot. You are now running MicroPython.
- You can access the REPL via USB Serial.
Step 3: Save the following files in your raspberry pi pico w
As you can see in the image, these files should be saved on the raspberry pi pico w. As they will be imported as modules when you write your application code.
[ Click on the file names to see the complete code ]
Step 4: Create a simple Bluetooth UART Code
which receives the message “Toggle\r\n” and Toggle the onboard LED and sends the state of the led back to the user.
# Import necessary modules
from machine import Pin
import bluetooth
from ble_simple_peripheral import BLESimplePeripheral
# Create a Bluetooth Low Energy (BLE) object
ble = bluetooth.BLE()
# Create an instance of the BLESimplePeripheral class with the BLE object
sp = BLESimplePeripheral(ble)
# Create a Pin object for the onboard LED, configure it as an output
led = Pin("LED", Pin.OUT)
# Initialize the LED state to 0 (off)
led_state = 0
xWR_flag = 0
# Define a callback function to handle received data
def on_rx(data):
print("Data received: ", data) # Print the received data
global led_state,xWR_flag # Access the global variable led_state
if data == b'toggle\r\n': # Check if the received data is "toggle"
led.value(not led_state) # Toggle the LED state (on/off)
led_state = 1 - led_state # Update the LED state
xWR_flag = 1
# Start an infinite loop
while True:
if sp.is_connected(): # Check if a BLE connection is established
sp.on_write(on_rx) # Set the callback function for data reception
if xWR_flag == 1:
# Create a message string
msg="LED STATE: "
# Send the message via BLE
sp.send(msg)
sp.send(str(led_state))
sp.send(str("\r\n"))
xWR_flag = 0
Leave a Reply