Using mpr121 toch sensor in openhab

I am trying to make touch switch with mpr121. It communicates over I2c. I am confused if openhab can take inputs via i2c. Is there any way to make it work.

Hello Arghya_Ghosh

Maybe a bit late, but anyway there is an answer.

Yes I have it working over I2c and it works very well too. I used the python library for reading the information. You can find this lib at

I made copy of the python library to my openhab python lib and made a small modification in this file by adding a function start.
The only change with the begin function is, that it doesn’t reset the device every time you call the function on a interrupt. The polling in the example didn’t work reliable at all. You miss a lot of signals with polling, it is slow and it takes a lot of CPU time. With the interrupt installed, it is very fast, reacts allways with hardly no CPU time.

The change in python lib MPR121 is shown below
The new function start is a copy of the begin function, but without the reset.

  def start(self, address=MPR121_I2CADDR_DEFAULT, i2c=None, **kwargs):
        # Assume we're using platform's default I2C bus if none is specified.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
            # Require repeated start conditions for I2C register reads.  Unfortunately
            # the MPR121 is very sensitive and requires repeated starts to read all
            # the registers.
        # Save a reference to the I2C device instance for later communication.
        self._device = i2c.get_i2c_device(address, **kwargs)
        return True  

Then I wrote 2 python programms for initing and reading the touch info.

#!/usr/bin/env python
import sys
import time
import MPR121 as MPR121

# Create MPR121 instance.
cap = MPR121.MPR121()

# Initialize communication with MPR121 using default I2C bus of device, and
# default I2C address (0x5A).     
if not cap.begin():      // This is the old function in the library
    print('Error initializing MPR121.  Check your wiring!')

import sys
import time
import MPR121 as MPR121

# Create MPR121 instance.
cap = MPR121.MPR121()

# init function in

if not cap.start():      // this is the new function in the library
   print('Error ininitalizing MPR121. Check your wiring!')

current_touched = cap.touched()
print (current_touched) 

I connected the mpr121 device to I2C bus and also connected the IRQ output of the mpr121 to an input on the pi

This input will be triggered as you touch one of the inputs 11 in my setup. The name Interrupt is just an item name, not a keyword or so.

Contact Interrupt "Interrupt mpr121 " {gpio="pin:11 debounce:1" }

In the rules a have added the following function

rule "mpr interrupt handler"
    Item Interrupt changed from OPEN to CLOSED     // interrupt mpr_121 geset  (= GPIO pin 11) 

       var tmp =  new Integer(executeCommandLine("sudo python /usr/share/openhab/python/", 5000))   //read the string and map it on a tmp var.

       var tmp2 =  new DecimalType(tmp)  

       var state = tmp2.toBigDecimal.toBigInteger

       if(state.testBit(0))  // for the first input
                // do something you like
       if(state.testBit(1)) // for the next etc....
                // do something you like

And a rule when the system is started

rule "init the system"
		System started

    	executeCommandLine("sudo python /usr/share/openhab/python/", 5000)  // init touchsensor mpr121 


don’t forget to change owner of your python files with
sudo chown 777 *.py

Good luck, and if you have any questions, be my guest.

Thanks for your informations. works, but returns allway 0, also if I start from command line.
I modified and used start.
It looks like using begin function.
What can I do?

1 Like

Did you make a copy of the MPR121 python lib in same directory where the file is?

Thanks for your quick answer.
Yes I do as described once more. I use the actual openHABian-image on Raspi3.

[10:16:58] openhabian@openHABianPi:/etc/openhab2/scripts$ python /etc/openhab2/scripts/
Adafruit MPR121 Capacitive Touch Sensor Test
Press Ctrl-C to quit.
8 touched!
4 touched!
0 touched!
4 released!
8 released!
0 released!
^CTraceback (most recent call last):
  File "/etc/openhab2/scripts/", line 73, in <module>
[10:43:48] openhabian@openHABianPi:/etc/openhab2/scripts$ sudo python /etc/openhab2/scripts/ begin _reset
[10:47:39] openhabian@openHABianPi:/etc/openhab2/scripts$ sudo python /etc/openhab2/scripts/ start touched
[10:47:54] openhabian@openHABianPi:/etc/openhab2/scripts$ sudo python /etc/openhab2/scripts/ start touched
[10:48:02] openhabian@openHABianPi:/etc/openhab2/scripts$

Before start read I entered some keys and the interruped changed to LOW after this back to HIGH.

Did you connect the IRQ interrupt line of the mpr121 tot an input of the pi?

All together you need 5 connections between the mpr121 and your pi.

The touch sensor test is based on polling and does not use an interrupt. This will work, but only as long as the program is running. That is the reason why I made it based on an interrupt.

Yes, the interruped works, openHAB-rule starts the python-programm und gets back a 0 also.
Thats, why I disconnect the IRQ-line to openHAB, extend the program with the log-infos and tested at command-line.
It looks like, reset is running, but it doesn’t.
Could it be, that a Raspi 3 or my python work different?

I run it on a raspi 3 with standard python on it.
This you also changed owner of the python files. It needs to read

“-rwxrwxrwx 1 root root 8907 feb 11 2017”

I don’t changed the owner, but I started as sudo on command-line, so I think, it isn’t a authority-problem.