Hi everyone.
I working on a project where I utilize an ESP32 device and a CAN level shifter to create an analog to CAN device. So far I have reasonable amount of success with this, the only issue I have left to overcome is the Baud rate in my code is always double what I measure on the CAN bus using a CANAlyzer.
I am using a Sparkfun ESP32 Thing Plus:
Along with the Comimark CAN driver:
https://www.amazon.com/gp/product/B07YHYQ7C9/ref=ppx_yo_dt_b_asin_title_o06_s00?ie=UTF8&psc=1
I have successfully installed the ESP32-Arduino-CAN-master library.
Here is the code I have been using:
#include "CAN.h"
#include "CAN_config.h"
// Define the CAN_cfg variable
CAN_device_t CAN_cfg = {
.speed = CAN_SPEED_1000KBPS, // Set the desired speed
.tx_pin_id = (gpio_num_t)16, // TX pin number
.rx_pin_id = (gpio_num_t)17, // RX pin number
};
// Define all 8 analog input pins
const int analogPins[8] = {26, 25, 34, 35, 32, 33, 27, 14}; // Replace with actual pin numbers
void setup() {
Serial.begin(115200);
if (CAN_init() != 0) {
Serial.println("Starting CAN failed!");
while (1);
}
analogReadResolution(12); // 12 bits resolution for ESP32
}
void loop() {
// Read voltage from all analog pins and send over CAN in two messages
for (int msgIdx = 0; msgIdx < 2; msgIdx++) {
CAN_frame_t frame;
frame.FIR.B.FF = CAN_frame_std;
frame.FIR.B.DLC = 8; // 2 bytes per analog read
frame.MsgID = 100 + msgIdx; // CAN ID: 100 for first message, 101 for second
for (int i = 0; i < 4; i++) {
int analogValue = analogRead(analogPins[msgIdx * 4 + i]);
float voltage = 3.3 * analogValue / 4095; // Convert to voltage
uint16_t voltageInt = (uint16_t)(voltage * 100); // Convert to 2 decimal places
frame.data.u8[i * 2] = (voltageInt >> 8) & 0xFF; // High byte
frame.data.u8[i * 2 + 1] = voltageInt & 0xFF; // Low byte
}
// Send message
if (CAN_write_frame(&frame) == 0) {
for (int i = 0; i < 4; i++) {
Serial.print("Voltage");
Serial.print(msgIdx * 4 + i + 1);
Serial.print(": ");
int analogValue = analogRead(analogPins[msgIdx * 4 + i]);
float voltage = 3.3 * analogValue / 4095;
Serial.print(voltage);
Serial.println(" V");
}
} else {
Serial.println("Error sending message");
}
}
delay(10); // Adjust delay as needed
}
Any pointers will be great, thanks. Frustrating to get it so close to fully working. I'm sure it's something dumb on my part...
Richard