Using I2C Buttons and LEDs with MCP23017

Basic Information:

Printer Model: Voron2.4
MCU / Printerboard: RPi / Fysetc Spider 2.3
klippy.log
klippy_VoronGold.log (67.6 KB)

Fill out above information and in all cases attach your klippy.log file (use zip to compress it, if too big). Pasting your printer.cfg is not needed
Be sure to check our “Knowledge Base” Category first. Most relevant items, e.g. error messages, are covered there

Describe your issue:

Trying to use some buttons and leds with klipper. They are attached to my RPi via I2C by a MCP23017-Chip. Functionality is tested with a simple python-script, so the hardware works, but not within klipper. I defined a additional mcu i2c_rpi in my printer.cfg. But how do I access the buttons and leds now?
The example configuration in config/sample-raspberry-pi.cfg is very poor explained and I don´t know how to access the buttons and leds now:

# Example 3:Using a pin on another gpiochip connected to the RPi
# (in this case on an MCP23017)
[fan]
pin: host:gpiochip2/gpio7

Where do I define the I2C-address and the speed. How do I set led states and read buttons now from within klipper?
Any help would be appreciated

This is not possible since Klipper does not support this chip.
Your only option currently is to use regular GPIO pins.

Ok!
Strange!
As the chip is explicit mentioned in the sample configuration…

Did you think about about an RPi pico Raspberry Pi Pico + ADXL345: Portable resonance measurement

Quite cheap.

Good luck, hcet14

No that was not my intention.
But no problem. I will write a small python script which reacts to the button presses and than I will send a macro to the printer queue.
This should work either.

It seems I have to correct myself:

While it is true that Klipper cannot directly address this chip, it seems that the Linux Kernel can.
This means that this chip should simply appear as another gpiochipN where N is the number as assigned by the Kernel.
Thus, Klipper should be able to transparently access the GPIOs connected there without having to know where they come from.

The command gpiodetect should list the available GPIO chips as detected by the Linux kernel and if it appears there you should be able to do something like:

pin: host:gpiochip2/gpio7

With:

Thanks for your help and efforts, not to give up!
Your comment brought me to the solution:
I activated i2C over raspi-config and than i2cdetect -y 1 shows the address (which was 0x20 in my case). But gpiodetect showed only the “normal” GPIO ports from the raspberry pi, not the mcp:

$ gpiodetect 
gpiochip0 [pinctrl-bcm2711] (58 lines)
gpiochip1 [raspberrypi-exp-gpio] (8 lines)

To make the MCP23017 visible and usable with gpiodetect it is neccessary to load the dedicated driver overlay for this chip so the kernel can handle it as a GPIO. This has to be done by adding the overlay into /boot/config.txt:

dtoverlay=mcp23017,addr=0x20,noints=1

Only activating i2C over raspi-config is not enough to use the chip as GPIO!
Now gpiodetect shows:

$ gpiodetect
gpiochip0 [pinctrl-bcm2711] (58 lines)
gpiochip1 [raspberrypi-exp-gpio] (8 lines)
gpiochip2 [mcp23017] (16 lines)

I have´t testet it in klipper until now but I think now it should work as you described above.
Regards

4 Likes

Good find with loading the relevant DTO! Thanks for sharing.
Indeed, the gpiodetect now looks like Klipper should be able to address these GPIOs.

So the output via klipper works!

[mcu rpi]
serial: /tmp/klipper_host_mcu

Then you can define output-pins (here attached at line 8 and 9 of the mcp23017):

[output_pin myLED8]
pin: rpi:gpiochip2/gpio8

[output_pin myLED9]
pin: rpi:gpiochip2/gpio9

And use them at the console with:

SET_PIN PIN=myLED8 VALUE=1

Now I have to think about catching inputs from the attached buttons…
Perhaps I will do that with a python script.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.