The Controller Area Network bus, or CAN bus, is a very effective communication protocol thanks to its high speed capabilities, long range reliability, and noise immunity. For these reasons, CAN communication has become the standard in automotive technologies and high noise environments. Devices on the CAN bus are called nodes. All nodes on the CAN bus are connected in parallel, meaning that each node is connected to all the other nodes on the network. A single CAN bus can have up to 115 nodes at once, depending on the rate of message transmission, but for most applications, it is recommended to have up to 32 devices. It is also recommended to keep the length between the first and last node less than 40 meters apart.
This step-by-step tutorial will show you how to set up a CAN node using the STM32 microcontroller, including the circuit and simple C code to read and write to the CAN bus
To communicate with the CAN bus, we will use the MCP2551 CAN transceiver IC. The IC acts as an intermediate transmitter/receiver pair to connect the STM32 to the CAN bus. The circuit to set up this IC is quite simple, but there are a few things to note:
Now that the transceiver circuit is connected to the STM32, we can start writing messages to the CAN bus. This instructable guide will not go in-depth into the STM32 code. However, be sure to check out our code for examples here. Using the STM32 as a CAN node will require a CAN header file. We wrote our own, which can be found on our github here. Here, we will give a brief overview of the read/write process.
To read from the CAN bus, we first need to know the ID of the CAN message. Every message should have a unique ID, with lower IDs having higher priority. The code snippet shown here is waiting for a CAN message with ID 0x622. In our system, if the first bit of the 6th byte is high, then we want to set pin A10 high.
When writing a CAN message, we must remember that CAN messages are multi-byte. Each written message must have an ID and length. In the second snippet of code shown, we write data to each byte, then send the message (The ID and length parameters are defined earlier in the code).
When connecting multiple CAN nodes, careful attention should be paid to the length of cables. The two farthest nodes can be up to 40m apart from each other. Middle nodes connecting to the bus should be within 50cm of the main bus lines.
CAN connections follow an industry standard of using a Dsub9 connector with the CANL line on pin 2 and the CANH line on pin7. The option CANGND line can go on pin 3.
JLCPCB offers fast, high quality service at very reasonable prices. Get 5 boards, any colour with tons of customization, for only $2! And if it's your first order, get 10 boards for the same price!
Just upload your gerbers and get an instant quote! Submit your order and your boards will be reviewed for production within the hour. Once you pay, you can expect your high quality boards in as soon as three days!
Check it out here
Huge shout out to JLCPCB for sponsoring this project. JLCPCB (ShenzhenJLC Electronics Co., Ltd.), is the largest PCB prototype enterprise in China and a high-tech manufacturer specializing in quick PCB prototyping and small-batch PCB production. They were kind enough to provide UBC Solar with our new PCBs for our solar-powered race car. We placed our order on Friday and got the boards on Wednesday!