// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <CAN.h>
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("CAN Sender");
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
void loop() {
// send packet: id is 11 bits, packet can contain up to 8 bytes of data
Serial.print("Sending packet ... ");
CAN.beginPacket(0x12);
CAN.write('h');
CAN.write('e');
CAN.write('l');
CAN.write('l');
CAN.write('o');
CAN.endPacket();
Serial.println("done");
delay(1000);
// send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
Serial.print("Sending extended packet ... ");
CAN.beginExtendedPacket(0xabcdef);
CAN.write('w');
CAN.write('o');
CAN.write('r');
CAN.write('l');
CAN.write('d');
CAN.endPacket();
Serial.println("done");
delay(1000);
}
Your schematic does not show termination resisters on each end of the bus. There should be two 120 ohm resistors, one on each physical end of the bus. The receiver must be active when sending a message as it must be acknowledged by another node on the bus.
From the picture that does not compute. The can driver/transceiver modules are not connected to the ESP unless you have done so on the back of the board which I am assuming you have done. They are connected to each other correctly. These transceiver modules only translate signals to and from the micro to the bus. All of the logic is in the controller (ESP device). I like at least 1 meter distance or more, I have had problems with very short buss lines.
The I MCP2515 is not a driver but a controller. I do not know if the ESP uses a similar construction internally. The TJA1050 is the CAN bus driver. You have them connected properly per your drawing. I assume you are using the Espressif ESP32 wiring diagram and software examples.
The other thing is I start very slow so I can see the bits on my scope and look for the ACK bit. If that is there the remote is responding and the message is close if not correct. There are some cheap (under $10 US) available on the web and they work ok. They are nice for this type of thing and they will decode different protocols. Hopefully this helps. I am not familiar with the library you are using, hopefully some of the others here can help with that.
Have you looked at the data sheet? If it is a TJA1050 it probably will not work, that is a 5V part. You could use something like a LTC2875 as it is 3 and 5V compatible. Before you change try using a diode in series with the 5V power supply to the transceiver module. If it gets better we are on the right track. There are also mods on the internet showing how to modify the transceiver module for 3.3V/5V. I use 5V systems so I am sorry I missed this one.