HW7 - ndm736/ME433_2020 GitHub Wiki
The 100 and 200 series of PIC32MX contain a special peripheral for measuring capacitance called the Charge Time Measurement Unit, or CTMU. You can read about the capabilities of the CTMU in the Family Reference Manual Chapter for the CTMU. The CTMU is a neat set of features that includes a constant current generator, useful for charging capacitors. This can be used for measuring time at very high resolution, and measuring from sensors, like the type of capacitive touch screens on our phones and computers. Capacitive touch is nice because there are no moving parts to wear out or jam, and the surface can be made waterproof. The buttons can also be interwoven to make one dimensional sliders or two dimensional position sensors. There is a little bit of magic to the design of the physical electrode conductor, and the thing that touches it must also be conductive, but the CTMU can change the amount of output current over two orders of magnitude, so with a little calibration we can tune to the appropriate sensitivity for our device.
The method of detecting if someone is touching a conductor is to detect a change in relative capacitance. If the conductor is hanging out in space, it has a natural capacitance to ground, related to the size of the conductor, the distance from ground, and the properties of the material separating them. When another conductive body approaches, like your finger, another capacitor is created, and changes the total capacitance of the system.
The CTMU detects relative changes in capacitance by applying a constant current to the conductor and waiting a fixed amount of time. The voltage generated is the integrated current divided by the value of the capacitance, so we can read the voltage with the ADC, and use the value it returns with some calibration to know if the conductor is being touched.
This means we will need to use the analog to digital converter (ADC). Sample code to initialize and read any of the AN pins is here. Remember to set the ANSEL bit for the pin you are reading to 1 to enable the analog capability of the pin. The ADC works by sampling the voltage for a short time, then converting it to a 10 bit number (0-1023). There are many modes in the ADC setup to make this happen automatically, but this sample code uses the most manual method, and the core timer for timing.
The CTMU works by pushing current out of the selected AN pin. I've added an extra function to the ADC library to read from the ADC after charging the pin with the CTMU, called ctmu_read(int pin, int delay), which returns the ADC value. The larger that capacitance, the smaller the number will be. For more resolution, increase the delay. The pin is the number of the AN (example, B3 is AN5, so the pin would be 5).
Use an alligator clip to connect a short wire to the AN pin. Touch the other end of the alligator pin. Write some code that reads the CTMU for that pin and sums up the result of about 10 reads (this will help reduce the noise, increase the number of samples for a more stable read). Note the baseline value of the read, and how much it goes down when you touch the alligator. Try clipping the alligator to something conductive, like a big piece of metal or a banana. Is the change big enough and reliable enough to know that the item was touched? Can you increase the delay or the number of reads to make it more reliable?
A cool application for capacitive touch (other than a banana piano) is a one dimensional slider. To build a slider, create two long triangles out of aluminum foil, with a very thin strip in between the hypotenuses (see page 13 of this application note (short aside: most manufacturers publish Application Notes that describe how they intend their products to be used, including circuit schematics and sample code.)) As you slide your finger along the triangles, one value will increase as the other decreases. '''Use each triangle individually to turn on a WS2812B if it is being touched, and use the values together to light up a WS2812B proportionally as you slide your finger''', with the equations described in the app note. Use cardboard, aluminum foil, and clear tape to build the sensor. When you are not touching the sensor, none of the lights should be on.
Upload your code to a folder called HW7 in your git repo, and make a demonstration video for Canvas.