A few words about what the CAN bus is, what it is for and how it works!

A few words about what the CAN bus is, what it is for and how it works!

what's up everybody
here and today I'm going to be talking all about can communication what it is why we care and how it works so what is canned communication can communication is a protocol that is used extensively in the automotive industry everywhere from internal combustion vehicles to electric vehicles to Robotics and the reason it is so widespread is because it is a fast and reliable protocol which is very important for safety critical applications the can protocol is used between ecu's in these vehicles so an ECU is an electronic control unit or pretty much you can think of it as a microcontroller so you have ecu's spread out all across the vehicle you might have one on the high voltage battery you might have another one controlling the windows you'll have another one uh controlling you know the motors of the vehicle and they all need to communicate with each other and the way they do this is through this embedded protocol called can

so what does a can bus? look like or what is a can bus?

so can is a shared bus topology so what that means is there is no master device there are a bunch of distributed can nodes that are all able to communicate with each other over this shared bus now this shared bus happens to be a pair of differential signal lines or wires and these are usually Twisted together because a twisted pair of wires is less prone to electromagnetic interference and the reason for this is imagine you have a wire and you have some sort of interference that distorts the signal well if you have two wires and you have a distortion it distorts both wires by the same amount and as I'll explain later can a can signal is actually the difference between these two wires so if both wires are distorted to the same degree then it doesn't actually affect the can signal

that's what makes can so reliable in the first place so as I've shown here we have all the can nodes on the shared bus and the shared bus itself like I mentioned is just two cables and these have what are called terminating resistors on each end of the bus pretty much the idea is that when you have a transmission line and you have a signal going on the transmission line it can reflect off the end of the transmission line and you want to pick resistor values to reduce this reflection as much as possible so that's why these terminating resistors are very important on a canvas now you might be wondering okay we have a bunch of nodes on a bus how do we prevent them from talking over each other well that's where we enter uh can message arbitration so each node on a can bus

has an ID and every time a can node wants to send a message it will first transmit its ID onto the bus now something very important about the can protocol is that a zero bit overpowers a one bit so if you have two nodes and they both want access to the bus they're both going to start by transmitting their IDs now since a zero overpowers a one bit the can node that has a lower ID will always overpower the can node with a higher ID so that means lower ID is higher priority so for example let's say you have a node that has ID 15 and you have another node with ID 16. they're both going to start their Transmissions and by the time they get to bit four that is when their IDs will start to become different and the note 15 because it is low smaller than 16 will have a zero here and overpower the 16. so an important feature of the can bus first of all is that every node um can try to transmit onto the bus but they will only be able to if they win the can message arbitration and the way that they know that they've won the cam message arbitration is if the the ID that they transmit their own ID is read back successfully so each node can transmit and read their own transmissions on the bus so they know that they've successfully obtained access to the bus if they can read back their own ID pretty much so as you can see in the case of node 16 here it fails so it stops its own transmission and it will wait until node 15 is done and then it'll try again and that is pretty much the gist of it that is how you have a bunch of nodes on a shared bus um making sure

they don't step over each other so what does a can signal actually look like so the can protocol operates on the physical layer and the data link layer of the OSI model so the file layer the physical layer is is as low as it gets it just concerns with voltage levels so the can signal voltage level specification is as follows you have two wires as I mentioned before we'll call one of them can high and the other can low for obvious reasons now it is the difference between these two wires that determine whether or not a bit is a zero or a one now there are different types of canned standards but the most common one that you'll come across is can high speed and that says that if there are two volts between kid high and can't low that is a dominant uh voltage so that'll be a bit zero if there is no difference or a very small difference between can high and can't low we say that the lines are at a recessive voltage and that is a one and that's pretty much it really

so moving on we have the data link layer of the protocol so one step above physical now the data link layer is concerned with what data is actually sent over these lights um and so the can signal has 64 bits of data and it has a whole bunch of other metadata involved by the way I found this diagram on Wikipedia and I highly recommend that if you're interested in can that you go read this Wikipedia page it goes into extreme detail I'll keep things at a bit of a higher level I won't go into every single bit here but I'll cover the important stuff so a can transmission will start with the soft bit or start of frame uh so we can see here we have the can high and can low we also have can RX uh so the can RX line is what is what goes into a microcontroller so you can just think of this as the data so we start off our transmission uh in the idle state or I guess before we start our transmission the bus is in the auto state and then we go to a dominant voltage which as we know is a zero for one bit and that indicates that we are now

beginning a transmission we now have the arbitration phase so as we talked about earlier there could be several can nodes that want to transmit something at the same time so this sequence of bits will just determine which can node will actually get access to the bus these stuff bits here are pretty interesting so the stuff bit pretty much is like a heartbeat it just checks that there are no issues on the can bus because you can imagine there might be like a short circuit on can't hire can low and if it's just constantly held at one then there might be an issue but the stuff bit just interrupts this pattern and just says okay yeah everything's fine continue on so you can pretty much just ignore the stuff bit so moving on after the can node gets accessed to the bus it will send the DLC so a DLC stands for data length code and this pretty much just says hey my message has this many bites because even though we have 64 bits of data available in the can frame we might only have three bits of data or 13 bits or 21 bits we don't necessarily need to use all 64 bits so the DLC just says you can expect this much data then we send the data and other stuff bit after all the data is sent we send something called a CRC and a CRC is a cyclic redundancy check and it's pretty much just a sort of cryptographic function of our data of our can frame to make sure that there are no uh Corruptions in the data uh we then have the ack bits so the AK bits will be acknowledged from the receiver so the transmitter will see the ACT bits and we'll say okay cool the receiver got my message then we have the end of frame bits so these are a recessive voltage so pretty much like the bus will just go back to idle for a certain amount of bits and then there are the inter-frame spacing bits and this is just three bits to just make sure that there is a bit of space between the can frames uh on the can bus so now we have a pretty good idea of what the actual can bus and protocol looks like uh so what does the can node look like well the can node is usually just a microcontroller that has a can controller and the can controller is what handles the data length data link layer of the protocol uh so it'll handle like the the can frames um you know figuring out which ID it is that sort of thing and then we have the can transceiver which is the Phi layer of the protocol and usually uh microcontrollers don't actually have can transceivers you need to buy an external device but most microcontrollers do have the can controller so we can see in the diagram here we have can high can't low these will go into the can transceiver which then passes those on to the can controller the can controller will do uh like can message arbitration it'll detect errors just Implement uh the basic functionality of the protocol and then we have our microcontroller which will then consume the data that pretty much covers the basics of can so there's a lot more to this protocol to know such as like a can air frame uh there's fault tolerant low speed can there's can FD can FD is just really fast can it's pretty new there's

acceptance filters acceptance filters are pretty much uh just a way for a can node to ignore data doesn't care about I'll leave some links in the description for further reading and uh I've got some more videos coming soon where I might go into more detail about some of these topics but we've pretty much covered like the basics of can so if you liked the video please drop a like And subscribe and stay tuned I've got a few more videos coming out about embedded protocols and I'll be coding some on the esp32

Easy Store Pickup

With in-store pickup options, pickups are convenient.

Free shipping over $20

Free shipping offer applies to all orders of BigSaleTech over $20

Low Price Guarantee

If you find a lower price, we’ll gladly match that price.

Latest Tech

Check out what's new with the latest launches.
Afterpay American Express Apple Pay Bitcoin Dankort Diners Club Discover Dogecoin Dwolla Forbrugsforeningen