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.
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.
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. 🙂
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:
- RaspberryPi – setup for nerdiys!
- RaspberryPi – The first configuration!
- RaspberryPi – Control the RaspberryPi via SSH
- NodeRed – Installing NodeRed on the RaspberryPi
- NodeRed – Install new nodes
- NodeRed - import and export node code
In the following lists you will find all the parts you need to implement this article.
|Raspberry Pi Buy at Amazon
|Raspberry Pi power supply Buy at Amazon
|Raspberry Pi case Buy at Amazon
|Micro SD card 64GB Buy at Amazon
|MPR121 breakout board Buy at Amazon
|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.
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.
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
Confirm the dialog that appears by clicking with the arrow keys on
and confirm with Enter.
A confirmation of the activation is then displayed again.
Now you can exit the menu by clicking on
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
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.
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.
By clicking on the MPR121 Node you can configure the address of your MPR121. This must match the hardware configured address of the MPR121.
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. 🙂