The Problem

I've built some simple capacitors to use as pressure pads on the stairs at home - the aim is to light the stairs up as you go up them - here it is in action. But I'm getting weird readings form them, but only when connected one way round.

The Hardware

I've built them by sticking a piece of kitchen foil to each side of a piece of card, then sticking a stripped wire to either side. They then attach to a small circuit board via a molex KK connector ("SENSOR"), schematic below. circuit schematic

DATA_IN and DATA_OUT ports connect to other similar circuit boards. RA0 outputs data in serial format at 57600 baud. RA2 connects to RA1 of the "downstream" board and allows for the devices to self-enumerate and manage usage of the serial bus.

Power is provided by a battery and data is read through a rasberry pi via an optocoupler - so no electrical connection outside of this system.

The Firmware

The firmware on the PIC works by first setting RA4 low for a millisecond to drain the capacitor. RA4 then becomes an input and is attached to a comparator which compares against an internal voltage reference (4/5 Vdd).RA5 now goes high, and we start a timer and measure how long it takes for the voltage to reach the reference. When weight is applied to the sensor, the card compresses and the capacitance increases - and takes longer to charge up.


Most of them work normally, and take about 800 uS to charge up to 4/5 Vdd. But some give really bizarre readings, fluctuating between about 30uS up to 4000uS to charge - and it changes on every single reading. Even weirder, if I take the connector attached to the "SENSOR" header on the board and reverse the polarity of the sensor - it starts working normally.

So in a sense, I have a solution to the problem - reverse the polarity of the sensors - but this is unsatisfactory as I have no idea why these apparently symmetrical sensors should behave so differently depending on which way round they are attached. Why is this happening?

  • 1
    \$\begingroup\$ It could be that "reversing the polarity" is in fact simply fixing an intermittent connection. Might we worth checking your crimps to make sure they are well attached. \$\endgroup\$ Dec 21, 2017 at 18:08
  • \$\begingroup\$ What connections do you have to ground? \$\endgroup\$
    – Finbarr
    Dec 21, 2017 at 18:08
  • \$\begingroup\$ Nothing is connected to ground as in connected to a spike in the earth - Pins 1 in DATA_IN and DATA_OUT are connected to the negative terminals of the battery - and these are connected to a ground plane on the reverse of the circuit board \$\endgroup\$ Dec 21, 2017 at 18:17
  • \$\begingroup\$ Just a guess but do they work fine if the side you stand on is connected to RA4/RA5 but not when connected to negative? \$\endgroup\$
    – Finbarr
    Dec 21, 2017 at 18:40
  • 1
    \$\begingroup\$ My guess is you have a big antenna for line frequency E fields and no shielding to 0V or earth gnd. Hence poor design. Try injecting a clock and measure coupling of signal with load and pressure controlled coupling with band pass filter to reject humm \$\endgroup\$ Dec 21, 2017 at 18:47

2 Answers 2


One plate of your capacitor is picking up interference, probably from the power wiring in your house. The reason the readings are so erratic is that it depends on what part of the power cycle the reading is taken in. Only some sensors exhibit this problem because either the capacitor or the wires to it are closer to power wiring than those of the other sensors.

The reason flipping the polarity helps is because the interference is coming from one side of the capacitor. Tying the plate on that side to ground greatly reduces the coupled noise to the other plate, because the grounded plate is now acting like a shield.

This is a great illustration of why such a naive capacitive sensing algorithms isn't used in real cases. Capacitance changes are small, and impedances large to make the time constants tractable. That is just inviting noise pickup. Without a algorithm that at least tries to cancel noise, there is little hope of reliable operation.

You describe the old time-based cap-sense method, even embodied by peripherals built into some PICs. However, that does not make it a good idea. Note that this kind of cap-sense peripheral isn't included much, if at all, in newer parts.

Here is the cap sense method I mostly use:

Always at least try to cancel common mode noise. Take two readings using opposite polarity or plates or something. The two opposite low level readings are taken together as one higher level reading. Ideally, you take these readings often repetitively. If they are arranged right, common mode noise cancels out after suitably low pass filtering the stream of higher level readings.

Instead of measuring a time constant, measure the change in voltage due to a step. That is much faster, so the time window when vulnerable to noise is shorter. It also allows for consecutive measurements to be taken closer in time, helping to reduce common mode noise if each measurement flips something from the previous. More measurements per time also allow more aggressive low pass filtering, yielding better noise margin.

For my cap sense buttons (non deforming, so a little different problem than yours), I drive the common exitation line, and the line I want to sense, both high. Switch the sense line to a A/D input, drive the exitation line low, wait just a little longer than the A/D acquisition time, and convert. Then do the same thing again except both lines start low and the exitiation line gets a high-going step. By summing how much the first dropped from high and how much the second rose from low, common mode noise is significantly reduced. Doing this often, switching polarity each time, yields good noise immunity overall after low pass filtering.

In this method I measure coupling to ground, which decreases the coupling from the exitation line. A positive signal is therefore getting less of a overall response. In the case of cap sense buttons on a PCB, I surround each button with a ground flood. The finger adds capacitance between the button and the ground flood. The ground flood also acts as a shield, minimizing noise pickup from elsewhere.

I've got a cap sense demo board where random noise after all processing is about ±1 count just sitting there. A solid finger touch yields values of about 200-350. In other words, I get a solid 100:1 signal to noise ratio with this method.


More investigation on my system has shown the answer to this - 50Hz coupling from the mains power lines in the house (as everyone else has said). Every sensor does actually display some variation between samples, and putting all the numbers into a spreadsheet shows a sinusoidal variation, with a period of approximately 1 second. Some sensors have much more variation in one orientation than the other, likely due to their physical orientation towards the source of interference.

Each sample takes about 5ms to process - and then it waits 250ms until doing the next sample, This gives us a period of 255ms, or 12¾ cycles at 50Hz - which will process through the mains signal every 4 samples (or one second).

I've solved this problem for myself by getting the PIC to monitor the 50Hz ripple and sync the sample with the peak of the ripple. This has lead to rock-steady sensor readings.

Olin's solution is actually much better than this, and I'll implement that next if I run into any problems


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.