Hello friends, I hope you all are doing great. Today, I am going to share the ESP32 Capacitive Touch Light Panel with MPR121 - Proteus Simulation, the ninety-third project of our IoT Projects series. If you have searched for an ESP32 touch switch, how to use the MPR121 capacitive touch sensor, a touch slider dimmer, or how capacitive touch really works, here is a complete wall panel in a Proteus simulation. Modern light switches have no buttons at all: a sheet of glass, some copper behind it, and a chip that notices when your finger comes close.

This panel has twelve pads, read by an MPR121 on the ESP32's I2C bus. Four pads are keys: a tap toggles a light, a one-second hold switches everything off (or everything on). The other eight sit in a row and form a slider: slide your finger along it and a 12 V lamp follows, dimmed by PWM through a logic-level MOSFET. The ESP32 works out where the finger is between two pads from how strongly each one feels it. An LCD shows the state, and a dashboard on the Wi-Fi controls everything, adds a child lock and shows each pad's raw capacitance reading live. In this tutorial, we will go through the circuit, how a capacitive pad and the MPR121 measure a finger, the baseline and the two thresholds, how a slider finds a position between pads, how to tell a tap from a hold, and the code. Then we tap, swipe and hold in the simulation and watch the numbers. At the end, you can download the project with the sketch and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the ESP32 capacitive touch light panel in Proteus simulation:

Figure: The capacitive touch light panel in a Proteus simulation.
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Capacitive Touch Light Panel Do?

  • Four touch keys: a tap on E0 - E3 toggles HALL, DESK, PORCH or NIGHT.
  • Two scenes: hold E0 for one second for ALL OFF, hold E3 for ALL ON.
  • A touch slider: eight pads (E4 - E11) dim a 12 V lamp from 0 to 100 %, between pads too.
  • Smooth light: a 500 Hz PWM into an IRLZ44N, with a fade of one second over the whole range.
  • A child lock: LOCK on the dashboard; the pads are still read but change nothing.
  • A look inside the sensor: each pad's filtered data, baseline and delta on the dashboard and as JSON.

It is a great project for learning I2C, a sensor's registers without a library, interrupts and touch gestures in one go. In the simulation, the MPR121's pop-up shows every pad's data, baseline and delta against the thresholds, so we can see exactly what the chip sees when a finger lands.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.

The parts of the touch light panel and their library posts
PartProteus deviceLibrary postConnection
ESP32 DevKit V1ESP32 DEVKIT V1ESP32 DevKit V1 Library for ProteusU1, ESP32_Touch_Panel.hex, Wi-Fi 192.168.1.101
MPR121 touch sensorTOUCHMPR121TEPMPR121 Touch Sensor Library for ProteusU2: SDA D21, SCL D22, IRQ D4, ADD to GND (0x5A)
5 mm LEDs (4)LED5MMYELLOWTEP ...5mm LED Library for ProteusD1 - D4 on D27, D26, D25, D33
IRLZ44N MOSFETIRLZ44NTEPN-MOSFET Library for ProteusQ1: the gate from D32 through R5, the source to ground
W5W 12 V 5 W lampBULBW5WTEPFilament Bulb Library for ProteusL1 from +12V to the drain
Resistors 220 ohm (5)RESISTOR4BANDTEPResistor Library for ProteusR1 - R4 the LEDs, R5 the gate
I2C LCD 16x2I2CLCD16X2BLUEADVTEPI2C LCD 16x2 Library for ProteusLCD1 at 0x27 on the same bus, VCC 5 V
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Touch Panel: the Circuit

Figure: The complete circuit of the touch light panel on an A3 Proteus sheet.
The connections of the touch light panel
SignalESP32Goes to
SDA / SCLD21 / D22the MPR121 and the LCD (one I2C bus: 0x5A and 0x27)
IRQD4the MPR121's IRQ (open drain, active low; the input's pull-up)
L1 - L4D27, D26, D25, D33R1 - R4 220 ohm into the LEDs HALL, DESK, PORCH, NIGHT
LAMPD32R5 220 ohm into the IRLZ44N's gate (500 Hz PWM)
5VVINthe LCD's VCC
+3.3V-the MPR121's 3.3V pin

The MPR121 and the LCD share one I2C bus at different addresses: 0x5A for the touch controller (its ADD pin on ground) and 0x27 for the LCD's PCF8574 backpack. The MPR121's electrode pins E0 - E11 are the panel's pads: on a real panel, copper areas on a circuit board behind the glass, each with one wire to its pin. The lamp is switched on its low side: the W5W sits between +12V and the MOSFET's drain, and the PWM on the gate sets how much of the time it is connected.

How the Capacitive Touch Panel Works

A Pad, a Finger and a Capacitor

A copper pad is one plate of a small capacitor; everything around it, the ground plane and the room, is the other. A finger is a large conductor connected to your body, so when it comes close it adds a few picofarads. The MPR121 measures each pad by charging it with a set current for a set time and reading the voltage it reaches: V = I x t / C. More capacitance, less voltage, so the reading falls when a finger arrives. In the MPR121 library's model, an untouched pad reads about 700 counts and a finger takes away about 60, growing over 30 ms as the finger lands.

The Baseline and Two Thresholds

A fixed limit would not work: a pad's reading drifts with temperature, humidity and the cover in front of it. So the MPR121 keeps a baseline for each pad, a slow average of its untouched reading, and compares the filtered data against it. The difference is the delta. When the delta passes the touch threshold (12 here), the pad is touched; it is released only when the delta falls below the lower release threshold (6). The gap between the two is hysteresis: a finger resting near the limit does not make the pad flicker. The chip samples every pad every 16 ms, and at every change of the touch status it pulls IRQ low until the status is read.

A Slider from Separate Pads

Eight pads in a row could give eight positions. But a finger is wider than the gap between two pads, so near a border it touches both, each a little less. The ESP32 takes the centroid: every touched pad's position (0 for E4 up to 7 for E11) weighted by its delta. A finger right on E6 gives 2.0; one across E8 and E9 gives about 4.5. Divided by 7, that is the lamp's level: E4 = 0 %, E11 = 100 %, and the halves between. Real sliders use zig-zag pads so that a finger always covers two of them and the centroid moves smoothly.

A Tap or a Hold?

E0 has two jobs: a tap toggles HALL, a one-second hold switches everything off. At the moment the finger lands, the ESP32 cannot know which one it is. So a key toggles its light when the finger lifts, if that was within one second. If the finger is still there after one second, the hold fires at once, and the lift that follows does nothing. E3 works the same way with ALL ON. That is why a touch key feels slightly different from a mechanical switch: the action comes on release.

Why the MPR121, Not the ESP32's Own Touch Pins

The ESP32 has capacitive touch pins of its own, but they share the GPIOs with everything else and, with touchRead(), leave most of the filtering and the thresholds to your code. The MPR121 does all of that in hardware for twelve pads, keeps a filtered value and a baseline per pad, and only bothers the microcontroller through IRQ when something changes. The same panel works with any board that has I2C, and up to four MPR121s share one bus (0x5A - 0x5D) for 48 pads.

The Arduino Code Explained

The sketch is ESP32_Touch_Panel.ino, built for our ESP32 DevKit V1 with the WiFi, WebServer and Wire libraries and LiquidCrystal_I2C for the display. It talks to the MPR121 through its registers, with no library. The start-up: a soft reset, a check that the chip answered (register 0x5D always comes out of reset as 0x24), the two thresholds for every pad, then run mode for all twelve:

  wr(0x80, 0x63);                                                   // soft reset: stop mode, every register at its reset value
  if (rd(0x5D, &v, 1) != 1 || v != 0x24) return false;              // 0x5D resets to 0x24: the chip answered
  for (byte e = 0; e < 12; e++) { wr(0x41 + 2 * e, TOUCH_TH); wr(0x42 + 2 * e, RELEASE_TH); }
  wr(0x5E, 0x8C);                                                   // ECR: the baselines from the first samples, E0 - E11 running

The loop reads the pads at once when IRQ goes low, every 50 ms while a finger is on (for the hold timer and the slider), and every 250 ms otherwise (for the dashboard's numbers):

  if (digitalRead(IRQ_PIN) == LOW || ms - lastRead >= (status ? 50UL : 250UL)) { lastRead = ms; touchUpdate(); }

Each read also fetches the twelve filtered values (10 bits, two bytes each) and the baselines (their top 8 bits), and works out the deltas:

  if (rd(0x04, b, 24) == 24) for (byte e = 0; e < 12; e++) data[e] = b[2 * e] | ((b[2 * e + 1] & 3) << 8);
  for (byte e = 0; e < 12; e++) delta[e] = (int16_t)base[e] - (int16_t)data[e];

The slider's centroid, weighted by the deltas, and the lamp following it:

  for (byte k = 0; k < 8; k++) if (st & (1 << (4 + k))) { const long w = max(1, (int)delta[4 + k]); sw += w; swx += w * k; }
    if (!locked) target = pos / 7;

A key toggles its light when the finger lifts; the holds on E0 and E3 fire after one second. Note the fresh millis() for the hold: loop() reads the time first, then touchUpdate() stamps the touch with a slightly later time. Our first run compared those two, and the unsigned difference of a later time from an earlier one wrapped round to a huge number: ALL OFF fired the instant E0 was touched.

      else setLight(k, !light[k], "tap");
  const unsigned long now = millis();
  for (byte k = 0; k < 4; k += 3) if (held[k] && !longDone[k] && now - downAt[k] >= HOLD_MS) {

The lamp: a 500 Hz PWM with 10 bits, moved 1 % every 10 ms towards the target. 500 Hz, not the usual 5 kHz: in the simulation a PWM of 1 kHz or more reaches a switch as its average voltage, and the MOSFET would sit half on and get hot.

  ledcAttach(LAMP, 500, 10);
    if (level != was) ledcWrite(LAMP, (uint32_t)(level * 1023 + 0.5f));

And the LCD writes a line only when it has changed: every character costs several I2C transfers through the PCF8574, and a full refresh would hold up the touch reads for tens of milliseconds:

  if (!strcmp(shown[row], text)) return;

The dashboard's links are Refresh, ALL OFF, ALL ON, HALL, DESK, PORCH, NIGHT, LOCK and LAMP 0 / 50 / 100 %; /data gives the lights, the lamp, the touch status and the twelve pads' data, baselines and deltas as JSON.

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files (the steps are under the download button).
  2. Open ESP32-Touch-Panel.pdsprj from the Proteus Simulation folder. Keep the HEX and the DLL files next to it.
  3. Press Play. When the serial monitor says READY, click U2 for the MPR121's pop-up.
  4. Its chips E0 - E11 put a finger on a pad and take it off again: tap E0 - E3, touch E4 ... E11 one after the other for the slider, or keep E0 on for a second.
  5. Open the Laptop WiFi Monitor (press Go) for the dashboard: the lights, ALL OFF / ALL ON, LOCK and the lamp.

Simulation Results: the Touch Panel in Proteus

We pressed Play and touched the pads with the MPR121 pop-up's chips: two taps, a swipe along the slider, a tap on it, a hold on E3, the lock from the dashboard, a hold on E0, then two clicks on the dashboard. The pop-up shows each pad's filtered data, baseline and delta, so we can watch exactly what the chip saw at every step. The times are the sketch's clock; the whole run took 36.9 simulated seconds.

The Start: Twelve Quiet Pads

At 3.39 s the sketch reported READY. The MPR121's pop-up showed it at address 0x5A on 3.30 V, ECR = 0x8C (all 12 electrodes running), a sample every 16 ms. Every pad read about 700 counts, a few counts apart from each other, with its baseline right on it: the deltas stayed within 3 counts, far under the touch threshold of 12. All four lights and the lamp were off, and the LCD and the dashboard said so.

Figure: The start: everything off.
Figure: The MPR121: twelve quiet pads.
Figure: The dashboard.

Two Taps: HALL and PORCH

A finger on E0 at 7.07 s, lifted at 7.39 s: HALL ON (tap), at the moment of the lift. Then a finger on E2, and we froze the simulation while it was there. The pad glows green on the sheet; the pop-up shows E2 TOUCHED with data 653 against a baseline of 712: a delta of 59 counts, far over the threshold, and the touch status 0x004 (bit 2). IRQ had gone low and was already high again: the ESP32 had read the status. When the finger lifted, 1.8 s later, PORCH came on.

Figure: A finger on E2.
Figure: E2: a delta of 59 counts.
Figure: HALL and PORCH on.

A Swipe Along the Slider

Next, a swipe: a finger on E4, then on E5 while it left E4, and so on along the row. We froze it with the finger across E8 and E9: both glow on the sheet, both read a delta of 61, and the touch status is 0x300. The centroid is 4.5 (half-way between pad 4 and pad 5 of the slider), so the lamp's target was 4.5 / 7 = 64 %, and the lamp was fading up to it: 500 Hz, about 64 % on, the filament at 2452 K giving 22 lumen. The pop-up's touch history draws the swipe as a staircase across the rows. At the end of the row the ESP32 reported SWIPE right: 0 % to 100 % in 4166 ms, and a second later the lamp was at full power: 4.99 W, 2699 K, 50 lumen.

Figure: The swipe: the finger across E8 and E9.
Figure: Two pads at once: the position between them.
Figure: The lamp following the finger.
Figure: The end of the swipe: 100 %.
Figure: Full brightness.
Figure: The LCD.

A Tap on the Slider

A tap on E6 (position 2) set the lamp to 2 / 7 = 29 %, and it faded down. The MOSFET's pop-up counted the switching: 501 times a second, about 29 % on. Because the MOSFET is either fully on or fully off, it barely warms: its average loss was about 2 mW. The filament settled at 2056 K and gave only 5 lumen: a tenth of the full light for 29 % of the time on. Along the run, 50 % gave 13 lumen and 64 % gave 22: a filament's light rises much faster than its power, which is why real dimmers bend the slider's scale.

Figure: A slider tap: 29 %.
Figure: The lamp at 29 %.
Figure: The MOSFET: switching, not heating.

Hold for a Scene: ALL ON

A finger on E3 at 20.93 s, and kept there. At exactly 21.93 s, one second later, the ESP32 switched ALL ON (hold E3): all four LEDs and the lamp heading for 100 %. We froze it with the finger still on: the pop-up shows E3 touched (delta 58), the touch status 0x008. When the finger lifted at 23.31 s, nothing toggled: the hold had already done its job.

Figure: ALL ON.
Figure: The finger still on E3.

The Child Lock

Then LOCK on the laptop. The LCD's second line changed to LAMP 100% LOCKED. A tap on E1 was still seen by the MPR121, as every touch is, but the ESP32 answered DESK tap ignored: LOCKED and DESK stayed as it was. UNLOCK on the dashboard brought the pads back.

Figure: LOCK from the laptop.
Figure: Locked: a tap ignored.
Figure: The LCD while locked.

Hold for a Scene: ALL OFF

A finger on E0 at 28.30 s: at 29.30 s, one second later, ALL OFF (hold E0). The LEDs went out at once and the lamp faded down: we caught it at 39 %, 2180 K and 8 lumen on its way to dark.

Figure: ALL OFF.
Figure: The lamp fading out.

From the Dashboard

Last, two clicks on the laptop: DESK (DESK ON (web)) and LAMP 50 %. The page answers at once with the target, 50 %, while the lamp fades to it: 2321 K and 13 lumen. The phone shows the same dashboard as buttons, and the serial monitor holds every touch and release of the run with its delta.

Figure: LAMP 50 % clicked.
Figure: DESK and 50 % from the web.
Figure: The lamp at 50 %.
Figure: The phone: the same dashboard.
Figure: The serial monitor: the whole run.

Every gesture did what it should: the taps toggled on the lift, both holds fired exactly one second after the touch, the swipe and the slider tap gave the right levels from the centroid, and the lock kept the pads from changing anything.

Download the Project Files

Click the button below to download ESP32-Capacitive-Touch-Light-Panel-MPR121-Proteus-Simulation-v1.0.zip (about 4.8 MB, without the source code of the models):

ESP32 Capacitive Touch Light Panel with MPR121 - Proteus Simulation V1.0
  • Proteus Simulation: ESP32-Touch-Panel.pdsprj, ESP32_Touch_Panel.hex and copies of the models.
  • Arduino Code: ESP32_Touch_Panel.ino, the sketch.
  • Proteus Library Files: the LIB files of every part.
  • Proteus Model Files: the DLL models.
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy the files from Proteus Model Files into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

Open the sketch in the Arduino IDE with the TEP Boards for Proteus installed, select ESP32 Dev Module (TEP Proteus), install LiquidCrystal_I2C, and use Sketch > Export Compiled Binary. Put the new HEX next to the project. The thresholds, the hold time and the fade time are near the top. To try the panel with no clicks, set the MPR121's TOUCH property to DEMO: it plays the library's own sequence of touches.

Ideas to Extend the Project

  • Add a buzzer for a short click at every touch, the feedback a glass panel lacks.
  • Use the MPR121's proximity electrode to light the panel when a hand comes near.
  • Drive four relays instead of the LEDs, and a MOSFET per channel for more dimmers.
  • Store the lights and the lamp level in Preferences so the panel remembers them after a power cut.
  • Add double-tap and swipe gestures: a swipe left over the whole slider as a quick OFF.
  • Connect two MPR121s (0x5A and 0x5B) for a 24-pad panel.

From Simulation to Real Hardware

  • The front: glass or acrylic up to a few millimetres thick; thicker covers need larger pads or lower thresholds.
  • Short, separate wires: every millimetre of electrode wire is part of the pad; keep the wires short and apart, and the ground plane away from the pads.
  • Tune the thresholds: read the deltas on the dashboard with a real finger and set the thresholds to about half of the touch delta.
  • Mains lights: switch them with relays or SSRs rated for the load, in a proper enclosure, never straight from the ESP32.
  • The lamp: a filament lamp's light rises much faster than its power; for a natural feel add a gamma curve to the slider.

Troubleshooting

  • MPR121 NOT FOUND: SDA and SCL swapped, the 3.3V pin not powered, or ADD not on ground (the chip is at 0x5B then).
  • No touches: the MPR121's pop-up says STOP MODE: ECR was never written; or the thresholds are zero.
  • A light toggles only on release: that is the tap; it has to be told apart from a hold.
  • The lamp only goes on and off: a PWM of 1 kHz or more reaches the MOSFET as its average voltage in the simulation; keep 500 Hz.
  • No dashboard: the ESP32 joins TEP-WiFi about three seconds after the start; press Go again.

Limitations of the Simulation

  • The MPR121, the lamp and the MOSFET are our libraries' models, built from their datasheets, not every detail of real parts.
  • A finger in the simulation is the same on every pad (about 60 counts); a real finger's delta depends on the pad, the cover and how it is pressed.
  • The MPR121 model does not include the charge current and time settings or the auto-configuration registers.
  • ESP32 is a product of Espressif Systems; this project is not made or endorsed by Espressif or by Labcenter.

So, that was all about the ESP32 Capacitive Touch Light Panel with MPR121 - Proteus Simulation. I hope it helps you understand how a pad notices a finger, why a touch controller needs a baseline and two thresholds, how a slider finds a position between pads, and how to tell a tap from a hold, all tested in a simulation before you cut any copper. More projects with the ESP32 and the Raspberry Pi 5 are coming. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!