This here is a CAN transceiver where CAN stands for Controller Area Network and is a type
of serial communication.
In previous videos we’ve talked about UART, i2c and SPI communications, and the Arduino
UNO or NANO for example, have all these types of serial ports.
But it doesn’t have a CAN bus, so what is CAN, how it works internally, why is better
or worse than other types of serial communications and why we use this type of serial data transfer
for the entire automobile industry?
We will check the signals, explain you how they work and establish a CAN connection between
an Arduino and an ESP32
So guys, let’s get started.
What’s up my friends, welcome black.
It has been a while since I wanted to talk about CAN.
My previous video on i2c and SPI had a lot of viewers and it seems that you are interested
into CAN communication as well so let’s get started.
First of all CAN stands for Controller Area Network, so as its name implies, it works
well in a network so we can have multiple devices connected together.
That’s why we use CAN communication for automotive applications where we have a bunch
of sensors, actuators, lights screens and so on, all communicating together.
CAN communication is a serial type communication, it has a bus topology, is half duplex, asynchronous,
differential signal level and amazing noise protection so let’s go step by step for
all these features.
Here is a CAN signal that I’ve captured with my oscilloscope and we will see the Arduino
example in just a moment.
This kind of communication uses two pins that are called CANH for high and CANL for low
as you can see on this module which is a CAN transceiver.
So, part number 1, why is this a bus topology?
Well, first of all, the CAN communication doesn’t need a host or a so-called master
slave connection where each device is connected to the master in a star configuration.
With CAN, all devices can send the data.
So instead of having each device connected with a separate connection to a master device,
we have a bus, basically the H and L lines, and then, each device will connect its CAN
pins to this bus.
CAN communication is message based protocol based on an ID plus a message.
So each device could send the data that it wants but it also has to send an ID.
All the other devices will receive the same message, but only the device with the given
ID will act on it.
In this way, using only two wires we can create a communication between all the devices in
a network, that’s why it is called Controller Area Network.
And since it is half duplex, only one device can use the bus and send data at a time.
Once the transfer is complete, the other device could respond and send its data.
So in a car you would have the temperature controller, the tire pressure sensor, the
gasoline level sensor, the main music display, the radar sensor and so on, all connected
to the CAN bus, communicating with each other.
In that way, we use a lot less wires and we don’t create a mess.
But there's another thing why we use CAN communications with cars.
So part number 2, noise level and differential signal.
To understand, in digital terms, usually the bits 0 and 1 are represented by a high or
a low voltage level such as 0 and 5V for example where 0V represents a 0 and 5V represents
the 1 bit.
But inside a car there is a lot of electrical noise, vibration from the motor, magnetic
fields from the speakers, from outside the car and so on.
Just imagine that the CAN bus is passing just behind your music speaker for example.
That would create a lot of magnetic disturbance which might induce a voltage over the signal,
right?
So let’s imagine this was a normal signal, and that the induced voltage happens exactly
on this bit, which was a 0.
But now, because of the noise magnetic induction, this bit voltage level was raised to let’s
say 4 volts or something like that, and on that level, the receiver would interpret that
as a 1, right?
And that would be an error.
But with CAN communication we don’t use two levels of 0 and 5V, we use differential
levels.
That means the receiver will always read the voltage difference between the H and L signals
and interpret that as a bit.
For example, when H is low and L is high, the differential level would be 2.5V - 2.5V
equal to 0, and that represents a 1 bit.
But when H is high and L is LOW we have 3.75V minus 1.25V, equals to a voltage of 2.5V and
that represents a 0 bit.
But look what happens when we introduce the same magnetic noise as before.
Let’s say this bit here is affected by the same noise level.
But guess what, since both wires are next to each other, both levels will be affected
with the same amount.
So let’s say that H goes from 3.75V to 5V and L goes from 1.25V to a voltage of 2.5V.
The differential value between the signals is still 2.5V, and we still interpret that
as a 0 bit, so the data was not affected by the external noise.
Pretty cool right?
That’s why we get these shapes on the oscilloscope for a differential level communication where
we don’t read the signal voltage level but the voltage difference between two signals.
Ok, for part number 3, CAN communication is asynchronous.
That’s the same as we have seen for the UART communication and it basically means
that the devices are not using a shared clock source.
There is no clock pin.
But all devices must agree on a baud rate.
If one devices is sending data at a speed of 1Mbit/second but the other devices is expecting
a bit speed of 5Mbit/seconds, those devices can’t communicate between each other.
So for the data frame, the signal starts with a start bit so the rest of the devices will
be aware that new data is coming in.
Then we have the ID bits so we know to which device the data is sent.
Then we have the control bit and the actual data which could be from 0 to 8 bytes of data.
Then we have the CRC bits which we will talk about in a moment and finally the end frame
bits.
Ok, so before we talk about the final step which is CRC, let’s make an example using
Arduino.
This is a CAN transceiver module using the TJA 10 50 chip.
This needs an RX and TX input and it will create the differential signals on the H and
L pins.
But the Arduino UNO for example, can’t directly control those RX and TX pins because that’s
not a normal UART port.
It needs a CAN controller.
For example this module has the same TJA 10 50 IC but it also has this CAN controller
chip which is the MCP 25 15.
This has an SPI communication it will automatically generate the TX signals or read the RX signals
from the CAN transceiver.
By the way, the Arduino DUE does have CAN controller pins but I don’t have a DUE right
now.
But the ESP32 also has CAN controller pins so we can use that.
So I will connect the module with the CAN controller to the Arduino UNO and the CAN
transceiver without the controller directly to the ESP32 as in this schematic.
Then I connect the L and H lines from one module to the other.
In Arduino IDE install this library called CAN.h that you could download from below.
In examples you have a code for a can sender or a CAN receiver.
Upload the receiver code to the Arduino UNO.
For the ESP32 copy this code from below the video.
As you can see, here we define the ESP32 pins 21 and 22 for TX and RX.
Then we send 8 bytes with the letters ELECTORN.
Upload this code to the ESP32.
Now I open the serial monitor and as you can see, the data from the ESP32 is sent and received
by the Arduino UNO.
We have successfully made a CAN communication.
Check more about this example on electronoobs.com Finally, to end this video, you must know
that CAN has that CRC implementation standing for cyclic redundancy check.
So even if the differential level read fails, we can still check if the received data is
good or not.
Basically you get the data you want to send and calculate the CRC for that data using
a formula, for example these values.
The receiver gets the data and the CRC and makes the inverse calculation of the CRC and
also the CRC of the received data.
If they're not the same, then the data had errors on the way.
That’s why CAN communication is so secure against noise and is used for automobiles
for safety reasons.
So guys, I hope you now know how CAN communication works and how to implement one using Arduino
or ESP32.
For the code, schematics, modules I’ve used and libraries, check electronoobs.com for
the full tutorial.
Thanks again and see you later guys.