It's easy to use the CAN Bus FeatherWing with CircuitPython and the Adafruit_CircuitPython_MCP2515 module. This module allows you to easily write Python code that lets you utilize the MCP2515 CAN bus controller.
CircuitPython Microcontroller Wiring
A Feather RP2040 can connect to the CAN Bus FeatherWings by plugging them both into a FeatherWing Doubler:
Plug the Feather RP2040 into one slot on the doubler and then plug the CAN Bus FeatherWing into the remaining slot. This will connect all of the FeatherWing pins to the Feather RP2040.
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 import busio from digitalio import DigitalInOut from adafruit_mcp2515.canio import Message, RemoteTransmissionRequest from adafruit_mcp2515 import MCP2515 as CAN cs = DigitalInOut(board.D5) cs.switch_to_output() spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 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 REPL, you'll see the byte array message be sent successfully every second.
Testing with Two CAN Bus FeatherWings
You can test with two CAN Bus FeatherWings by preparing two FeatherWing Doublers with Feather RP2040s and CAN Bus FeatherWings.
Once you have two Feather RP2040's connected to CAN Bus FeatherWings, you can connect the CAN signals:
- FeatherWing A CANH terminal block to FeatherWing B CANH terminal block (blue wire)
- FeatherWing A - (ground) terminal block to FeatherWing B - (ground) terminal block (black wire)
- FeatherWing A CANL terminal block to FeatherWing B CANL terminal block (yellow wire)
Upload the Project Bundle to both CIRCUITPY drives. In the code, change the can_bus
instantiation on both Feather RP2040'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 REPL window and see messages being exchanged between the two boards.
Text editor powered by tinymce.