import board
import busio
import digitalio
import adafruit_mcp2515
CS_PIN = 5
cs = digitalio.DigitalInOut(board.D5)
cs.direction = digitalio.Direction.OUTPUT
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
mcp = adafruit_mcp2515.MCP2515(spi, cs)
Hi thank u for repliyingWelcome to AAC.
This usage example is from the Adafruit website, and they have full API documentation here. You‘ll have to be more specific for someone to offer help.
What are you trying to do?Usage Example:import board import busio import digitalio import adafruit_mcp2515 CS_PIN = 5 cs = digitalio.DigitalInOut(board.D5) cs.direction = digitalio.Direction.OUTPUT spi = busio.SPI(board.SCK, board.MOSI, board.MISO) mcp = adafruit_mcp2515.MCP2515(spi, cs)
What have you tried?
What “didn’t work”?
First, which Arduino board are you using? You can use Circuit Python on more modern Arduino-made boards and reduce the confusion with a common code base.Hi thank u for repliying
I am trying to create a can bus communcation using mcp2515 between Raspberry pi pico and Arduino Uno i couldn't try something because arduino's mcp2515 libary look like makes job totally diffrent then adafruit's mcp2515 libary
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
#define CS_PIN 10
MCP2515 mcp2515(CS_PIN); //declare the MCP device and its chip select
void setup()
{
while (!Serial); //acquire serial interface & initialise
Serial.begin(115200);
//GPIO setup
//nothing needed. CS_PIN GPIO configured & enabled by MCP2515 library by default
//SPI setup
//nothing needed, SPI configured & enabled by MCP2515 library by default
//MCP2515 setup
mcp2515.reset();
mcp2515.setBitrate(CAN_250KBPS, MCP_8MHZ); //Sets CAN at speed 250KBPS and Clock 8MHz
mcp2515.setNormalMode();
}
void loop(){
//send a standard message
struct can_frame canMsgToSend; // declare a message structure
//prepare the message
canMsgToSend.id = 0x123; //11-bit ID for standard frame
canMsgToSend.data[0] = 'H'; // message data
canMsgToSend.data[1] = 'e';
canMsgToSend.data[2] = 'l';
canMsgToSend.data[3] = 'l';
canMsgToSend.data[4] = 'o';
canMgToSend.dlc = 5; //data length
//send it
mcp2515.sendMessage(&canMsgToSend);
//receive a message
struct can_frame canMsgToRecv; // declare a message structure
if (mcp2515.readMessage(&canMsgToRecv) == MCP2515::ERROR_OK) { // message waiting
Serial.print("Message ID: " ); Serial.print(canMsgToRecv.id, HEX);
Serial.print("Message length: "); Serial.print(canMsgToRecv.dlc, DEC);
for (int i = 0; i<canMsgToRecv.can_dlc; i++) { // print the data
Serial.print(canMsgToRecv.data[i],HEX);
Serial.print(" ");
}
Serial.println();
}
delay(100);//send messages approx 10 per sec.
}
import board
import busio
import digitalio
import adafruit_mcp2515
import time
#GPIO ssetup
CS_PIN = 5
cs = digitalio.DigitalInOut(board.D5)
cs.direction = digitalio.Direction.OUTPUT
#SPI setup
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
#MCP2515 setup
mcp2515 = adafruit_mcp2515.MCP2515(spi, cs) #defaults to 250kps & NormalMode
while true:
#send a standard message
#prepare the message
msgToSend = Message(id=0x123, data=b"Hello", extended=False)
#send it
mcp2515.send(msgToSend)
#receive a message
msgToRecv = mcp2515.read_message()
if msgToRecv != None:
print("Message ID ", hex(msgToRecv.id))
if isinstance(msgToRecv , Message):
print("message data:", msgToRecv.data)
time.sleep(0.1) #100mS
thank u sir i am a newbie and i am stugling a lotI'm not sure why you can't figure this out, newbie or not. Or is that you have no experience of using libraries associated with communication hardware? As Ya'akov said, its hard to advise you how to get somewhere without knowing where you are starting from!
In the simplest form, irrespective of library used, your code has to:
1. initialise the MCP2515 hardware
2a. prepare the transmit data and send the message; and/or
2b. prepare to receive a message, wait for it to arrive, then decode it
Initialising the hardware
The main difference between the libraries is configuring the hardware. In the Adafruit library there is one call to the constructor to configure the SPI chip select (CS) pin, CAN bus bitrate and a few other items that can be ignored and left to the default values. In the Arduino library only the CS pin is configured in the constructor, the bitrate is set separately in a subsequent call.
Depending on your hardware there may be other necessary calls, not part of the CAN Library, to select which SPI bus is being used for example.
Sending a message is just a case of filling in the packet data and ID and calling the sendmessage procedure. In the Arduino library, you fill in a message structure and pass it to the procedure. In the Adafruit library you pass the data as parameters to a procedure to create the packet. CANBus is broadcast system, the ID relates to the message-type not a device address - any device can listen for and act on a specific message-ID.
Receiving a message in both these examples uses polling to see if a message is waiting; if so, the message is read and processed. A high-performance real-time system would use interrupts for message reception, but that's too complex for here.
MCP2515 setup, Arduino-style:#include <SPI.h> //Library for using SPI Communication #include <mcp2515.h> //Library for using CAN Communication #define CS_PIN 10 MCP2515 mcp2515(CS_PIN); //declare the MCP device and its chip select void setup() { while (!Serial); //acquire serial interface & initialise Serial.begin(115200); //GPIO setup //nothing needed. CS_PIN GPIO configured & enabled by MCP2515 library by default //SPI setup //nothing needed, SPI configured & enabled by MCP2515 library by default //MCP2515 setup mcp2515.reset(); mcp2515.setBitrate(CAN_250KBPS, MCP_8MHZ); //Sets CAN at speed 250KBPS and Clock 8MHz mcp2515.setNormalMode(); } void loop(){ //send a standard message struct can_frame canMsgToSend; // declare a message structure //prepare the message canMsgToSend.id = 0x123; //11-bit ID for standard frame canMsgToSend.data[0] = 'H'; // message data canMsgToSend.data[1] = 'e'; canMsgToSend.data[2] = 'l'; canMsgToSend.data[3] = 'l'; canMsgToSend.data[4] = 'o'; canMgToSend.dlc = 5; //data length //send it mcp2515.sendMessage(&canMsgToSend); //receive a message struct can_frame canMsgToRecv; // declare a message structure if (mcp2515.readMessage(&canMsgToRecv) == MCP2515::ERROR_OK) { // message waiting Serial.print("Message ID: " ); Serial.print(canMsgToRecv.id, HEX); Serial.print("Message length: "); Serial.print(canMsgToRecv.dlc, DEC); for (int i = 0; i<canMsgToRecv.can_dlc; i++) { // print the data Serial.print(canMsgToRecv.data[i],HEX); Serial.print(" "); } Serial.println(); } delay(100);//send messages approx 10 per sec. }
MCP2515 setup, Adafruit circuitpython-style:import board import busio import digitalio import adafruit_mcp2515 import time #GPIO ssetup CS_PIN = 5 cs = digitalio.DigitalInOut(board.D5) cs.direction = digitalio.Direction.OUTPUT #SPI setup spi = busio.SPI(board.SCK, board.MOSI, board.MISO) #MCP2515 setup mcp2515 = adafruit_mcp2515.MCP2515(spi, cs) #defaults to 250kps & NormalMode while true: #send a standard message #prepare the message msgToSend = Message(id=0x123, data=b"Hello", extended=False) #send it mcp2515.send(msgToSend) #receive a message msgToRecv = mcp2515.read_message() if msgToRecv != None: print("Message ID ", hex(msgToRecv.id)) if isinstance(msgToRecv , Message): print("message data:", msgToRecv.data) time.sleep(0.1) #100mS
by Jake Hertz
by Robert Keim
by Don Wilcher
by Jake Hertz