Skip to content

Commit 6e66cb8

Browse files
committed
MPR121 12-Channel Capacitive Sensor (I2C) module
1 parent 3112208 commit 6e66cb8

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

devices/MPR121.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Copyright (c) 2017 Standa Opichal, Gordon Williams. See the file LICENSE for copying permission. */
2+
/*
3+
Module for the MPR121 12-Channel Capacitive Sensor (I2C)
4+
5+
Based on the http://sonnycruz.blogspot.cz/2014/07/espruino.html code snippet
6+
and other available libraries https://github.com/adafruit?q=MPR121 libraries
7+
```
8+
I2C1.setup({scl:B6,sda:B7});
9+
10+
function ready() {
11+
setTimeout(function() {
12+
var keys = mpr.touched();
13+
console.log("Touched: " + keys);
14+
}, 200);
15+
}
16+
17+
var mpr = require("MPR121").connect(I2C1, ready);
18+
// or
19+
var mpr = require("MPR121").connect(I2C1, ready, { address: 0x5B });
20+
21+
```
22+
23+
Default address is 0x5A, if tied to 3.3V its 0x5B
24+
If tied to SDA its 0x5C and if SCL then 0x5D
25+
*/
26+
exports.connect = (i2c, callback, options) => {
27+
options = options || {};
28+
29+
let addr = options.address || 0x5A; // default address
30+
31+
let MPR = {
32+
read: (count) => i2c.readFrom(addr, count),
33+
write: (data) => i2c.writeTo(addr, data),
34+
35+
touched: () => {
36+
i2c.writeTo(addr, 0x00);
37+
var data = i2c.readFrom(addr, 2);
38+
return (data[1] << 8) || data[0];
39+
},
40+
41+
setThresholds: (touch, release) => {
42+
for (i=0; i<24; i+=2) {
43+
i2c.writeTo(addr, 0x41+i, touch);
44+
i2c.writeTo(addr, 0x42+i, release);
45+
}
46+
}
47+
};
48+
49+
// disable electrodes
50+
i2c.writeTo(addr, [0x5E,0x00]);
51+
52+
i2c.writeTo(addr, [0x2B,0x01]);
53+
i2c.writeTo(addr, [0x2C,0x01]);
54+
i2c.writeTo(addr, [0x2D,0x00]);
55+
i2c.writeTo(addr, [0x2E,0x00]);
56+
57+
i2c.writeTo(addr, [0x2F,0x01]);
58+
i2c.writeTo(addr, [0x30,0x01]);
59+
i2c.writeTo(addr, [0x31,0xFF]);
60+
i2c.writeTo(addr, [0x32,0x02]);
61+
62+
// touch, release thresholds
63+
MPR.setThresholds(15, 8);
64+
65+
// 0x10: 16uA charge current
66+
// 0x20: 32uA charge current (default)
67+
i2c.writeTo(addr, [0x5C, options.config1 || 0x20]);
68+
// 0x20: 0.5us encoding, 1ms period
69+
// 0x3A: 0.5us encoding, 18 samples, 4ms period (default)
70+
i2c.writeTo(addr, [0x5D, options.config2 || 0x20]);
71+
72+
// enable all electrodes
73+
i2c.writeTo(addr, [0x5E,0x8F]);
74+
75+
if (callback) callback(MPR);
76+
return MPR;
77+
};
78+

devices/MPR121.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!--- Copyright (c) 2017 Standa Opichal. Released under the MIT license. -->
2+
Module for the MPR121 12-Channel Capacitive Sensor (I2C)
3+
=====================
4+
5+
<span style="color:red">:warning: **Please view the correctly rendered version of this page at https://www.espruino.com/MPR121. Links, lists, videos, search, and other features will not work correctly when viewed on GitHub** :warning:</span>
6+
7+
* KEYWORDS: Module,MPR121,touch,capacitative
8+
9+
Use the [MPR121](/modules/MPR121.js) ([About Modules](/Modules)) module for reading which of the MPR121's 12 electrodes are touched
10+
=====================
11+
12+
This module communicates with the chip over I2C.
13+
14+
Basic usage:
15+
16+
```
17+
I2C1.setup({scl:B6,sda:B7});
18+
19+
function ready() {
20+
setTimeout(function() {
21+
var keys = mpr.touched();
22+
console.log("Touched: " + keys);
23+
}, 200);
24+
}
25+
26+
var mpr = require("MPR121").connect(I2C1, ready);
27+
// or
28+
var mpr = require("MPR121").connect(I2C1, ready, { address: 0x5B });
29+
```
30+
31+
**MPR121 module functions**
32+
33+
**touched()** - gives back a 12bit number where each high bit is an activated electrode/key
34+
35+
**setThresholds()** - sets the press/release treshold values
36+
37+
**write()** - writes the provided data (array) to the i2c bus
38+
39+
**read()** - reads the given number of bytes from the i2c bus

0 commit comments

Comments
 (0)