HowTo: Raspberry Pi - Evaluate MPR121 touch sensor via I2C and Node Red

Thanks to the I2C bus, the Raspberry Pi can be equipped with many practical sensors and actuators. A (in my opinion) very elegant possibility for user input is offered by the MPR121.

The MPR121 is a controller that allows you to evaluate up to 12 touch electrodes. You can build up to 12 electrodes, which can then be used as buttons. Depending on the structure of the electrode, you can also detect these touches through thin materials or even just by approach (without touching).

For example, I use this on my MagicMirror to manually turn the screen on and off.

Everything you need to read out the MPR121 via NodeRed and to react on keystrokes can be found in the following article.


Safety instructions

I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/

Affiliate links/advertising links

The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂 


Requirements

Helpful articles:
So that you can install new nodes, NodeRed should of course already be installed.
How to prepare a RaspberryPi and then install NodeRed on it is described in the following articles.

The following three articles describe what needs to be done to prepare the RaspberryPi:

In the following lists you will find all the parts you need to implement this article.

Required tool:

NumberLink
1xScrewdriver set  Buy at Amazon
1xSD card reader  Buy at Amazon

Required material:

NumberLink
1x Raspberry Pi  Buy at Amazon
1x Raspberry Pi power supply  Buy at Amazon
1x Raspberry Pi case  Buy at Amazon
1x Micro SD card 64GB  Buy at Amazon
1x MPR121 breakout board  Buy at Amazon
1x Dupont cable  Buy at Amazon


Connect MPR121 correctly

In order for your Raspberry Pi to communicate with the MPR121, you must of course connect it correctly to the GPIOs of the Raspberry Pi.

For this you have to connect the MPR121 breakout board to the Raspberry Pi as shown.

RasPiMPR121
3.3V3.3V
GNDGND
GPIO2SDA
GPIO3SCL

Enable I2C bus in the RaspberryPi configuration.

In order to use the I2C bus with the Raspberry Pi, it must first be activated. There are two ways to do this.

Activate I2C bus of the Raspberry Pi via SSH

To enable the I2C bus via SSH connection, you need to connect to your Raspberry Pi via SSH and then enter the following command.

sudo raspi-config

The displayed menu will appear.

Navigates in it with the arrow keys to the option

  • Interface options

and confirm with “Enter”.

Navigates in the next menu with the arrow keys to the entry

  • I2C

Confirm the dialog that appears by clicking with the arrow keys on

  • yes

and confirm with Enter.

A confirmation of the activation is then displayed again.

Now you can exit the menu by clicking on

  • finish

changes and confirms with “Enter”.

Activate I2C bus of the Raspberry Pi via the graphical interface

To enable the I2C bus via the graphical user interface, you must first click on the Raspberry Pi icon in the upper left corner.

Then opens the program

  • Raspberry Pi configuration

In the displayed window, switch to the tab

  • interfaces

sets the “I2C” section to “enabled” and confirms this by clicking “OK”


Log into the NodeRed configuration interface

Before you can edit your NodeRed configuration, you must - if activated - first log into the NodeRed configuration interface.

View of the login dialog. Here you have to enter the login data that you specified during the configuration of the login. You can find information about this in the article NodeRed - set up user login.

Installation of the required node

In order for NodeRed to be able to communicate with your already connected MPR121, you must first install the node “node-red-contrib-mpr121”. How you can install a node is in the article NodeRed – Install new nodes described.


Import Node Red code

In this section you will find the Node Red code you need. How you can import this into your NodeRed environment is in the article NodeRed - import and export node code described.

You can find the Node Red code in the Nerdiy Git repository at the following link:

The NodeCode below evaluates all 12 electrodes of the MRP121. For this to work it must be connected to the Raspberry Pi via I2C. You also have to set the correct I2C address.

For each electrode, the current measured value is displayed on the dashboard. This value changes depending on the environment and also whether a finger (or other) is nearby, which should trigger an action. To ensure that a proximity/touch can be detected reliably, you can also set the threshold value for each electrode in the dashboard. Just try it out. You can't really break anything 🙂 .

When you release an electrode, a signal is sent to the associated link node, which you can use to perform any switching action.

View of the Node Red flow in the NodeRed configuration view.

By clicking on the MPR121 Node you can configure the address of your MPR121. This must match the hardware configured address of the MPR121.

View of the flow for the first electrode.

Dashboard view

Here you can see the current reading of each electrode and also set the threshold for each electrode.


More articles on the topic

In the following category you will find more links about Rasperry Pi and Node Red.


Have fun with the project

I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂

PS Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂

Buy Me a Coffee at ko-fi.com       

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert

This site uses Akismet to reduce spam. Learn how your comment data is processed.