Description
Board
ESP32 S2 mini Lolin / Wemos
Device Description
A mini wifi cards based on ESP32-S2FN4R2.
Hardware Configuration
In order to recover the canbus signal with an ESP32, it is advisable to use a transceiver of the type SN65HVD230 Can Board Connecting MCUs 1 to be placed at the level of the TX and RX pins of the board
Version
v2.0.8
IDE Name
IDE Arduino
Operating System
Windows 10
Flash frequency
40
PSRAM enabled
yes
Upload speed
115200
Description
Good morning
I am trying to retrieve information from the comfort canbus in 125kbit from a car using an ESP32 LOLIN S2 MINI + SN65HVD230 transceiver
Here is what I have already done:
-
I managed to read data from the car and find the correct frame id from canbus which I should use in my code. But the problem is that I managed this with a CANBed board from Seeed Studio CAN bus development kit for Arduino (ATmega32U4 with MCP2515 and MCP2551)
-
This board is based on an ATmega32U4 with the Arduino Leonardo bootloader coupled to an MCP2515 CAN bus controller and MCP2551 CAN bus transceiver
-
I used this code to retrieve the AC information I needed and it works perfectly
/////////////////////
// Libraries //
/////////////////////
#include <SPI.h>
#include <Wire.h>
#include <mcp2515.h> // https://github.com/autowp/arduino-mcp2515 + https://github.com/watterott/Arduino-Libs/tree/master/digitalWriteFast
/////////////////////
// Configuration //
/////////////////////
#define CS_PIN_CAN0 17
#define SERIAL_SPEED 115200
#define CAN_SPEED CAN_125KBPS // Entertainment CAN bus - Low speed
#define CAN_FREQ MCP_16MHZ // Switch to 8MHZ if you have a 8Mhz module
////////////////////
// Initialization //
////////////////////
MCP2515 CAN0(CS_PIN_CAN0); // CAN-BUS Shield N°1
////////////////////
// Variables //
////////////////////
String LeftTemp;
String RightTemp;
// CAN-BUS Messages
struct can_frame canMsgRcv;
void setup() {
// Initalize Serial for debug
Serial.begin(SERIAL_SPEED);
// CAN-BUS from car
Serial.print("Initialization CAN0");
CAN0.reset();
CAN0.setBitrate(CAN_SPEED, CAN_FREQ);
while (CAN0.setNormalMode() != MCP2515::ERROR_OK) {
delay(100);
}
}
void loop() {
// Receive CAN messages from the car
if (CAN0.readMessage( & canMsgRcv) == MCP2515::ERROR_OK) {
int id = canMsgRcv.can_id;
int len = canMsgRcv.can_dlc;
if (id == 464){
char tmp[3];
snprintf(tmp, 3, "%02X", canMsgRcv.data[5]);
LeftTemp = String (tmp);
Serial.print("LeftTemp = ");
Serial.println(LeftTemp);
Serial.println(tmp);
snprintf(tmp, 3, "%02X", canMsgRcv.data[6]);
RightTemp = String (tmp);
Serial.print("RightTemp = ");
Serial.println(RightTemp);
Serial.println(tmp);
Serial.println();
}
}
}
-
In order to recover the canbus signal from the car, it is advisable to use a trancreicever of the SN65HVD230 Can Board Connecting MCUs type to be placed at the level of the TX and RX pins of the card
-
So I bought the necessary components and made this assembly:
-
I then loaded the code for ESP32 which allows to process the canbus signal renamed TWAI
-
It obviously didn't work
-
By putting a delay of 5000 ms at the start of void setup() I have the initialization messages which go up correctly:
--- Driver installed
--- Driver started
--- CAN Alerts reconfigured -
Then I have the error message "Failed to receive message" which loops
I don't understand what is not working. I don't know if it's a software or hardware issue.
Sketch
#include "driver/gpio.h"
#include "driver/twai.h"
//#define ACCEPT_ID 0x036 //11 bit standard format ID;if not define any ID,accept all IDs.
#ifdef ACCEPT_ID
#define MASK_ID ~(0x7FF << 21) //32 bit standard format ID,but only care for the frist 11 bits.
#define TWAI_FILTER_CONFIG() {.acceptance_code = (ACCEPT_ID << 21),.acceptance_mask = MASK_ID,.single_filter = true};
#endif
// Pins used to connect to CAN bus transceiver:
#define RX_PIN 2
#define TX_PIN 1
// Intervall:
#define POLLING_RATE_MS 10
void setup() {
// put your setup code here, to run once:
delay (5000);
Serial.begin(115200);
twai_init();
}
void loop() {
// put your main code here, to run repeatedly:
twai_receive();
}
void twai_init()
{
//Initialize configuration structures using macro initializers
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);
//twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_LISTEN_ONLY);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS();
#ifdef ACCEPT_ID
twai_filter_config_t f_config = TWAI_FILTER_CONFIG();
#else
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
#endif
//Install TWAI driver
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
Serial.println("Driver installed");
} else {
Serial.println("Failed to install driver");
return;
}
//Start TWAI driver
if (twai_start() == ESP_OK) {
Serial.println("Driver started");
} else {
Serial.println("Failed to start driver");
return;
}
}
void twai_receive()
{
//Wait for message to be received
twai_message_t message;
if (twai_receive(&message, pdMS_TO_TICKS(POLLING_RATE_MS)) == ESP_OK) {
// Serial.println("Message received");
Serial.println("ID DATA----------->");
Serial.print(message.identifier, HEX);
if (!(message.rtr))
{
for (int i = 0; i < message.data_length_code; i++)
{
Serial.print(" ");
Serial.print(message.data[i], HEX);
}
}
Serial.println(" ");
} else {
Serial.println("Failed to receive message");
return;
}
}
Debug Message
Compilation OK
Other Steps to Reproduce
- I tested SoftwareSerial communication by putting TX on pin 1 and RX on pin 2 and it worked.
- I tested this with the following code which reads in the serial monitor the message sent by TX.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 1); // RX, TX
void setup()
{
delay (5000);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Native USB only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
- By entering a text in the console then "ENTER", when I connect RX and TX then I have the message that is displayed on the serial console.
- If I unplug the connection between RX and TX then I have nothing coming up in the console.
- Conclusion, there is indeed a serial link via TX and RX on an ESP32-S2 Lolin min
I carried out other tests by now defining in input TX = 39 and RX = 37 as defined in my original code.
-
Here is the wiring used. Connecting the TX pin to the RX pin
-
Conclusion, it also works so the TX / RX serial link pins are well configurable.
-
Another remark noted, obviously the serial link does not work on the TX and RX pins if the baud exceeds 100,000. So with a baud of 115,200 it does not work.
-
Last observation, the definition of the RX / TX pins in the pins_arduino.h file does not change anything in this code. The definition of pins in the "SoftwareSerial" object takes precedence over the definition of pins in the pins_arduino.h file
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.
Activity
SuGlider commentedon May 17, 2023
Please set the Log output to Verbose and run the application. You can report here the serial console output.
SuGlider commentedon May 17, 2023
The documentation about TWAI for IDF 4.4 (used with Arduino Core 4.4) is here:
https://docs.espressif.com/projects/esp-idf/en/v4.4.4/esp32s2/api-reference/peripherals/twai.html
SuGlider commentedon May 17, 2023
There is information about TWAI peripheral in the S2 TRM in the chapter 29:
https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf
SuGlider commentedon May 17, 2023
A few projects that use Canbus with ESP32... it may help.
https://github.com/MagnusThome/RejsaCAN-ESP32
https://github.com/miwagner/ESP32-Arduino-CAN
Related issues:
#3921
https://forum.arduino.cc/t/esp32-waveshare-sn65hvd230-can/1089185/2
SuGlider commentedon May 17, 2023
I don't have such hardware... sorry for not helping further.
Strangistra commentedon May 22, 2023
Hi SuGlider
Sorry for the late reply but all this information does not solve my problem.
I tested the ESP32-S2 with an MCP2515 module with SPI interface and this solution works fine.
In fact I don't understand either why the SN65HVD230 module doesn't work.
After several researches I found that many people give up the can bus on ESP32 after the passage in TWAI library. In CAN library formerly used on the first ESP32 it worked perfectly but not with the new models.
Strangistra commentedon May 22, 2023
This is the case with your mentioned issue #3921
designer2k2 commentedon May 26, 2023
if you run the TWAI example, does it output CAN frames to Serial? Or what does it output?
You will have to change the Bitrate to fit:
https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/TWAI/TWAIreceive/TWAIreceive.ino
(i made this example and here is my working setup with a SN65HVD230 : #7430 (comment)
VojtechBartoska commentedon Aug 18, 2023
any updates @Strangistra?
Strangistra commentedon Aug 18, 2023
/Hello everyone. I will come back to give you news of my investigations concerning this subject.

After buying the Adafruit CAN Pal which never arrived I acquired these modules: TECNOIOT 5pcs MCP2551 High Speed Can Protocol Controller Bus Interface Module
I then made the following connection:

I defined the RX and TX pins in the "pins_arduino.h" file located in the following path C:\Users\XXXX\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.9\variants\lolin_s2_mini
I then downloaded the original code of the ESP32 from canbus namely the example "ESP32 TWAI receive example" available in the ESP32 examples:

After testing the code on the car... miraculously I received the frames from the canbus.
Conclusion :
the configuration of the outputs of the RX and TX pins works well on the ESP32 S2 mini Lolin board
I do not know if all of my old SN65HVD230 canbus cards were fakes or if the card is not compatible with this type of material in any case they did not work for me
MCP2551 based canbus modules work perfectly
and finally, the TWAI example of the ESP32 actually also works in order to acquire the frames of a car
I hope that all of this information will be useful to someone else who finds themselves in the same situation as me.
See you soon for new experiences
3 remaining items