sparkfun MPR121 Hook-Up Manual Download Page 12

void setup()

    { 

//make sure the interrupt pin is an input and pulled hig

h

pinMode(irqPin, INPUT);
digitalWrite(irqPin, HIGH);

//configure serial out

      Serial.begin(9600);

// initalize I2C bus. Wiring lib not used. 
i2cInit();

// initialize mpr121
mpr121QuickConfig();

// Create and interrupt to trigger when a button
// is hit, the IRQ pin goes low, and the function getNum

ber is run. 

attachInterrupt(0,getNumber,LOW);

// prints 'Ready...' when you can start hitting numbers

      Serial.println("Ready...");
    } 

The setup loop starts by setting the interrupt pin as an input and pulling it 
high. The serial bus is started at 9600 bps. Next, the code initializes the I C 
communication lines without using the Wiring library.

The MPR121 chip is then configured with the proper sensitivity settings on 
the electrodes. The final step in the setup loop creates an interrupt in the 
code that will trigger when any of the buttons are hit.

void loop()

//You can put additional code here. The interrupt will run i

n the backgound. 

The loop function is actually empty currently, as the button presses use the 
interrupt we created in the setup loop. If you did have additional hardware 
you wanted to incorporate into your project, the code for it can live in this 
loop.

The last two functions in the example code tell the Arduino to check the 
electrode states for a button press and what to print out when a button 
press is detected.

2

Page 12 of 24

Summary of Contents for MPR121

Page 1: ...GPIO functionality on electrode pins 4 through 11 giving you a lot of freedom for setting up your project The sensor works from 1 6V to 3 3V The sensor isn t very current hungry drawing only around 2...

Page 2: ...he most versatile option of the three MPR121 products You can wire it up to any kind of electrode you want and as it is a simple breakout board does not have a particular microcontroller footprint it...

Page 3: ...ind the most up to date firmware for working with the breakout board available on GitHub Let s take a look and see exactly what the code is doing include mpr121 h include Wire h int irqpin 2 Digital 2...

Page 4: ...of the code is incredibly simple as it only calls a single function void loop readTouchInputs The function is actually described in the next section of the code The Arduino requests the electrode stat...

Page 5: ...uched Serial print pin Serial print i Serial println was just touched else if touchStates i 1 pin i is still being touched touchStates i 1 else if touchStates i 1 Serial print pin Serial print i Seria...

Page 6: ...or section of the code defines the threshold values for each electrode Each electrode must have a touch threshold and a release threshold for the Arduino to compare the current state of the electrode...

Page 7: ...register 0x5A ELE0_R REL_THRESH set_register 0x5A ELE1_T TOU_THRESH set_register 0x5A ELE1_R REL_THRESH set_register 0x5A ELE2_T TOU_THRESH set_register 0x5A ELE2_R REL_THRESH set_register 0x5A ELE3_T...

Page 8: ...0x0B set_register 0x5A ATO_CFGU 0xC9 USL Vdd 0 7 vdd 256 0xC9 3 3V set_register 0x5A ATO_CFGL 0x82 LS L 0 65 USL 0x82 3 3V set_register 0x5A ATO_CFGT 0xB5 Target 0 9 USL 0xB5 3 3V set_register 0x5A EL...

Page 9: ...ilities for your project using the MPR121 IC The shield itself has 9 touch pads on it conveniently numbered 1 9 in a 3x3 grid and has headers for 3 additional electrode connections The shield connects...

Page 10: ...eld However you don t have to do this to get the shield to function It s up to you Check out the Fritzing diagram below to see how your shield should look if you have added buttons on to your shield o...

Page 11: ...definitions This is laying out the keypad functionality ONE TWO etc and defining which electrode pin corresponds to each number As the comment states electrode pins 9 10 and 11 are not currently conne...

Page 12: ...de initializes the I C communication lines without using the Wiring library The MPR121 chip is then configured with the proper sensitivity settings on the electrodes The final step in the setup loop c...

Page 13: ...sed if touchstatus 1 j touchNumber if touchNumber 1 if touchstatus 1 SEVEN digits 7 else if touchstatus 1 FOUR digits 4 else if touchstatus 1 ONE digits 1 else if touchstatus 1 EIGHT digits 8 else if...

Page 14: ...s int touch touch mpr121Read 0x01 8 touch mpr121Read 0x00 return touch Now that you have basic communication up and running with your shield you can start integrating this into projects If you haven t...

Page 15: ...is very similar to the MPR121 breakout board but instead of having to attach your own electrodes this board comes with a 12 pin keypad built in The keypad has 5 lines that need to be connected to you...

Page 16: ...er to communicate with your keypad you ll want to download the example sketch available here Alternatively you can check for the most up to date firmware available in the GitHub repository This sketch...

Page 17: ...define POUND 8 define NINE 9 define SIX 10 define THREE 11 int irqpin 2 D2 uint16_t touchstatus char phoneNumber PHONE_DIGITS The second section of the code is the basic initialization of the serial...

Page 18: ...r Serial print nDialing for int i 0 i PHONE_DIGITS i Serial print phoneNumber i while 1 As you can see in the function loop for getPhoneNumber the Arduino checks the touchstatus register for each elec...

Page 19: ...if touchstatus 1 STAR phoneNumber i else if touchstatus 1 SEVEN phoneNumber i 7 else if touchstatus 1 FOUR phoneNumber i 4 else if touchstatus 1 ONE phoneNumber i 1 else if touchstatus 1 ZERO phoneNu...

Page 20: ...cSendByte MPR121_W write 0xB4 i2cWaitForComplete i2cSendByte address write register address i2cWaitForComplete i2cSendStart i2cSendByte MPR121_R write 0xB5 i2cWaitForComplete i2cReceiveByte TRUE i2cWa...

Page 21: ...e i2cSendByte address write register address i2cWaitForComplete i2cSendByte data i2cWaitForComplete i2cSendStop The mpr121QuickConfig function is then defined In this function all 12 of the electrodes...

Page 22: ...e ELE1_R REL_THRESH mpr121Write ELE2_T TOU_THRESH mpr121Write ELE2_R REL_THRESH mpr121Write ELE3_T TOU_THRESH mpr121Write ELE3_R REL_THRESH mpr121Write ELE4_T TOU_THRESH mpr121Write ELE4_R REL_THRESH...

Page 23: ...Q pin for an indication that an electrode has been pressed It then returns either a 1 or 0 as the value for checkInterrupt variable byte checkInterrupt void if digitalRead irqpin return 1 return 0 Now...

Page 24: ...tutorials have I C communication They can also both be used in conjunction with this sensor to act as a display for the button triggers or to monitor the temperature as well OpenSegment Hook Up Guide...

Reviews: