Raspberry Pi Based CAN-BUS Reader
by T20A02 in Circuits > Raspberry Pi
1548 Views, 2 Favorites, 0 Comments
Raspberry Pi Based CAN-BUS Reader
CAN networking is used in many industrial and Automotive applications as It is a robust 2 wire networking system, that significantly simplified the wiring of the model automobile, as it replaced the need for a dedicated wire connection from each module to each other module, reducing the need to a pair of wires in a bus style network. This article outlines the setup and configuration for a generic RS485 RPI hat, and how to connect it the your PI, as well as basic configuration, and the basics of capturing and interpreting the data, from your OBD2 diagnostic port.
Supplies
For or hardware setup we will need a few key components:
- We will need our raspberry pi, for hosting the OS, and to run our capture software. I had a spare, for the project, but they can be picked up from Adafruit starting at 35$.
- Second we will need our RS485 Hat. There are a couple options here, but I primarily used the simple one connection hat, but did also purchase the 2 RS485 and 1 CAN hat at later date. If you plan on powering this setup from the cars diagnostic ports power source, you will need the latter, but to keep it simple you can opt for the first, and use a battery bank, or AC power.The simple RS485 can be found on Ebay for around 17$. The larger model with power delivery onboard, they are also available on Ebay for around 27$.
- The third piece of the Puzzle is the OBD2 port pigtail. This also can be found on Ebay for around 9$.
- Finally you will need a micro SD card + a way to read it (Laptop with micro SD slot, or a USB adapter), and a Power supply for the RPI. The micro SD card will be used to flash the operating system on, and the Power supply will be used to power the PI when setting it up, and when using it in the car. Having a USB type C battery bank, or car charger with the RPI4 would be your best bet, but if you are using an older model PI a USB micro B will be needed.
Some additional components you likely have spare;
You will need a soldering iron and solder. If you have spent any time with electronics in the past you may have one, if you don't a cheap and simple Iron will work, but taking the time to learn the basics before you start this project.
Finally you will need a little bit of wire, to connect the OBD2 to the RPI hat. You will want at least 4 runs of 3-5 feet of wire. You can also find this at the local hardware store, or automotive store, but make sure to get a gauge of wire not to large that it doesn't fit in the CAN hat, but not so small it is too fragile.
Installing Raspbian OS
Before starting please note that all data on the micro SD card will be destroyed.
1.Download Raspberry Pi Imager:
First, you need to download the Raspberry Pi Imager. You can download it from the official Raspberry Pi website.
2. Install Raspberry Pi Imager
After downloading, install the Raspberry Pi Imager on your computer. The installation process is straightforward and similar to other applications, just follow the defaults. The Imager application can be found on the Official site here.
3. Insert Micro SD Card
Insert your micro SD card into your computer using your USB adapter if necessary, and make sure the PC/Laptop detects it.
5. Choose OS
Open Raspberry Pi Imager and click on the ‘CHOOSE OS’ button. A list of available operating systems will appear. Select ‘Raspberry Pi OS (other)’, then select ‘Raspberry Pi OS Lite (32-bit)’.
6. Choose SD Card
Next, click on the ‘CHOOSE SD CARD’ button. A list of available drives will appear. Select your micro SD card from the list.
7. Write
After selecting the OS and SD card, click on the ‘WRITE’ button. The imager will then write the Raspberry Pi OS onto the SD card. This process may take a while and it may ask for administrative privileges on your system.
8. Now after clicking write, you should be asked if you would like to configure any settings before writing. Here we need to click "yes" as we need to create a user account, and connect to your WiFi for accessing the interface.
9. Now that you have the settings page open:
- Set the "hostname" field to the name you want the PI to use on the network.
- Set a Username for the user on the device, and a Password for that User. This is the account we will use to login.
- Check the "Configure Wireless LAN" and for SSID type the name of your WiFi, and password for that network.
- Your settings should look like the sample Image.
- Finally Click save, and then "yes" to apply settings
10. Eject SD Card
Once the writing process is complete, eject the micro SD card from your computer.
11. Insert SD Card into Raspberry Pi
Finally, insert the micro SD card into your Raspberry Pi. You can now boot up your Raspberry Pi with the newly installed Raspberry Pi OS.
Assembling Hardware
Now we have to assemble the hardware, this is where we break out our soldering station, or we phone a friend to have it soldered up!
- Step one, we are going to unbox out RPI hat, and install it on the 40 PIN adapter on the RPI. Simply slot the ins on the pi, into the adapter, with the adapter over the PI, like seen in the pictures. Sometimes this requires a good deal of force, but make sure you have the pins aligned not to bend them.
- Next we have to create out adapter. All modern cars with OBD2 have a set number of pins in the plug, that are standardized. We are going to use this, to interface with the Engine CAN network, as it is standard for OBD2 Scanners used my automotive shops.
- First Reference the Wiring Diagram. In that you will see the RPI RS485 hat we have installed. This has a terminal block, so there in so soldering required. We will be focusing in the end with the OBD2 adapter.
- Pins 14 and 6 will always be Positive and Negative CAN as per the standard, as will 16 and 4 be the 12 Power. You can ignore the power pins if you are using an external power adapter, but if you opp'ed for the larger RS485 board, with 12V inputs you will need them.
- Solder 4 leads to the Connector, 2 for the CAN and 2 for the power. Additionally if you have access to a different network you can add additional leads to those wires. For Instance the test vehicle had a secondary BUS accessible from the OBD2.
- Now that those leads are soldered, you can assemble the connector end, and strip the insulation from the other ends of the wire.
- Finally terminate them into he screw terminals, making sure the power leads are not connected to data. If the power leads do end up in the data pins, the hat, and possibly the PI will be fried.
- Now that the hat is assembled, take some time to double check your wires, and label them for later use. This is helpful, especially if you are using one color wire. Now we are ready to do our first power up and setup the PI.
Connecting to the PI + Initial Settup
SSH Access
Now we are going to connect to the pi using SSH. This can be don't from any Linux, Windows, or OSX laptop. There are lots of guides online on how to ssh each, but they all are very simpler in steps.
- First we need to get the IP address of the PI. To do this, you need to access the WiFi router that the PI is connecting too. Simply navigate to your routers, IP, usually the "Gateway" address for the network. This process Varies across all vendors, and models, and if you are unsure how to do this, you can likely find a guide online.
- Once you have the IP address, from a Terminal instance, or powershell in windows run the following command, with the username, and IP of your device:
ssh username@ipaddress
3. Now you will be prompted for the password you set when imaging the PI.
4. This will drop you into a shell session for the PI.
CAN Hat Setup with the LGPIO Library
To set up lgpio on your Raspberry Pi, follow these steps:
- Open the Raspberry Pi terminal and run the following command:
wget https://github.com/joan2937/lg/archive/master.zip
unzip master.zip
cd lg-master
sudo make install
- For more information, refer to the official Waveshare website.
Python Setup
To use lgpio with Python, follow these steps:
sudo apt-get update sudo apt-get install python-serial sudo pip install python-can
CAN Usage
This demo requires a Raspberry Pi 3 and an RS485 CAN HAT.
Preparation
Insert the module into the Raspberry Pi and modify the startup script "config.txt":
sudo nano /boot/config.txt
Add the following content to the file:
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000
Note: Change the oscillator value to match the ocillator found on your device. usually either am 8 or 12MHz clock, you need to change the value to "8000000" for 8MHz or "12000000" for 12MHz Etc.*
- Save and exit, then restart the Raspberry Pi:
sudo reboot
After rebooting, run the command to check if the initialization was successful:
dmesg | grep -i '\(can\|spi\)'
The "Successfully initialized" output indicates success.
- Make sure both sides of the Raspberry Pi are handled this way and connect the H's and L's of the two modules correspondingly. If using another CAN device, ensure that the lines H-H and L-L are connected.
Open CAN
To open CAN, run the following commands:
sudo ip link set can0 up type can bitrate 1000000
sudo ifconfig can0 txqueuelen 65536
Loopback Mode Testing
To perform loopback mode testing, first install can-utils. This package is important as it contains the tools the system needs to use a can network interface.
sudo apt-get install can-utils
Then, open can0 and enable loopback mode with the following commands one at a time. Note the current bit-rate is just for testing, and will have to be changed to match the bit-rate that the CAN network we will be capturing from will be running. There can be many different speeds used in modern cars.
sudo ip link set can0 down
sudo ip link set can0 type can bitrate 1000000 loopback on
sudo ip link set can0 up type can bitrate 1000000
Open two terminal windows and test can0 self-loop back, by running each of the two following commands, one in each window, in the following order, to start connecting, and the send a test message. If everything is configured correctly, then you should see the message from the sent side get listed on the dump side.
candump can0
cansend can0 000#11.22.33.44
Software and Capturiing
Software Requirements for the packet capture.
Please run the following commands to install some tools we will need
sudo apt-get update
sudo apt-get install tshark
This Updates the system packages, and installs our command line network traffic sniffer, tshark.
Set Can interface up, set bit-rate, and set to listen only:
Make sure you have the bit-rate set to the proper speed or you will experience issues with capturing. It is important to use listen only as this will prevent the can hat from trying to send reply messages to the vehicle, which can cause it to have troubles if the header ID of the hat is the same as a module in the car. In my case with the Saab 9-3, it caused the ABS module to shut off, and separate from the network presumably due to it sharing a header ID.
sudo ip link set can0 up type can bitrate 10500 listen-only on
Capture from can interface with tshark and write out to file
Now running the following command, will start tshark, as we installed earlier. This sets it to the can interface and writes the outputs of the collected messages, to a .pcap file, which can be viewed in Wireshark a much easier tool to navigate on a desktop computer.
sudo tshark -i can0 -w /opt/captures/capture.pcap
Now that this file has been generated, you can make more captures if you would like. When ready to read the files simply power down the pi, and remove the micro SD card. Now you can connect the SD card to a PC, and copy the file off from the location you saved/wrote the .pcap file to. This file can be read using a tool called Wireshark. Wireshark is a free tool used to capture and analyze network traffic, and functions much like the tool we used above called tshark.
Simply install Wireshark, like you would for any other application, follwing the default steps and then use file > open, to view the .pcap file, and analyze the results. Here you can see the headers and content of the messages running on the system, and is your starting point for breaking down, and comparing the headers to their respective parent modules. You can see an exmaple of the wireshark utility above.
These messages will vary between the CAN bus, Make, Module, Year, of the vehicle, and depending in this there could already be someone who has decoded a portion of the messages. From here you can run wild. Knowing what the car is running, and what data it is passing on the network. Weather you are diagnosing issues, or you are creating custom functions.