How-To CAN-Bus Address Switches
Description
The Pi-Tron CM4 has six DIP switches, where the switches 1 to 4 are connected to the onboard GPIO expander MCP23017 and can be used as CAN-Bus address selectors, if the device is intended to be a CAN-Bus slave or node. However, since these 4 switches are connected to GPIO inputs, they can be used for anything or any configuration option which is intended to be set or chosen through a hardware switch. For example, looking past the name, these four switches could be used to select a Modbus RTU devices address. These are just two possibilities how to use the switches.
This How-To shows how to enable the GPIO expander chip and access the GPIOs.
Requirements
-
Development computer with network access to the Pi-Tron CM4 or alternatively connect an HDMI monitor, mouse and keyboard.
-
To login into the Pi-Tron CM4 via network, it is requires that the SSH server is running.
- The login details are by default: username: pi and password: raspberry
- Note: The latest version of the Raspberry Pi OS requires the user creating the initial SD card or writing the system to eMMC memory to choose a username and password or the information has to be entered during first boot of the system. The above mentioned defaults may not apply to your situation.
- 24 volts power supply with 2 pin power plug for the Pi-Tron CM4.
- An active Internet connection to install additional software.
- The the MCP23017 I/O expander is connected to I2C bus 1 and has the address 0x20.
How-To
Preparation
Activate I2C
First make sure the I2C bus 1 is activated. Use the raspi-config program to enable the I2C interface, if this is not already the case. Afterwards reboot the Pi-Tron CM4.
Install the libgpiod library
To access GPIOs on the Raspberry Pi weather they are part of the Raspberry Pi directly or are added via I2C, the libgpiod library offers service programs to read and write GPIOs from the OS console but also via APIs from different programming languages.
sudo apt update
sudo apt install gpiod libgpiod-dev libgpiod-doc
Install the i2c-tools
Installing the i2c-tools allows the user to perform an I2C bus scan to see which addresses (devices) are available on the bus.
sudo apt update
sudo apt install i2c-tools
Add the MCP23017 overlay to the config.txt file
To activate the MCP23017 driver within the Raspberry Pi OS, the corresponding overlay needs to be added to the config.txt file. For Raspberry Pi OS Buster and Bullseye you can use the following.
sudo nano /boot/config.txt
Starting with Raspberry Pi OS Bookworm, the config.txt file has moved:
sudo nano /boot/firmware/config.txt
At the end of the file add the following line.
dtoverlay=mcp23017,noints=1
Save the changes and leave the editor with Ctrl + O → Enter → Ctrl + X.
Reboot
Reboot the Pi to activate all the changes.
sudo reboot
Accessing the CAN-Bus address switches GPIOs
The CAN-Bus address switches, dip switches 1 to 4, are connected to the I/O expander as follows
- Switch 1 = GPIO 8 (GPB0)
- Switch 2 = GPIO 9 (GPB1)
- Switch 3 = GPIO 10 (GPB2)
- Switch 4 = GPIO 11 (GPB3)
The I/O expander is connected to I2C bus 1 on the Pi-Tron CM4 and has the address 0x20. Before trying to access the GPIOs, it's a good idea to first check that the I/O expander is really there.
-
Log in into the Pi-Tron CM4 and run the i2cdetect program to see if the I/O expander is present at address 0x20. Note: Instead of seeing the number 20, there should be 2 U's, which means this address is in use by a driver, this is correct.
sudo i2cdetect -y 1
-
The result should look something like the following table:
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: 50 -- 52 -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
-
Once the presents of the I/O expander is confirmed, the next step is to find out how it is named, meaning which device name and number it has received from the system. Usually the device name is something like gpiochipXY, e.g. gpiochip2.
-
Run the program gpiodetect to list all known GPIO devices.
pi@raspberrypi:~ $ gpiodetect gpiochip0 [pinctrl-bcm2711] (58 lines) gpiochip1 [raspberrypi-exp-gpio] (8 lines) gpiochip2 [mcp23017] (16 lines)
-
The I/O expander was assigned the device name gpiochip2, which can be identified by the driver/chip name mcp23017 in square brackets behind the device name. The result also tells us that the I/O expander has 16 lines (GPIOs), however only the GPIO 8, 9, 10 and 11 are of interest.
Note on signals
The signals of the dip switches for the CAN address at the I/O expander side are inverted, which means 1 = off and 0 = on.
-
Make sure all switch are in the bottom (off) position and then run the gpioget program to read the state of all four dip switches/GPIOs.
pi@raspberrypi:~ $ gpioget gpiochip2 8 9 10 11 1 1 1 1
-
The result should be four 1, confirming that all four switches are in off position.
-
Setting the first switch to on and running the gpioget program again, should show a different result, GPIO 8 should now be 0.
pi@raspberrypi:~ $ gpioget gpiochip2 8 9 10 11 0 1 1 1
-
To get a continuous readout, try using the watch command to run gpioget every second and refresh the result on the console.
watch -n 1 gpioget gpiochip2 8 9 10 11
-
This outputs the following information continuously.
Every 1.0s: gpioget gpiochip2 8 9 10 11 raspberrypi: Mon Jan 1 10:00:00 2022 1 0 0 1
-
The above example shows that switch 2 and 3 are in position on. The watch command can be aborted with Ctrl + C.
This concludes the How-To for the CAN-Bus address switches. The MCP23017 I/O expander should be active in the system and allow access to the GPIOs 8, 9, 10 and 11 which correspond to the dip switches 1 to 4. Of course these 4 switches can also be used for anything else where a physical hardware switch is needed to change a setting in software, not just for a CAN-Bus address setting. The libgpiod library should also offer options to access the GPIOs from different programing languages like C or Python.
Restrictions
- No known Restrictions
Related documentation
- MCP23017 16-Bit I/O Expander: https://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf
- Covering all aspects of the Raspberry Pi, the official documentation: https://www.raspberrypi.com/documentation
- Documentation for the raspi-config tool: https://www.raspberrypi.com/documentation/computers/configuration.html
- Linux I2C-Tools package: https://i2c.wiki.kernel.org/index.php/I2C_Tools
- Manpages i2c-tools: https://manpages.debian.org/bullseye/i2c-tools/index.html
- Manpages gpiodetect: https://manpages.debian.org/experimental/gpiod/gpiodetect.1.en.html
- Manpages gpioget: https://manpages.debian.org/experimental/gpiod/gpioget.1.en.html
- libgpiod Debian package information: https://packages.debian.org/source/bullseye/libgpiod
- lingpiod git repository: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git
- Raspberry Pi OS now requires users to create their own login credentials on first boot: https://www.raspberrypi.com/news/raspberry-pi-bullseye-update-april-2022