Touch Sensor

The touch sensor is also an I2C device so we can use the I2CDevice class to talk to the sensor chip. I this case, it is the CAP1188 chip. I could not find classes in the IOT.Device.Bindings library to drive this chip, so I had to resort to the DataSheet for the chip, and the Python code found on Adafruit's website here. While reading the data sheet for the chip, and inspecting the adafruit board pinouts, I saw an IRQ pin. This is very useful as it goes low when a touch is detected. This means I can use a GPIO pin to trigger an event in my code when the touch sensor is touched. This means I will not need to poll the sensor using up CPU. As the ReSpeaker 4Mic board has a digital IO port on it, I can connect the touch sensor IRQ pin to the digital IO port which in turn is connected to GPIO 12 on the Raspberry PI. Now for the code. I re-coded the python class from AdaFruit into C# and have used that. Again see my Git Repo for the full code.


Here is how I use the Touch Sensor class

// Init Touch Sensor - Use GPIO12 to detect IRQ from Touch Sensor to avoid polling.
Console.WriteLine("Init Touch Sensor and IRQ on GPIO12");
I2cConnectionSettings touchSettings = new I2cConnectionSettings(1, 0x29);
I2cDevice touchI2CDevice = I2cDevice.Create(touchSettings);
TouchDriver touchDriver = new TouchDriver(touchI2CDevice, gpio,12, true);           
touchDriver.OnTouched += TouchDriver_OnTouched;

As you can see, the specify the I2C bus connection settings, and create an I2C device. I then pass the device into the Touch Driver class which performs the initialization. I need to pass in the gpio object and the GPIO pin number (12) into the device to allow it configure the IRQ handling. Then all I need to do is to handle the OnTouched event which gets raised each time the touch sensor is pressed - Either a single or multiple touches at once.

(C) P J Tewkesbury 2024 - 2023.11.23.1 - All Rights Reserved - Forgot Password
Powered by .NET 7.0.14 and running on serverpi with Unix 6.6.58.8 - Deployed from commit cb263ce4