six-9のブログ

おっさんのブログ。エロゲとかアニメとか。

CAN通信モジュールとArduino

MCP2515 CAN通信モジュールをArduino Uno Rev3で試してみた。

インターフェース2024年3月号とかトランジスタ技術2024年1月号のArduino Uno Rev4の特集*1で何度か取り上げられていたので、気になっていた。仕事でCAN通信を使った機材に触る機会がちょこちょこあり、その理解のためにも触ってみたかった。

参考サイト

おうちでできるCAN通信(Arduino+MCP2515+sigrok) #can - Qiita

Arduinoから2つのCANモジュール同士を通信させる。結線とプログラムの参考にした。

購入したCAN通信モジュール

MCP2515 CAN通信モジュールはAmazonから購入した。SPI通信でマイコンから制御できるCAN通信モジュール。2個で700円くらい。安い。 https://www.amazon.co.jp/dp/B091YNRJZG?ref=ppx_yo2ov_dt_b_fed_asin_title

CAN通信モジュールの回路図

CAN通信モジュールの回路図
CANコントローラーとしてMCP2515が、CANトランシーバーとしてTJA1050が搭載されている。8MHzの水晶が載っていて、5VDC電源で駆動。電源接続のLEDもついている。J1を短絡すると終端抵抗120Ωとなる。J1開放時は終端抵抗無限大。J3がCANHとCANLのCAN通信端子。J2は外部抵抗などで60Ωなどの設定をするために使用するのだろう。

実験風景

実験風景
ブレットボードを利用して2つのCANモジュールを接続させた。使用したArduinoはArduino Uno Rev3。J1の短絡用のジャンパーピンが用意できなかったので、ブレットボード上で短絡させている。配線は上に示した参考URL*2の通り。

プログラム

CAN通信確認用Arduinoスケッチ
// MCP2515 CAN Send and Recieve Test

#include <mcp_can.h> //https://github.com/coryjfowler/MCP_CAN_lib
#include <SPI.h>

unsigned long rxId;
byte len;
byte rxBuf[8];
byte txBuf0[8];
byte txBuf1[8];
byte txBuf2[8];
long Pre_millis;
static int count = 0;

MCP_CAN CAN0(10); // CAN0 CS: pin 10
MCP_CAN CAN1(9);  // CAN1 CS: pin 9

void setup() {
  Serial.begin(115200);
  
  // init CAN0 bus, baudrate: 250kbps@8MHz
  if (CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ) == CAN_OK) {
    Serial.println("CAN0: Init OK!");
    CAN0.setMode(MCP_NORMAL);
  } else {
    Serial.println("CAN0: Init Fail!");
  }

  // init CAN1 bus, baudrate: 250kbps@8MHz
  if (CAN1.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ) == CAN_OK) {
    Serial.println("CAN1: Init OK!");
    CAN1.setMode(MCP_NORMAL);
  } else {
    Serial.println("CAN1: Init Fail!");
  }

  Pre_millis = millis();
}

void loop() {
  if (millis() - Pre_millis > 1000) { // Period: 1000ms
    // Fill txBuf0, txBuf1, txBuf2 with increasing values based on count
    for (byte i = 0; i < 8; i++) {
      txBuf0[i] = (count + i) & 0xFF;  // txBuf0 with values from count to count+7
      txBuf1[i] = (count + i + 8) & 0xFF; // txBuf1 with values from count+8 to count+15
      txBuf2[i] = (count + i + 16) & 0xFF; // txBuf2 with values from count+16 to count+23
    }

    // Send CAN messages
    CAN0.sendMsgBuf(0x100, 0, 8, txBuf0);
//    CAN0.sendMsgBuf(0x101, 0, 8, txBuf1);
//    CAN0.sendMsgBuf(0x102, 0, 8, txBuf2);

//    Serial.print("Send ID: 0x100, 0x101, 0x102 ");
    Serial.print("Send ID: 0x100 ");
    Serial.println();
    Serial.print("Data: ");
    for (byte i = 0; i < 8; i++) {
      Serial.print(txBuf0[i], HEX);
      Serial.print(" ");
    }
    Serial.println();

    count++; // Increment count for the next iteration
    Pre_millis = millis();
  }
  
  if (CAN1.checkReceive() == CAN_MSGAVAIL) {
    CAN1.readMsgBuf(&rxId, &len, rxBuf);
    Serial.print("Receive ID: ");
    Serial.print(rxId, HEX);
    Serial.print(" Data: ");
    for (byte i = 0; i < len; i++) {
      Serial.print(rxBuf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }

  // Add sorting to ensure IDs are printed in ascending order
  // Use a simple algorithm to sort the received IDs.
  static unsigned long lastRxId = 0;
  if (rxId != lastRxId) {
    lastRxId = rxId; // Update to the last received ID
  }
}

URLのサイトを参考に、ChatGPT(無課金)に頼んでカウントアップするように変更した。正常に通信できているか確認する目的。通信の遅延などがあれば、連番の数字が飛ぶからわかるかと考えた。

出力結果

Entering Couccessful!
CAN1: Init OK!
ng Baudrate Successful!
CAN0: Init OK!
Entering Configuration Mode Successful!
Setting Baudrate Successful!
CAN1: Init OK!
Entering Configuration Mode Successful!
Setting Baudrate Successful!
CAN0: Init OK!
Entering Configuration Mode Successful!
Setting Baudrate Successful!
CAN1: Init OK!
Send ID: 0x100 
Data: 0 1 2 3 4 5 6 7 
Receive ID: 100 Data: 0 1 2 3 4 5 6 7 
Send ID: 0x100 
Data: 1 2 3 4 5 6 7 8 
Receive ID: 100 Data: 1 2 3 4 5 6 7 8 
Send ID: 0x100 
Data: 2 3 4 5 6 7 8 9 
Receive ID: 100 Data: 2 3 4 5 6 7 8 9 
Send ID: 0x100 
Data: 3 4 5 6 7 8 9 A 
Receive ID: 100 Data: 3 4 5 6 7 8 9 A 
Send ID: 0x100 
Data: 4 5 6 7 8 9 A B 
Receive ID: 100 Data: 4 5 6 7 8 9 A B 
Send ID: 0x100 
Data: 5 6 7 8 9 A B C 
Receive ID: 100 Data: 5 6 7 8 9 A B C 
Send ID: 0x100 
Data: 6 7 8 9 A B C D 
Receive ID: 100 Data: 6 7 8 9 A B C D 
Send ID: 0x100 
Data: 7 8 9 A B C D E 
Receive ID: 100 Data: 7 8 9 A B C D E 
Send ID: 0x100 
Data: 8 9 A B C D E F 
Receive ID: 100 Data: 8 9 A B C D E F 
Send ID: 0x100 
Data: 9 A B C D E F 10 
Receive ID: 100 Data: 9 A B C D E F 10 
Send ID: 0x100 
Data: A B C D E F 10 11 
Receive ID: 100 Data: A B C D E F 10 11 
Send ID: 0x100 
Data: B C D E F 10 11 12 
Receive ID: 100 Data: B C D E F 10 11 12 
Send ID: 0x100 
Data: C D E F 10 11 12 13 
Receive ID: 100 Data: C D E F 10 11 12 13 
Send ID: 0x100 
Data: D E F 10 11 12 13 14 
Receive ID: 100 Data: D E F 10 11 12 13 14 
Send ID: 0x100 
Data: E F 10 11 12 13 14 15 
Receive ID: 100 Data: E F 10 11 12 13 14 15 
Send ID: 0x100 
Data: F 10 11 12 13 14 15 16 
Receive ID: 100 Data: F 10 11 12 13 14 15 16 
Send ID: 0x100 
Data: 10 11 12 13 14 15 16 17 
Send ID: 0x100 
Data: 11 12 13 14 15 16 17 18 
Send ID: 0x100 
Data: 12 13 14 15 16 17 18 19 
Send ID: 0x100 
Data: 13 14 15 16 17 18 19 1A 
Send ID: 0x100 
Data: 14 15 16 17 18 19 1A 1B 
Send ID: 0x100 
Data: 15 16 17 18 19 1A 1B 1C 
Send ID: 0x100 
Data: 16 17 18 19 1A 1B 1C 1D 
Send ID: 0x100 
Data: 17 18 19 1A 1B 1C 1D 1E 
Send ID: 0x100 
Data: 18 19 1A 1B 1C 1D 1E 1F 
Send ID: 0x100 
Data: 19 1A 1B 1C 1D 1E 1F 20 
Send ID: 0x100 
Data: 1A 1B 1C 1D 1E 1F 20 21 
Receive ID: 100 Data: 12 13 14 15 16 17 18 19 
Receive ID: 100 Data: 11 12 13 14 15 16 17 18 
Receive ID: 100 Data: 10 11 12 13 14 15 16 17 
Send ID: 0x100 
Data: 1B 1C 1D 1E 1F 20 21 22 
Receive ID: 100 Data: 1B 1C 1D 1E 1F 20 21 22 
Send ID: 0x100 
Data: 1C 1D 1E 1F 20 21 22 23 
Receive ID: 100 Data: 1C 1D 1E 1F 20 21 22 23 
Send ID: 0x100 
Data: 1D 1E 1F 20 21 22 23 24 
Receive ID: 100 Data: 1D 1E 1F 20 21 22 23 24 
Send ID: 0x100 
Data: 1E 1F 20 21 22 23 24 25 
Receive ID: 100 Data: 1E 1F 20 21 22 23 24 25 
Send ID: 0x100 
Data: 1F 20 21 22 23 24 25 26 
Receive ID: 100 Data: 1F 20 21 22 23 24 25 26 
Send ID: 0x100 
Data: 20 21 22 23 24 25 26 27 
Receive ID: 100 Data: 20 21 22 23 24 25 26 27 
Send ID: 0x100 
Data: 21 22 23 24 25 26 27 28 
Receive ID: 100 Data: 21 22 23 24 25 26 27 28 
Send ID: 0x100 
Data: 22 23 24 25 26 27 28 29 
Receive ID: 100 Data: 22 23 24 25 26 27 28 29 
Send ID: 0x100 
Data: 23 24 25 26 27 28 29 2A 
Receive ID: 100 Data: 23 24 25 26 27 28 29 2A 
Send ID: 0x100 
Data: 24 25 26 27 28 29 2A 2B 
Receive ID: 100 Data: 24 25 26 27 28 29 2A 2B 
Send ID: 0x100 
Data: 25 26 27 28 29 2A 2B 2C 
Receive ID: 100 Data: 25 26 27 28 29 2A 2B 2C 
Send ID: 0x100 

Sendした8バイトのデータがReceiveされている。 Data: 10 11 12 13 14 15 16 17 から Data: 1A 1B 1C 1D 1E 1F 20 21 までは2つのCANモジュールの配線を外した状態。通信が途絶えて、送信だけはできているが受信できていないことがわかる。

まとめと今後の展望

簡単だけどCANモジュールで遊んだ結果をまとめた。8バイトしかデータを送れないけれど、ロバストさには定評があるから、ノイズの多い工場内や機器内の配線に使用されているようだ。 Arduino Uno Rev4にはCANコントローラーが搭載されているので、CANトランシーバーのみ追加すればCAN通信が実現できるようだ*3。今回使用したCANモジュールのCANトランシーバーはTJA1050が使用されていたが、AmazonでCANトランシーバーモジュールを検索していたら、TJA1051のモジュールがあった。CANコントローラーとCANトランシーバーが載ったCANモジュールも2個で700円で、トランシーバーだけでも同じくらいの値段だから、正直、買うならCANモジュールのほうが得な気もする。Arduino Rev4の動作確認くらいでしか使い道がないかな。 https://www.amazon.co.jp/Youmile-JK-YM-377-2%E5%80%8B3V%E3%80%9C5V-CJMCU-1051-TJA1051CAN%E3%83%88%E3%83%A9%E3%83%B3%E3%82%B7%E3%83%BC%E3%83%90%E3%83%BC%E9%AB%98%E9%80%9F%E4%BD%8E%E6%B6%88%E8%B2%BB%E9%9B%BB%E5%8A%9B%E3%83%88%E3%83%A9%E3%83%B3%E3%82%B7%E3%83%BC%E3%83%90%E3%83%BC%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB1Mbits%E3%80%81Arduino%E7%94%A8DuPont%E3%82%B1%E3%83%BC%E3%83%96%E3%83%AB%E4%BB%98%E3%81%8D/dp/B09TKZZT9M