Beställt på vardagar före 14:00 = skickas samma dag. 30 dagars ångerrätt

Kopplingsschema och kodexempel

Så här kopplar du CAN-busmodul MCP2515 SPI 5V till en Arduino UNO eller ESP32, med ett exempel­sketch som du kan ladda upp direkt.

CAN Bus Communication with MCP2515

Connects an MCP2515 CAN bus module to an Arduino Uno using hardware SPI. The sketch initializes the CAN controller at 500 kbps, sends a test CAN message every 2 seconds, and prints any received frames to the Serial Monitor.

Kopplingsschema: CAN-busmodul MCP2515 SPI 5V på en Arduino UNO
Kopplingsschema · Klicka för att förstora
Kopplingstabell: CAN-busmodul MCP2515 SPI 5V på en Arduino UNO
Kopplingstabell · Klicka för att förstora
CAN_Bus_Communication_with_MCP2515.ino · kompilerad och testad
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : CAN Bus Communication with MCP2515
 *  Board     : Arduino UNO (arduino:avr:uno)
 *  Parts     : CAN-bus module MCP2515 SPI 5V
 *  Libraries : mcp_can 1.5.1
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

#include <SPI.h>
#include <mcp_can.h>

const byte CAN_CS_PIN = 10;
const byte CAN_INT_PIN = 2;

MCP_CAN CAN(CAN_CS_PIN);

unsigned long lastSend = 0;

void setup() {
  Serial.begin(115200);
  pinMode(CAN_INT_PIN, INPUT);

  // Initialize MCP2515 with standard 8MHz crystal at 500 kbps
  if (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
    Serial.println(F("MCP2515 initialized successfully!"));
  } else {
    Serial.println(F("MCP2515 initialization failed!"));
    while (1);
  }

  CAN.setMode(MCP_NORMAL);
  Serial.println(F("CAN controller ready in normal mode."));
}

void loop() {
  // Check for received CAN messages
  if (CAN.checkReceive() == CAN_MSGAVAIL) {
    unsigned long rxId;
    byte len = 0;
    byte rxBuf[8];

    CAN.readMsgBuf(&rxId, &len, rxBuf);

    Serial.print(F("RX -> ID: 0x"));
    Serial.print(rxId, HEX);
    Serial.print(F(" DLC: "));
    Serial.print(len);
    Serial.print(F(" Data: "));
    for (byte i = 0; i < len; i++) {
      if (rxBuf[i] < 0x10) Serial.print('0');
      Serial.print(rxBuf[i], HEX);
      Serial.print(' ');
    }
    Serial.println();
  }

  // Send a test packet every 2 seconds
  if (millis() - lastSend >= 2000) {
    lastSend = millis();
    byte txData[4] = {0xDE, 0xAD, 0xBE, 0xEF};
    byte sndStat = CAN.sendMsgBuf(0x100, 0, 4, txData);

    if (sndStat == CAN_OK) {
      Serial.println(F("TX -> Sent frame (ID: 0x100, Data: DE AD BE EF)"));
    } else {
      Serial.println(F("TX -> Error sending message"));
    }
  }
}

Nödvändiga bibliotek: mcp_can 1.5.1

Wiring diagram and example code generated by Codey.online — https://www.codey.online

ESP32 CAN Bus Communication with MCP2515

This circuit connects an ESP32 to an MCP2515 CAN-bus module via SPI, using a 4-channel bidirectional level shifter to protect the ESP32's 3.3V logic from the 5V module. The sketch initializes the CAN controller at 500 kbps, sends a test CAN data frame every two seconds, and prints any received CAN bus messages to the Serial Monitor.

Kopplingsschema: CAN-busmodul MCP2515 SPI 5V på en ESP32 DevKit V1
Kopplingsschema · Klicka för att förstora
Kopplingstabell: CAN-busmodul MCP2515 SPI 5V på en ESP32 DevKit V1
Kopplingstabell · Klicka för att förstora
ESP32_CAN_Bus_Communication_with_MCP2515.ino · kompilerad och testad
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : ESP32 CAN Bus Communication with MCP2515
 *  Board     : ESP32 DEVKIT V1 (esp32:esp32:esp32doit-devkit-v1)
 *  Parts     : CAN-bus module MCP2515 SPI 5V
 *  Libraries : mcp_can 1.5.1
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

#include <SPI.h>
#include <mcp_can.h>

const int CAN_CS_PIN = 5;
MCP_CAN CAN0(CAN_CS_PIN);

void setup() {
  Serial.begin(115200);
  while (!Serial);

  // Initialize MCP2515 running at 8MHz clock and 500kbps CAN baud rate
  if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
    Serial.println("MCP2515 Initialized Successfully!");
  } else {
    Serial.println("Error Initializing MCP2515...");
  }

  CAN0.setMode(MCP_NORMAL);
}

void loop() {
  // Prepare sample data payload (8 bytes)
  byte data[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  
  // Send CAN message with standard ID 0x100
  byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
  if (sndStat == CAN_OK) {
    Serial.println("CAN Message Sent Successfully!");
  } else {
    Serial.println("Error Sending CAN Message...");
  }

  // Check for incoming CAN messages
  if (CAN0.checkReceive() == CAN_MSGAVAIL) {
    long unsigned int rxId;
    unsigned char len = 0;
    unsigned char rxBuf[8];
    
    CAN0.readMsgBuf(&rxId, &len, rxBuf);
    
    Serial.print("Received ID: 0x");
    Serial.print(rxId, HEX);
    Serial.print(" Data: ");
    for (byte i = 0; i < len; i++) {
      Serial.print("0x");
      if (rxBuf[i] < 0x10) Serial.print("0");
      Serial.print(rxBuf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }

  delay(2000);
}

Nödvändiga bibliotek: mcp_can 1.5.1

Wiring diagram and example code generated by Codey.online — https://www.codey.online

Produktsäkerhet
Tillverkare
OTRONIC
Meerweg 76A, 2121 VC BENNEBROEK, Nederland
support@otronic.nl
Ansvarig i EU
OTRONIC
Meerweg 76A, 2121 VC BENNEBROEK, Nederland
support@otronic.nl
Säkerhetsinformation
Läs produktinformationen och eventuell manual före användning. Elektroniska moduler och delar är avsedda för personer med teknisk kunskap; förvara dem utom räckhåll för barn. Använd endast angiven spänning och anslut eller koppla inte loss något medan kretsen är spänningssatt. Lämna uttjänt elektronik och batterier till en insamlingsplats; släng dem inte i hushållsavfallet.

CAN-busmodul MCP2515 SPI 5V

MCP2515 CAN-busmodulen är lämplig för Arduino och andra mikrocontrollerprojekt där du vill kommunicera med ett CAN-bussystem via SPI-protokollet.

Stöder CAN V2.0B med en kommunikationshastighet på 1 Mb/s.

Matningsspänning: 5V
Gränssnitt: styrbar med SPI
Mått: cirka 2,8 x 4,8 cm

Mått
Längd: 4cm
Bredd: 2,8cm
Höjd: 1,2cm

  • MCP2515
  • 5V-strömförsörjning
  • SPI-gränssnitt
  • 2,8 x 4,8 cm

Varför välja denna produkt?

  • För Arduino – Lämplig för Arduino-projekt.
  • CAN V2.0B – Stöder CAN V2.0B.
  • Hög hastighet – Kommunikationshastighet upp till 1 Mb/s.
  • SPI-gränssnitt – Styrs med SPI.
Specifikationer
Bredd2,8 cm
Längd4 cm
Matningsspänning5V
Höjd1,2 cm
CAN-versionV2.0B
Kommunikationshastighet1 Mb/s

Recensioner

Inga omdömen än.

Skriv ett omdöme
Ditt omdöme

Frakt & returer

Frakt

  • Paket med spårning85 kr5 arbetsdagar
  • Paket med spårning104 kr5 arbetsdagar
  • Paket med spårning124 kr5 arbetsdagar
  • Paket med spårning130 kr5 arbetsdagar
Allt om frakt

Returer

Inte nöjd? Du har 30 dagars ångerrätt efter leverans för att returnera produkten.

Allt om returer

Tidigare visade

MQ-8 Gassensor modul Vätgas

MQ-8 Gassensor modul Vätgas

29 kr
AD9850 DDS-signalgenerator-modul

AD9850 DDS-signalgenerator-modul

209 kr
Svänghjul för robot

Svänghjul för robot

18 kr
MQ-9 gassensormodul

MQ-9 gassensormodul

29 kr
Mikrokontroller ATMEGA328P-PU

Mikrokontroller ATMEGA328P-PU

119 kr