### Read Touch States with Adafruit_MPR121::touched() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Reads the touch state of all 12 electrodes simultaneously, returning a 12-bit integer. Use bitmasking to detect individual touch and release transitions. This example demonstrates how to track when a finger is placed on or lifted from a pad. ```cpp #ifndef _BV #define _BV(bit) (1 << (bit)) #endif uint16_t lasttouched = 0; uint16_t currtouched = 0; void loop() { currtouched = cap.touched(); // bitmask of all 12 channels for (uint8_t i = 0; i < 12; i++) { // Finger just placed on pad i if ((currtouched & _BV(i)) && !(lasttouched & _BV(i))) { Serial.print("Channel "); Serial.print(i); Serial.println(" touched"); } // Finger just lifted from pad i if (!(currtouched & _BV(i)) && (lasttouched & _BV(i))) { Serial.print("Channel "); Serial.print(i); Serial.println(" released"); } } lasttouched = currtouched; // save state for next iteration delay(50); } ``` -------------------------------- ### Initialize MPR121 Sensor with Adafruit_MPR121::begin() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Initializes the MPR121 sensor, verifies its presence on the I2C bus, and configures default settings. Must be called before any other library methods. Supports custom Wire instances and thresholds. ```cpp #include #include "Adafruit_MPR121.h" Adafruit_MPR121 cap = Adafruit_MPR121(); void setup() { Serial.begin(9600); // Default I2C address 0x5A; address pin options: // GND -> 0x5A (default) // 3.3V -> 0x5B // SDA -> 0x5C // SCL -> 0x5D if (!cap.begin(0x5A)) { Serial.println("MPR121 not found, check wiring?"); while (1); // halt } Serial.println("MPR121 found!"); // Custom Wire instance, custom thresholds, autoconfig disabled: // cap.begin(0x5A, &Wire1, 15, 8, false); } ``` -------------------------------- ### Adafruit_MPR121::begin() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Initializes the MPR121 sensor, verifies its presence on the I2C bus, and configures default settings. This method must be called before any other library functions. It returns `true` on success and `false` otherwise. ```APIDOC ## Adafruit_MPR121::begin() ### Description Initializes the MPR121 sensor, verifies its presence on the I2C bus, and configures default settings. This method must be called before any other library functions. It returns `true` on success and `false` otherwise. ### Method `bool begin(uint8_t address = 0x5A, TwoWire *theWire = &Wire, uint16_t touch_threshold = MPR121_TOUCH_THRESHOLD_DEFAULT, uint16_t release_threshold = MPR121_RELEASE_THRESHOLD_DEFAULT, bool autoconfig = true)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include "Adafruit_MPR121.h" Adafruit_MPR121 cap = Adafruit_MPR121(); void setup() { Serial.begin(9600); if (!cap.begin(0x5A)) { Serial.println("MPR121 not found, check wiring?"); while (1); } Serial.println("MPR121 found!"); } ``` ### Response #### Success Response (true) Returns `true` if the sensor was successfully initialized. #### Response Example `true` ``` -------------------------------- ### Enable Autoconfiguration with Adafruit_MPR121::setAutoconfig() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Enables automatic calibration of charge current (CDC) and charge time (CDT) for each electrode. This is useful for optimizing performance across different conditions. Disable if manual tuning is preferred. ```cpp #include #include "Adafruit_MPR121.h" Adafruit_MPR121 cap = Adafruit_MPR121(); void setup() { Serial.begin(115200); Wire.begin(); if (!cap.begin(0x5A, &Wire)) { Serial.println("MPR121 not found!"); while (1); } // Enable autoconfig — MPR121 calibrates CDC/CDT on startup cap.setAutoconfig(true); Serial.println("Autoconfig enabled. Electrodes self-calibrated."); // Disable again if manual CDC/CDT tuning is preferred: // cap.setAutoconfig(false); } ``` -------------------------------- ### Manual Register Write for Debounce and Charge Current Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Demonstrates writing directly to MPR121 registers to manually set debounce parameters and charge current for a specific channel. Use for advanced configuration not covered by the public API. ```cpp void setup() { cap.begin(0x5A); // Manually set debounce: 3 touch samples + 3 release samples before event fires // Bits [2:0] = touch debounce, bits [6:4] = release debounce cap.writeRegister(MPR121_DEBOUNCE, 0b00110011); // Manually set charge current for channel 0 to 32 µA (register 0x5F) cap.writeRegister(0x5F, 32); } ``` -------------------------------- ### Adjust Sensitivity with Adafruit_MPR121::setThresholds() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Sets the touch and release detection thresholds for all channels. Higher values decrease sensitivity. The touch threshold should be higher than the release threshold to provide hysteresis. Defaults are used if not specified. ```cpp void setup() { // ...begin() first... cap.begin(0x5A); // More sensitive: lower touch threshold (8), smaller hysteresis gap cap.setThresholds(8, 4); // Less sensitive: higher touch threshold (20), larger hysteresis gap // cap.setThresholds(20, 10); // Defaults used by begin() are: // touch = MPR121_TOUCH_THRESHOLD_DEFAULT (12) // release = MPR121_RELEASE_THRESHOLD_DEFAULT (6) } ``` -------------------------------- ### Direct Register Access for Charge Configuration Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Dumps the charge current (CDC) and charge time (CDT) registers for each channel. Intended for advanced users to inspect or debug internal state. ```cpp void dump_charge_config() { Serial.println("CHAN CDC CDT_low CDT_high"); for (int chan = 0; chan < 12; chan++) { uint8_t cdc = cap.readRegister8(0x5F + chan); // charge current uint8_t cdt_reg = cap.readRegister8(0x6C + (chan / 2)); // paired CDT register uint8_t cdt = (chan % 2 == 0) ? (cdt_reg & 0x07) // low nibble : ((cdt_reg >> 4) & 0x07); // high nibble Serial.print(chan); Serial.print("\t"); Serial.print(cdc); Serial.print("\t"); Serial.println(cdt); } } void setup() { Serial.begin(115200); cap.begin(0x5A, &Wire); cap.setAutoconfig(true); dump_charge_config(); // inspect values after autoconfig } ``` -------------------------------- ### Read Per-Channel Baseline and Filtered Data Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Reads and prints the baseline capacitance and filtered data for each of the 12 channels. Useful for diagnosing drift issues and monitoring touch events. ```cpp void loop() { Serial.print("Base: "); for (uint8_t i = 0; i < 12; i++) { Serial.print(cap.baselineData(i)); Serial.print("\t"); } Serial.print(" | Filt: "); for (uint8_t i = 0; i < 12; i++) { Serial.print(cap.filteredData(i)); Serial.print("\t"); } Serial.println(); // Deviation = baseline - filtered; touch triggers when deviation > threshold delay(200); } ``` -------------------------------- ### Configure Multiple MPR121 Sensors on One I2C Bus Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Initializes up to four MPR121 sensors on the same I2C bus by assigning unique addresses (0x5A-0x5D). Each sensor instance allows access to 12 touch channels, providing a total of 48 channels. ```cpp #include #include "Adafruit_MPR121.h" Adafruit_MPR121 cap1 = Adafruit_MPR121(); // ADDR → GND = 0x5A Adafruit_MPR121 cap2 = Adafruit_MPR121(); // ADDR → 3.3V = 0x5B Adafruit_MPR121 cap3 = Adafruit_MPR121(); // ADDR → SDA = 0x5C Adafruit_MPR121 cap4 = Adafruit_MPR121(); // ADDR → SCL = 0x5D void setup() { Serial.begin(115200); if (!cap1.begin(0x5A)) { Serial.println("cap1 not found"); while(1); } if (!cap2.begin(0x5B)) { Serial.println("cap2 not found"); while(1); } if (!cap3.begin(0x5C)) { Serial.println("cap3 not found"); while(1); } if (!cap4.begin(0x5D)) { Serial.println("cap4 not found"); while(1); } Serial.println("All 4 sensors found — 48 touch channels available"); } void loop() { uint16_t t1 = cap1.touched(); uint16_t t2 = cap2.touched(); uint16_t t3 = cap3.touched(); uint16_t t4 = cap4.touched(); // Process 48 virtual channels: channel N on sensor S is bit N of tS delay(10); } ``` -------------------------------- ### Adafruit_MPR121::baselineData() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Reads the 10-bit baseline capacitance for a single channel (0-12). This value tracks slow environmental drift and is useful for diagnosing drift issues. ```APIDOC ## `Adafruit_MPR121::baselineData()` — Read per-channel baseline value Returns the 10-bit baseline capacitance for a single channel (0–12). The baseline tracks slow drift in the environment; the difference between `filteredData()` and `baselineData()` is what the threshold is compared against. Useful for diagnosing drift issues. ```cpp void loop() { Serial.print("Base: "); for (uint8_t i = 0; i < 12; i++) { Serial.print(cap.baselineData(i)); Serial.print("\t"); } Serial.print(" | Filt: "); for (uint8_t i = 0; i < 12; i++) { Serial.print(cap.filteredData(i)); Serial.print("\t"); } Serial.println(); // Deviation = baseline - filtered; touch triggers when deviation > threshold delay(200); } ``` ``` -------------------------------- ### Multiple Sensors on One I2C Bus Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Demonstrates how to use multiple MPR121 sensors on a single I2C bus by assigning different addresses (0x5A-0x5D) to each sensor instance. ```APIDOC ## Multiple Sensors on One I2C Bus Up to four MPR121 sensors can share a single I2C bus by tying the ADDR pin to different potentials, giving addresses 0x5A–0x5D. Each sensor is represented by its own `Adafruit_MPR121` instance. ```cpp #include #include "Adafruit_MPR121.h" Adafruit_MPR121 cap1 = Adafruit_MPR121(); // ADDR → GND = 0x5A Adafruit_MPR121 cap2 = Adafruit_MPR121(); // ADDR → 3.3V = 0x5B Adafruit_MPR121 cap3 = Adafruit_MPR121(); // ADDR → SDA = 0x5C Adafruit_MPR121 cap4 = Adafruit_MPR121(); // ADDR → SCL = 0x5D void setup() { Serial.begin(115200); if (!cap1.begin(0x5A)) { Serial.println("cap1 not found"); while(1); } if (!cap2.begin(0x5B)) { Serial.println("cap2 not found"); while(1); } if (!cap3.begin(0x5C)) { Serial.println("cap3 not found"); while(1); } if (!cap4.begin(0x5D)) { Serial.println("cap4 not found"); while(1); } Serial.println("All 4 sensors found — 48 touch channels available"); } void loop() { uint16_t t1 = cap1.touched(); uint16_t t2 = cap2.touched(); uint16_t t3 = cap3.touched(); uint16_t t4 = cap4.touched(); // Process 48 virtual channels: channel N on sensor S is bit N of tS delay(10); } ``` ``` -------------------------------- ### Adafruit_MPR121::setAutoconfig() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Enables or disables automatic calibration of electrode settings. When enabled, the MPR121 automatically optimizes charge current (CDC) and charge time (CDT) for each electrode upon startup, based on NXP recommendations. ```APIDOC ## Adafruit_MPR121::setAutoconfig() ### Description Enables or disables automatic calibration of electrode settings. When enabled, the MPR121 automatically optimizes charge current (CDC) and charge time (CDT) for each electrode upon startup, based on NXP recommendations. ### Method `void setAutoconfig(bool autoconfig)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include "Adafruit_MPR121.h" Adafruit_MPR121 cap = Adafruit_MPR121(); void setup() { Serial.begin(115200); Wire.begin(); if (!cap.begin(0x5A, &Wire)) { Serial.println("MPR121 not found!"); while (1); } // Enable autoconfig — MPR121 calibrates CDC/CDT on startup cap.setAutoconfig(true); Serial.println("Autoconfig enabled. Electrodes self-calibrated."); } ``` ### Response None ``` -------------------------------- ### Adafruit_MPR121::readRegister8() / readRegister16() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Provides direct register access to read 8-bit or 16-bit values from any MPR121 register by address. Intended for advanced users for inspecting internal state. ```APIDOC ## `Adafruit_MPR121::readRegister8()` / `readRegister16()` — Direct register access Read an 8-bit or 16-bit value from any MPR121 register by address. Intended for advanced users who need to inspect or debug internal state such as CDC (charge current) and CDT (charge time) registers not exposed through the higher-level API. ```cpp void dump_charge_config() { Serial.println("CHAN CDC CDT_low CDT_high"); for (int chan = 0; chan < 12; chan++) { uint8_t cdc = cap.readRegister8(0x5F + chan); // charge current uint8_t cdt_reg = cap.readRegister8(0x6C + (chan / 2)); // paired CDT register uint8_t cdt = (chan % 2 == 0) ? (cdt_reg & 0x07) // low nibble : ((cdt_reg >> 4) & 0x07); // high nibble Serial.print(chan); Serial.print("\t"); Serial.print(cdc); Serial.print("\t"); Serial.println(cdt); } } void setup() { Serial.begin(115200); cap.begin(0x5A, &Wire); cap.setAutoconfig(true); dump_charge_config(); // inspect values after autoconfig } ``` ``` -------------------------------- ### Adafruit_MPR121::writeRegister() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Writes an 8-bit value to any MPR121 register. This method automatically handles mode switching for configuration registers. ```APIDOC ## `Adafruit_MPR121::writeRegister()` — Direct register write Writes an 8-bit value to any MPR121 register. Automatically places the device into Stop Mode before writing (required for most configuration registers) and restores Run Mode afterward. Used internally and available for advanced configuration not covered by the public API. ```cpp void setup() { cap.begin(0x5A); // Manually set debounce: 3 touch samples + 3 release samples before event fires // Bits [2:0] = touch debounce, bits [6:4] = release debounce cap.writeRegister(MPR121_DEBOUNCE, 0b00110011); // Manually set charge current for channel 0 to 32 µA (register 0x5F) cap.writeRegister(0x5F, 32); } ``` ``` -------------------------------- ### Adafruit_MPR121::setThresholds() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Adjusts the touch and release sensitivity thresholds for all 12 electrodes. These values determine how much the filtered data must deviate from the baseline to register a touch or release event. A larger gap between touch and release thresholds provides hysteresis. ```APIDOC ## Adafruit_MPR121::setThresholds() ### Description Adjusts the touch and release sensitivity thresholds for all 12 electrodes. These values determine how much the filtered data must deviate from the baseline to register a touch or release event. A larger gap between touch and release thresholds provides hysteresis. ### Method `void setThresholds(uint16_t touch, uint16_t release)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp void setup() { // ...begin() first... cap.begin(0x5A); // More sensitive: lower touch threshold (8), smaller hysteresis gap cap.setThresholds(8, 4); // Less sensitive: higher touch threshold (20), larger hysteresis gap // cap.setThresholds(20, 10); } ``` ### Response None ``` -------------------------------- ### Read Filtered Data with Adafruit_MPR121::filteredData() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Reads the 10-bit digitally filtered capacitance reading for a specific channel. This value passes through three stages of digital filtering to reduce noise. Useful for visualizing raw capacitance and tuning thresholds. ```cpp void loop() { Serial.print("Filtered ADC readings: "); for (uint8_t i = 0; i < 12; i++) { uint16_t val = cap.filteredData(i); Serial.print(val); if (i < 11) Serial.print("\t"); } Serial.println(); // Example output: 512 510 508 513 ... (values drop when touched) delay(100); } ``` -------------------------------- ### Adafruit_MPR121::touched() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Reads the touch state of all 12 electrodes simultaneously. Returns a 12-bit integer where each bit corresponds to the touch state of an electrode (LSB for channel 0, MSB for channel 11). ```APIDOC ## Adafruit_MPR121::touched() ### Description Reads the touch state of all 12 electrodes simultaneously. Returns a 12-bit integer where each bit corresponds to the touch state of an electrode (LSB for channel 0, MSB for channel 11). ### Method `uint16_t touched()` ### Parameters None ### Request Example ```cpp #ifndef _BV #define _BV(bit) (1 << (bit)) #endif uint16_t lasttouched = 0; uint16_t currtouched = 0; void loop() { currtouched = cap.touched(); // bitmask of all 12 channels for (uint8_t i = 0; i < 12; i++) { // Finger just placed on pad i if ((currtouched & _BV(i)) && !(lasttouched & _BV(i))) { Serial.print("Channel "); Serial.print(i); Serial.println(" touched"); } // Finger just lifted from pad i if (!(currtouched & _BV(i)) && (lasttouched & _BV(i))) { Serial.print("Channel "); Serial.print(i); Serial.println(" released"); } } lasttouched = currtouched; // save state for next iteration delay(50); } ``` ### Response #### Success Response (uint16_t) Returns a 16-bit unsigned integer representing the touch state of the 12 electrodes. #### Response Example `0x0003` (Channels 0 and 1 are touched) ``` -------------------------------- ### Adafruit_MPR121::filteredData() Source: https://context7.com/adafruit/adafruit_mpr121/llms.txt Retrieves the digitally filtered capacitance reading for a specific electrode. This 10-bit value passes through multiple filtering stages to reduce noise and can be used for visualizing capacitance levels or tuning thresholds. ```APIDOC ## Adafruit_MPR121::filteredData() ### Description Retrieves the digitally filtered capacitance reading for a specific electrode. This 10-bit value passes through multiple filtering stages to reduce noise and can be used for visualizing capacitance levels or tuning thresholds. ### Method `uint16_t filteredData(uint8_t electrode)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp void loop() { Serial.print("Filtered ADC readings: "); for (uint8_t i = 0; i < 12; i++) { uint16_t val = cap.filteredData(i); Serial.print(val); if (i < 11) Serial.print("\t"); } Serial.println(); // Example output: 512 510 508 513 ... (values drop when touched) delay(100); } ``` ### Response #### Success Response (uint16_t) Returns the 10-bit filtered ADC value for the specified electrode. #### Response Example `512` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.