One of the cool features of ESP32 is the touchpins, ie. the capacitive touch inputs system without the need of external circuits and even the complex discharge/ timing logic is neatly encapsulated in one API touchRead().
This is more of On/Off example. What I wanted was a analog control, something like potentiometer. This is achievable if the motion of the finger across the surface is somehow translated into change in capacitance.
Principle of operation
The capacitive slider surface can be of any shape and size you want. In this project I am simply using a strip of aluminium foil.
The main principle this works on is -
Capacitance changes with increase/decrease in thickness of dielectric between the two electrodes.
To leverage this I am creating a gradient of insulator on top of the aluminium foil to get this change in the capacitance from one end of the strip to another.
The choice of dielectric is clear plastic tape (sellotape), that is applied multiple layers. The tape is cut shorter by a few mm each time for the new layer applied on the aluminium foil. This will create a gradual increase in the thickness of the insulation over the strip. The change is still in terms of discreet steps, resolution of which should be decided based on the application and use case.
I could not take any good pictures to show the layering on my strip, so here's a 3D render of what it will look like -
The logic is to read the touch inputs repeatedly and check if the value has increased or decrease as compared to the previous reading. Each time such increase or decrease happens ideally your finger has moved from a point on the strip of one thickness to another point of different thickness.
By having a count of such increase/decrease, we can get an analog value reading as well as direction of swipe. And this can be achieved from one GPIO pin of ESP32.
Note: The code provided in this project is more of an proof of concept than a finished library. A lot has to be refined and polished before turning this into a general purpose library that can be used along with others in a typical project.
Slider Controller in my project
The resolution of the aluminium strip slider I made is very low (4 to be precise). And as a result of that the analog reading is not very accurate.
My initial idea was to change the color of the moon in a smooth manor according to the sliding on the strip. But the values read are not repeatable for the multiple slides of same manor. Hence I have repurposed it as a direction of swipe indicator.
I am increasing a Hue of the LED by a fixed value each time there is a swipe in one direction and decreasing by the same value for swipe in opposite direction.