Access a MPR121 breakout using Node.js from a Raspberry Pi or BeagleBone Black.
This library requires Node.js v6.0.0 or higher.
$ npm install adafruit-mpr121
On a Raspberry Pi configure I2C following these instructions. E.g.
sudo apt-get update
sudo apt full-upgrade -y
sudo apt-get install i2c-tools
Make sure you follow these steps to enable autoloading of I2C Kernel module.
Install Node.js if not yet installed, e.g. to install Node v10:
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
Reboot for above changes to take affect:
sudo reboot
You can check Node version like this:
node -v
# v10.11.0
To install adafruit-mpr121
in your own Node app:
mkdir myapp
cd myapp
npm init
npm install adafruit-mpr121 --save
To run node in interactive mode on command line:
node
Example code below should now work.
const MPR121 = require('adafruit-mpr121'),
mpr121 = new MPR121(0x5A, 1);
// listen for touch events
mpr121.on('touch', (pin) => console.log(`pin ${pin} touched`));
// listen for release events
mpr121.on('release', (pin) => console.log(`pin ${pin} released`));
// listen for changes to the state of a specific pin
mpr121.on(3, (state) => console.log(`pin 3 is ${state ? 'touched' : 'released'}`));
// check the current state of a specific pin synchronously
const state = mpr121.isTouched(2);
console.log(`pin 2 is ${state ? 'touched' : 'released'}`);
Copyright (c) 2016 Adafruit Industries. Licensed under the MIT license.