You've loaded CircuitPython on your board. Perhaps you've tried out a few of the CircuitPython Essentials examples. That's great and all, but isn't the whole point of this board to send and receive radio packets? Yes! Of course, we wouldn't leave you without a simple radio example.
Below, you'll find two demos: a Send Demo and a Receive Demo. To play along, you'll need to have two Feather RP2040 RFM69 boards. You'll load each demo onto a separate board. Then, when you press the Boot button on the Sender Feather, it will light up the NeoPixel on the Receiver Feather! This page will walk you through the code and how to use it.
Load the Code and Libraries
You'll need to copy the code and the necessary libraries to each of your Feathers. Choose one to be the sender and one to be the receiver.
Save each of the following examples to your CIRCUITPY drives as code.py.
Click the Download Project Bundle button above each example to download the necessary libraries and the applicable code.py file in a zip file. Extract the contents of the zip file, find your CircuitPython version, and copy the matching code.py file and lib/ folder to your CIRCUITPY drive.
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """ CircuitPython Feather RP2040 RFM69 Packet Receive Demo This demo waits for a "button" packet. When the first packet is received, the NeoPixel LED lights up red. The next packet changes it to green. The next packet changes it to blue. Subsequent button packets cycle through the same colors in the same order. This example is meant to be paired with the Packet Send Demo code running on a second Feather RP2040 RFM69 board. """ import board import digitalio import neopixel import adafruit_rfm69 # Set up NeoPixel. pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) pixel.brightness = 0.5 # Define the possible NeoPixel colors. You can add as many colors to this list as you like! # Simply follow the format shown below. Make sure you include the comma after the color tuple! color_values = [ (255, 0, 0), (0, 255, 0), (0, 0, 255), ] # Define radio frequency in MHz. Must match your # module. Can be a value like 915.0, 433.0, etc. RADIO_FREQ_MHZ = 915.0 # Define Chip Select and Reset pins for the radio module. CS = digitalio.DigitalInOut(board.RFM_CS) RESET = digitalio.DigitalInOut(board.RFM_RST) # Initialise RFM69 radio rfm69 = adafruit_rfm69.RFM69(board.SPI(), CS, RESET, RADIO_FREQ_MHZ) color_index = 0 # Wait to receive packets. print("Waiting for packets...") while True: # Look for a new packet - wait up to 5 seconds: packet = rfm69.receive(timeout=5.0) # If no packet was received during the timeout then None is returned. if packet is not None: print("Received a packet!") # If the received packet is b'button'... if packet == b'button': # ...cycle the NeoPixel LED color through the color_values list. pixel.fill(color_values[color_index]) color_index = (color_index + 1) % len(color_values)
The contents of the CIRCUITPY drive on your receiver Feather should resemble the following.
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """ CircuitPython Feather RP2040 RFM69 Packet Send Demo This demo sends a "button" packet when the Boot button is pressed. This example is meant to be paired with the Packet Receive Demo code running on a second Feather RP2040 RFM69 board. """ import board import digitalio import keypad import adafruit_rfm69 # Set up button using keypad module. button = keypad.Keys((board.BUTTON,), value_when_pressed=False) # Define radio frequency in MHz. Must match your # module. Can be a value like 915.0, 433.0, etc. RADIO_FREQ_MHZ = 915.0 # Define Chip Select and Reset pins for the radio module. CS = digitalio.DigitalInOut(board.RFM_CS) RESET = digitalio.DigitalInOut(board.RFM_RST) # Initialise RFM69 radio rfm69 = adafruit_rfm69.RFM69(board.SPI(), CS, RESET, RADIO_FREQ_MHZ) while True: button_press = button.events.get() if button_press: if button_press.pressed: rfm69.send(bytes("button", "UTF-8"))
The contents of the CIRCUITPY drive on your sender Feather should resemble the following.
RFM69 Radio Demo Usage
Once you have both Feathers set up and running, it's time to engage with the demo! First, connect to the serial console for the Receiver Feather.
The Receiver Feather will print to the serial console once the software starts up.
Now press the Boot button on the Sender Feather. Each time you press it, you'll see another message printed to the serial console.
You may have noticed the NeoPixel LED on the Receiver also lit up when you pressed the button on the Sender. That's the fun part of this demo!
On the first button press from the Sender, the NeoPixel LED on the Receiver turns red. On the second button press, the LED turns green. On the third press, the LED turns blue. As you continue to press the button, it cycles through the colors, beginning again with red.
Now you'll learn about what's going on in the code, and how to customise the NeoPixel colors.
Code Walkthrough
Red, green and blue are classic LED colors. The NeoPixel LED on your Feather is an RGB LED, meaning it contains three tiny LEDs inside of it that light up red, green or blue. Combined, these colors can make any color of the rainbow!
What if you wanted to expand the colors that the NeoPixel lights up in this demo? Turns out it's pretty simple.
NeoPixel Color Customisation
If you look at the example code, you'll find a list called color_values
following the NeoPixel set up. As the code is written, the list looks like this.
color_values = [ (255, 0, 0), (0, 255, 0), (0, 0, 255), ]
The tuples contained within the list are RGB color values. If you want to know more details about this concept, check out the RGB LED Colors section of the NeoPixel LED page in this guide. The important part to understand is that the three values within each tuple represent one of three colors: red, green and blue, in that order. The possible values are 0 to 255. This number determines what amount of each color is present. In the color list above, you see that the first entry is (255, 0, 0)
. This means there is maximum red, and no green or blue. The same applies to the other two, for the other two colors.
The plan here is to add another color to the list. We're going to add yellow to the bottom of the list. To make yellow using light, you combine red and green. Therefore, the tuple for yellow is (255, 255, 0)
. To add yellow, you add the new tuple, on a newline, followed by a comma, at the end of the list. The new list would look like this.
color_values = [ (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), ]
Add this to the code on your Receive Feather. Once everything has reloaded and is up and running, try pressing the Boot button on the Send Feather four times. On the fourth time, instead of red, you should see yellow!
That's all there is to adding more colors to the LED color list. You can add as many as you like as long as you follow the same formatting. You can make a button controlled sequential rainbow!
Receive Demo Details
This section will cover the Receive code.
First, you import the necessary modules and libraries.
import board import digitalio import neopixel import adafruit_rfm69
Next, you set up the NeoPixel and set the brightness to half. After that you define the color values.
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) pixel.brightness = 0.5 color_values = [ (255, 0, 0), (0, 255, 0), (0, 0, 255), ]
Next, you set up the RFM69 radio module.
RADIO_FREQ_MHZ = 915.0 CS = digitalio.DigitalInOut(board.RFM_CS) RESET = digitalio.DigitalInOut(board.RFM_RST) rfm69 = adafruit_rfm69.RFM69(board.SPI(), CS, RESET, RADIO_FREQ_MHZ)
Before the loop, you set the color_index
to 0
, and print the code status.
color_index = 0 print("Waiting for packets...")
Inside the loop, you begin looking for new packets. If no packets are received within 5 seconds, it returns None
. If a packet is received, it prints the status. If the packet string is b'button'
, then it fills the NeoPixel LED with a color. The last line is how the code cycles through the color list.
while True: packet = rfm69.receive(timeout=5.0) if packet is not None: print("Received a packet!") if packet == b'button': pixel.fill(color_values[color_index]) color_index = (color_index + 1) % len(color_values)
Send Demo Details
This section will cover the Send code.
First, you import the necessary modules and libraries.
import board import digitalio import keypad import adafruit_rfm69
Next you set up the button.
button = keypad.Keys((board.BUTTON,), value_when_pressed=False)
The RFM69 module set up is identical to the Receive code.
RADIO_FREQ_MHZ = 915.0 CS = digitalio.DigitalInOut(board.RFM_CS) RESET = digitalio.DigitalInOut(board.RFM_RST) rfm69 = adafruit_rfm69.RFM69(board.SPI(), CS, RESET, RADIO_FREQ_MHZ)
Inside the loop, the first thing you do is begin looking for button events. When a button event occurs, if that event is a button-press specifically, you send a packet containing the string button
.
while True: button_press = button.events.get() if button_press: if button_press.pressed: rfm69.send(bytes("button", "UTF-8"))
That's all there is to sending and receiving packets using the Feather RP2040 RFM69 microcontroller board!
Text editor powered by tinymce.