The main use case for this Feather is to use with CAN Bus. CircuitPython has the Adafruit_CircuitPython_MCP2515 module, which allows you to easily write Python code that lets you utilize the MCP25625 CAN bus controller on the Feather. In the examples below, you'll run a self-test for CAN Bus and then connect two RP2040 CAN Bus Feathers for back and forth CAN Bus communication.
CircuitPython Usage
To use with CircuitPython, you need to first install the MCP2515 library, and its dependencies, into the lib folder onto your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders:
- adafruit_bus_device/
- adafruit_mcp2515/
Simple Test Example
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries # # SPDX-License-Identifier: MIT from time import sleep import board from digitalio import DigitalInOut from adafruit_mcp2515.canio import Message, RemoteTransmissionRequest from adafruit_mcp2515 import MCP2515 as CAN cs = DigitalInOut(board.CAN_CS) cs.switch_to_output() spi = board.SPI() can_bus = CAN( spi, cs, loopback=True, silent=True ) # use loopback to test without another device while True: with can_bus.listen(timeout=1.0) as listener: message = Message(id=0x1234ABCD, data=b"adafruit", extended=True) send_success = can_bus.send(message) print("Send success:", send_success) message_count = listener.in_waiting() print(message_count, "messages available") for _i in range(message_count): msg = listener.receive() print("Message from ", hex(msg.id)) if isinstance(msg, Message): print("message data:", msg.data) if isinstance(msg, RemoteTransmissionRequest): print("RTR length:", msg.length) sleep(1)
In the code, the instantiation of can_bus
sets loopback
and silent
to True
. This allows you to test the CAN Bus messaging without another CAN device.
can_bus = CAN( spi, cs, loopback=True, silent=True ) # use loopback to test without another device
In the serial console, you'll see the byte array message be sent successfully every second.
Testing with Two CAN Bus Feathers
You can test with two CAN Bus Feathers by connecting the CAN Bus connection terminal blocks on both boards together.
- Feather A H terminal block to Feather B H terminal block (blue wire)
- Feather A middle (ground) terminal block to Feather B middle (ground) terminal block (black wire)
- Feather A L terminal block to Feather B L terminal block (yellow wire)
Upload the Project Bundle to both CIRCUITPY drives. In the code, change the can_bus
instantiation on both Feather's to have loopback
and silent
be False
.
can_bus = CAN( spi, cs, loopback=False, silent=False ) # use loopback to test without another device
When you run the code on both Feather boards, you can check each serial console window and see messages being exchanged between the two boards.
Text editor powered by tinymce.