How can i use this library with a raspberry pi pico?
Hi, i wanted to know if it was possible to use this library with a raspberry pi pico with some tweaks. I've used it with arduino uno so far, and i'd like to use it on a raspberry pi pico aswell, obviously using the MCP2515. I'm asking this because the pi pico is now cheaper than an arduino nano. If nothing can be done then i guess i'll just buy a nano. Thanks in advance!
I have tested the library for communication between a Raspberry Pi Pico and an ESP32 DOIT DevKit V1. Raspberry Pi PICO is using MCP2515 CAN Controller Module while ESP32 is using SN65HVD230 CAN Transceiver Module. CANSender from ESP32 to CANReceiver on Raspberry Pi PICO example sketches can work, however it requires a minor modification to the library's MCP2515.h header file.
But before that, check which SPI pins your Raspberry Pi PICO board is using by default, this can be done easily using the code below while referring to the PICO pinout diagram:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1000);
Serial.print("MOSI: ");
Serial.println(MOSI, DEC);
Serial.print("MISO: ");
Serial.println(MISO, DEC);
Serial.print("SCK: ");
Serial.println(SCK, DEC);
Serial.print("SS: ");
Serial.println(SS, DEC);
}
void loop() {
// put your main code here, to run repeatedly:
}
Then proceed to wire up your PICO board to the MCP2515 module. Take note that some documentations stated that the MCP2515 can work at 3.3V but NO, the MCP2515 CAN Controller chip can work from 2.75V to 5.5V (https://ww1.microchip.com/downloads/en/DeviceDoc/MCP2515-Stand-Alone-CAN-Controller-with-SPI-20001801J.pdf) BUT the TJA1050 CAN Transceiver chip can only work from 4.75V to 5.25V (https://www.nxp.com/docs/en/data-sheet/TJA1050.pdf). So be sure to hook it to 5V.
You might also come across some forums stating that the Raspberry Pi PICO board GPIOs are not 5V tolerant and you should connect the pins to a logic level shifter first before connecting it to the MCP2515 module. For my case, the logic level shifter method did not work. Directly connecting the module to the SPI pins on the Raspberry Pi PICO board did the trick, although I am not sure how long the pins can continue to withstand the logic levels from the module, this requires an oscilloscope to verify (which I am too broke to own one D:, although a cheap one might do the trick ).
Coming back, navigate to where the CAN library is stored at (on Windows, it is under Documents > Arduino > libraries > CAN), then open the "MCP2515.h" header file. From line 15 to line 22
#if defined(ARDUINO_ARCH_SAMD) && defined(PIN_SPI_MISO) && defined(PIN_SPI_MOSI) && defined(PIN_SPI_SCK) && (PIN_SPI_MISO == 10) && (PIN_SPI_MOSI == 8) && (PIN_SPI_SCK == 9)
// Arduino MKR board: MKR CAN shield CS is pin 3, INT is pin 7
#define MCP2515_DEFAULT_CS_PIN 3
#define MCP2515_DEFAULT_INT_PIN 7
#else
#define MCP2515_DEFAULT_CS_PIN 10
#define MCP2515_DEFAULT_INT_PIN 2
#endif
replace this segment of code with
#if defined(ARDUINO_ARCH_SAMD) && defined(PIN_SPI_MISO) && defined(PIN_SPI_MOSI) && defined(PIN_SPI_SCK) && (PIN_SPI_MISO == 10) && (PIN_SPI_MOSI == 8) && (PIN_SPI_SCK == 9)
// Arduino MKR board: MKR CAN shield CS is pin 3, INT is pin 7
#define MCP2515_DEFAULT_CS_PIN 3
#define MCP2515_DEFAULT_INT_PIN 7
#elif defined(ARDUINO_RASPBERRY_PI_PICO)
#define MCP2515_DEFAULT_CS_PIN 17 // <------- This is your RPi PICO's default SPI Chip Select pin
#define MCP2515_DEFAULT_INT_PIN 8 // <------- Unused for my case, put any suitable GPIO pins you might need
#else
#define MCP2515_DEFAULT_CS_PIN 10
#define MCP2515_DEFAULT_INT_PIN 2
#endif
upload the CANReceiver sketch to your Raspberry Pi PICO board, hook everything up and enjoy :D.
As my application only requires transmission of CAN data from ESP32 to the Raspberry Pi PICO board, I am unable to verify if the reverse setup will work or not. Also I have only tested the example sketch so far and have yet to build stuff on top of it. II will update here if I face any issues when working with this library on Raspberry Pi PICO board. Appreciate if anyone can share their results here too.