CAN Bus: Serial Communication - How It Works?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this here is a can transceiver where can stands for controller area network and it's a type of Serial communication in previous videos we talked about the uart I Square C 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 how they work and establish a can connection between an Arduino and an esp32 so guys let's get started hey guys PCB way sponsoring this video and let me just tell about their services for example look how awesome their prototyping pcbs are and you can get this for only five dollars they are so professional and they will make your project work a lot better and to order such pcbs you only need a few minutes on their website where you can select any configuration that you want for your boards along with that you can also order the SMD stencil for soldering the components using solder paste and you can also use their services for flexible pcbs and create some unique projects and if you want to make your project start to finish you can get the pcbs assembled together with the mode injected part or maybe 3D printed metal parts or other CNC Services all with PCB way what's up my friends welcome back it has been a while since I wanted to talk about can my previous videos on I Square C 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 Ken stands for controller area network so as the 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 some lights some screens and so on all communicating together can communication is a Serial type communication it has a bus topology it is half duplex asynchronous differential signal level and amazing noise protection so let's go step by step for all these features here I have 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 and these are called can H4 high and can L for low as you can see on this module which is a can transceiver so let's start with part number one why is this a bus topology well first of all decant 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 campaigns to this bus the can communication is a message based protocol based on an ID plus a message so each device will send the data that it wants to send but 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 this is called a controller area network and because this is half duplex only one device could use the bus and send the data at the time once the transfer is complete the other device could respond and send its data so for example in a car you want to 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 and in that way we can use a lot less wires and we don't create a mess but there is another thing why we use the can communication with cars so for part number two is the noise level and the differential signal to understand in digital terms usually the Beats 0 and 1 are represented by a high or low voltage level such as 0 volts and 5 volts for example where the zero volt represents a zero and the 5 volts represents the one bit but inside of the car there is a lot of electrical noise vibrations from the motor magnetic fields from the speakers from outside the car and so on so just imagine that the can bus is passing Behind the Music speaker for example and that will create a lot of magnetic disturbance which might induce a voltage over the signal right so let's imagine that this was a normal signal and that induced voltage happens exactly on this beat which was a zero 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 one bit right and that will be an error but with the can communication we don't use two levels of 0 and 5 volts we use differential levels that means that the receiver will always read the voltage difference between the H and the L signals and interpret that as a bit for example when H is low and L is high the differential level would be 2.5 volts minus 2.5 volts equal to zero and that represents a one bit but when H is high and L is low we have 3.75 volts minus 1.25 volts equals the voltage of 2.5 volts and that represents a zero beat but now look what happens when we introduce the same magnetic noise as before let's say that this bit here is affected by the same noise level but guess what because both wires were next to each other both levels will be affected with the same amount so let's say that the H goes from 3.75 volts to 5 volts and the L goes from 1.25 volts to a voltage of 2.5 volts so the differential value between the signals is still 2.5 volts and we still interpret that as a zero beat so the data was not affected by the external noise pretty cool right that's why we get these shapes on the oscilloscope for differential level communication where we don't read the signal voltage but the voltage difference between two signals okay so now for part number three can communication is asynchronous that's the same as we have seen for the uart communication and basically it means that the devices are not using the same shared clock source there is no clock pin but all devices must agree on about rate if one device is sending the data at a speed of one megabit per second but the other devices are expecting a bit speed of 5 megabits per second those devices can communicate between each other so for the data frame the signal starts with a start bit so in that way the rest of the devices will be aware that new data is coming in and 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'll talk about in a moment and finally the end frame bits okay so before we talk about the final step which is the CRC let's make an example using the Arduino this here is a can transceiver module using the TGA 1050 chip and 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 directly control this RX and DX pins because this is not a normal Ur Port it needs a can controller for example this other module has the same tj1050ic but it also has the scan controller chip which is the mCP 2515 this has an Spa communication and it will automatically generate the TX signals or to 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 Duane right now but the esp32 also has scan controller pins so we could 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 the H lines from one module to the other in Arduino IDE we installed this library that is called ken.h that you could download from Below in examples you have a code for a can sender or can receiver we upload the receiver code to the Arduino Uno but for the esp32 copy this code from below the video as you can see we defined the esp32 pins 21 and 22 for TX and RX and then we send 8 bytes with the letters electron 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 electronics.com and finally to end this video you must know that Ken has 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 that you want to send and calculate the CRC for that data using a formula for example these values the receiver will get the data and the CRC and makes the inverse calculation of the CRC and also the CRC of the received data if they are not the same that means that the data had errors on the way that's why the can communication is so secure against noise and is used in automobile for safety reasons so guys I hope that you now know how can communication works and how to implement one using Arduino or esp32 for the code the schematics the modules that I've used and the libraries check electrodes.com for the full tutorial thanks again and see you later guys hey guys so that was another project and I hope that you like it as you all know to buy all these modules a huge help from you is from patreon so if you want to support me you can support me there but also just commenting below giving me a like or sharing this video it will also support my channel so thank you very much to all my patrons and to you guys
Info
Channel: Electronoobs
Views: 132,968
Rating: undefined out of 5
Keywords: CAN, controller area network, port, bus, protocol, tutorial, Arduino, ESP32, TJA1050, MCP2515, serial, i2c, SPI, signals, CANH, CANL, car, RS232
Id: JZSCzRT9TTo
Channel Id: undefined
Length: 11min 25sec (685 seconds)
Published: Sun Jul 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.