1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32 + Arduino IDE + SN65HVD230でCAN通信をする

Last updated at Posted at 2025-10-02

CAN通信を試す

次の環境で試した。

  • マイコン (ELEGOO ESP-WROOM-32)
  • CANトランシーバー (SN65HVD230)
  • 開発環境 (Arduino IDE)

1 マイコン (ELEGOO ESP-WROOM-32)

使用するマイコン:ELEGOO ESP-WROOM-32

2 CANレシーバー (SN65HVD230)

3 Arduino IDE

使用したバージョン:2.3.6

インストール📌

Arduino IDEでESP32にプログラムを書き込めるようにするために、ボードマネージャーでESP32用とりあえず両方入れた。
image.png

ボード選択でESP32 Dev Moduleを選択する。
image.png

4 ピン配線

シンメトリーなピンアラインにします。

4.1 ESP32 <--> SN65HVD230

breadboard01-01.jpg

ESP32のピン SN65HVD230のピン
3V3 3.3V
GND GND
D21 CAN RX
D22 CAN TX

4.2 SN65HVD230 <--> SN65HVD230

本番環境であればCANHCANLはツイストペアとしますが、テストではスキップします。
breadboard02-02.jpg

SN65HVD230 ① SN65HVD230 ②
CANH CANH
CANL CANL

5 サンプルプログラム(暫定)

とりあえず、下記のプログラムをコピペしよう!

main.ino
#include <Arduino.h>
#include "driver/twai.h"

// 配線ピン(例): TX=GPIO22, RX=GPIO21
// ★ ピンは gpio_num_t 型で、値は GPIO_NUM_xx を使う
static const gpio_num_t TX_PIN = GPIO_NUM_22;
static const gpio_num_t RX_PIN = GPIO_NUM_21;

void setup() {
  Serial.begin(115200);
  delay(500);
  Serial.println("TWAI Sender start");

  twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_PIN, RX_PIN, TWAI_MODE_NORMAL);
  twai_timing_config_t  t_config = TWAI_TIMING_CONFIG_500KBITS();   // 500 kbit/s
  twai_filter_config_t  f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); // 受信フィルタ: 全受け

  if (twai_driver_install(&g_config, &t_config, &f_config) != ESP_OK) {
    Serial.println("Driver install failed");
    while (1) delay(1000);
  }
  if (twai_start() != ESP_OK) {
    Serial.println("Driver start failed");
    while (1) delay(1000);
  }
  Serial.println("TWAI started");
}

void loop() {
  // ID=0x123 のデータフレーム、8バイト送信
  twai_message_t msg = {};
  msg.identifier = 0x123;
  msg.extd = 0;          // 標準ID
  msg.rtr = 0;           // データフレーム
  msg.data_length_code = 8;
  uint8_t payload[8] = { 'H','e','l','l','o','C','A','N' };
  memcpy(msg.data, payload, 8);

  esp_err_t res = twai_transmit(&msg, pdMS_TO_TICKS(1000));
  if (res == ESP_OK) {
    Serial.println("TX OK: ID=0x123, 'HelloCAN'");
  } else {
    Serial.printf("TX FAIL: err=%d\n", (int)res);
  }

  delay(1000); // 1秒おきに送信
}

6 実行手順

  1. プログラムを書き込むボードを選択する。COM3とかCOM4とか。
  2. VerifyUploadを実行する。
    image.png

7 実行結果

次のように、選択した方のシリアルモニタでTX OK: ID=0x123, 'HelloCAN'が表示されていれば正しく動いている。もう片方のボードも選択して、シリアルモニタに同様に表示されているか確認してみよう!
image.png

8 Appendix

8.1 Arduino.hのある場所は?

C:\Users\ユーザ名\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.3.0\cores\esp32

8.2 twai.hのある場所は?

C:\Users\ユーザ名\AppData\Local\Arduino15\packages\esp32\tools\esp32-arduino-libs\idf-release_v5.5-b66b5448-v1\esp32\include\driver\twai\include\driver
1
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?