MBTechWorks.com    

MPR121 Capacitive Touch for Raspberry Pi Project

October 19, 2015

How to setup an MPR121 board with the Raspberry Pi

MPR121 Capacitive Touch Fun image

MPR121 Capacitive Touch Fun

The MPR121 capacitive touch chip is a marvel and lends itself to some fun projects for the Pi. This project will involve both hardware and software work. For programming, we will use Python. We will demonstrate the connections for both the MPR121 Capacitive Touch keypad and the MPR121 Capacitive Touch board for use with external touch pads or conductive “things” – like aluminum strips or fruit!  The following steps are done from the command line screen (or LXTerminal if running Raspbian desktop).

A. Prepare the Raspbian OS

1.  Make sure your Raspbian OS is up to date (some older versions don’t support needed capabilities).  Connect the Pi to the internet and do the following:

  • type “sudo apt-get update” and press Enter. Allow this step to complete
  • type “sudo apt-get upgrade”.  Answer “y” when prompted. This takes awhile.
  • Reboot with:  sudo reboot

2. Prepare I2C communications.

  • sudo apt-get install python-smbus
  • sudo apt-get install i2c-tools.  The OS update from step one may have done this, so if you see message not upgraded, it is okay.
  • sudo raspi-config
  • Select 8 Advance Options
raspi-config screen shot

raspi-config

  • On the next screen, select: A7  I2C    Enable/Disable automatic loading.
    • Select “Yes” to Would you like the ARM I2C interface to be enabled?
    • Select “Yes” to Would you like the i2C kernal module to be loaded by default?
    • Return to main screen and select <Finish> then  reboot <No>
  • Edit the modules file:
    • sudo  nano  /etc/modules
    • Add i2c-dev below last line in file
    • Hold the Ctrl key and press “O” to Writeout (save) the file.  Press Enter at prompt.  Hold Ctrl and press “X” to exit.
etc modules image

/etc/modules

  • Check kernel version:  uname  -r    If 3.18 or higher perform the next step:
    • sudo nano /boot/config.txt
    • Add line:  dtparam=i2c1=on to the end of the file
    • Ctrl O to writeout then Enter and Ctrl X to exit
/boot/config.txt image

/boot/config.txt

  • Reboot the Pi:  sudo reboot
  • You can confirm I2C with command:  sudo i2cdetect  -y  1   for Pi 2
    or sudo i2cdetect  -y  0  (zero)  for Pi 1.
i2cdetect screen shot

i2cdetect

  • Raspbian is now ready for the MPR121 board.

B.  Connecting MPR121 boards

We will show two different boards, but in both examples, the connection to the Pi is the same.  Take care to connect correctly and double check your work to avoid permanent damage that may result to either board through incorrect connections.

MPR121 Capacitive Touch Keypad

There are four connections.

  • GND  (Ground)
  • VCC   (+3.3V)
  • SDA  (Serial DAta line)
  • SCL   (Serial Clock line)

We will not use the IRQ or ADD lines if present on board.

1.  Solder the header pins onto the board if not already done.

MPR121 Keypad image

MPR121 Keypad

2.  Locate the four connections that we will use: GND, SDA, SCL and VCC.

MPR121 Keypad Connections image

MPR121 Keypad Connections

3.  Locate the connections that we will use on the Pi.

MPR121 Raspberry Pi pins image

MPR121 Raspberry Pi pins

Pi Pinout left side

Pi Pinout left side

4.  With Pi powered off, use female-to-female jumper wire to connect each of the line to the boards, connecting GND to GND, VCC to VCC, SDA to SDA and SCL to SCL.

MPR121 and Pi connections

MPR121 and Pi connections

>

MPR121 Capacitive Touch Board

Like the keypad, there are four connections. Note, however, that VCC is labeled +3.3v on this board

  • GND  (Ground)
  • +3.3V (VCC)
  • SDA  (Serial DAta line)
  • SCL   (Serial Clock line)

1.  Solder the header pins onto the board if not already done.

2.  Locate the four connections we will use: GND, SDA, SCL and +3.3v.  IRQ and ADD are not used.

MPR121 Capacitive Touch board

MPR121 Capacitive Touch board

3.  Locate the connections we will use on the Pi.

MPR121 Raspberry Pi pins

MPR121 Raspberry Pi pins

Pi Pinout left side

Pi Pinout left side

4.  With Pi powered off, use female-to-female jumper wire to connect each of the lines to the boards, connecting GND to GND, +3.3V to +3.3V, SDA to SDA and SCL to SCL.

MPR121 to Pi Connections

MPR121 to Pi Connections

5.  Connect some female to male jumper wires to any of pins 0 – 11 on the right side of the mpr121 board. The will become the inputs.  You can attach the male end of the plug to aluminum foil strips, coins and even insert pin into fruit!

6.  Connect the Pi to an amplified speaker through the audio jack.  We happen to like this one, but any amplified speaker will do.

Portable Speaker

Portable Speaker

C. Software

Now that you are connected, you need some software to read touches and play some sounds.

1.  Connect Pi to internet and power up.

2.  Download our capacitive touch python program with:  wget  -O    /home/pi/cap-touch.py    https://www.mbtechworks.com/files/cap-touch.py

3.  Set the Pi volume by typing following in command line:  amixer set PCM 100%

4.  Confirm you current working directory is “/home/pi” with:  pwd

5.  Start program with:  sudo python cap-touch.py

6. Have fun!!

D.  Notes

Threshold Sensitivity

You can set the sensitivity of the input probes by adjusting two values in cap-touch.py

# Global Constants

TOU_THRESH = 06
REL_THRESH = 12

Use: sudo nano cap-touch.py to edit the python file. Find the constants shown above. Increase 06 and 12 numbers to make less sensitive or decrease to make more sensitive. The release threshold (REL_THRESH) must always be larger than the touch threshold. The range limit is 0 – 255. Larger numbers, however, will make the the board so insensitive as to be unresponsive.  Numbers near zero, may make the board react repeatedly to false signals.

Audio Out

If Pi does not play sounds, try changing the audio output as follows.

From command line (launch LXTerminal if in GUI desktop mode), type:
“sudo amixer cset  numid=3  0”  (<-zero) and press Enter

This will set audio output to automatic – for either HDMI sound out or audio jack sound out.

Sound Files

We are using the sound files from Sonic Pi’s directory. You can view the complete list of files by listing the directory:  ls  /opt/sonic-pi/etc/samples

To change the defaults in the cap-touch.py edit the file: sudo nano cap-touch.py. Find the soundsList list. You can change any of the existing *.wav names in the list with others that are in the Sonic Pi directory.

Enjoy!


Watch a video of 3 different configurations in action
Article closing text image