Hey all,
I've been banging my head against a problem for some time and I need some help.
I am trying to build an external automotive gauge using Arduino, ESP32 and CAN BUS.
I have a MegaSquirt auto ECU which can broadcast CAN messages and I have the CAN H and CAN L pins connected to an SN65HVD230 transceiver's CAN H and CAN L respectively.
The CAN transceiver connects to the ESP32-S3's TX and RX pins, the 3.3v and the gnd.
The below code is what I am using to initiate the transceiver and receive messages.
Unfortunately, I only get " No CAN message received."
I'm hoping someone might be able to take a look and see if I'm doing something wrong or point me back on track. I'm blind at this point.
#include <driver/twai.h>
// Define CAN TX and RX pins
#define CAN_TX_PIN GPIO_NUM_43
#define CAN_RX_PIN GPIO_NUM_44
void setup() {
Serial.begin(115200);
while (!Serial); // Wait for the Serial Monitor to connect
// Configure CAN settings
twai_general_config_t general_config = TWAI_GENERAL_CONFIG_DEFAULT(CAN_TX_PIN, CAN_RX_PIN, TWAI_MODE_NORMAL);
twai_timing_config_t timing_config = TWAI_TIMING_CONFIG_500KBITS(); // Set CAN bus speed to 500 kbps
twai_filter_config_t filter_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); // Accept all CAN messages
// Install the TWAI driver
if (twai_driver_install(&general_config, &timing_config, &filter_config) == ESP_OK) {
Serial.println("CAN driver installed successfully.");
} else {
Serial.println("Failed to install CAN driver.");
while (1); // Halt if driver installation fails
}
// Start the TWAI driver
if (twai_start() == ESP_OK) {
Serial.println("CAN driver started.");
} else {
Serial.println("Failed to start CAN driver.");
while (1); // Halt if driver fails to start
}
}
void loop() {
// Structure to hold received CAN message
twai_message_t rx_message;
// Wait for a CAN message with a timeout of 1 second
if (twai_receive(&rx_message, pdMS_TO_TICKS(1000)) == ESP_OK) {
Serial.print("Received CAN message: ");
Serial.print("ID=0x");
Serial.print(rx_message.identifier, HEX);
Serial.print(" DLC=");
Serial.print(rx_message.data_length_code);
Serial.print(" Data=");
// Print each byte of the received data
for (int i = 0; i < rx_message.data_length_code; i++) {
Serial.print(rx_message.data[i], HEX);
Serial.print(" ");
}
Serial.println();
} else {
Serial.println("No CAN message received.");
}
}```