In this project, a capacitive sensor is created using conductive ink on a paper surface. The Analog-to-Digital Converter with Computation (ADCC) peripheral is used to detect the voltage changes on the pin connected to the sensor indicating a change in pin capacitance when a second conductive surface (such as a finger) is present.
The Burst Averaging Mode computation feature is used to perform 32 consecutive conversions of the voltage on the sensor pin every time the ADCC is triggered; with each new conversion adding to all of the previous readings. Once 32 conversions have completed, the accumulated value is divided by 32 and this new averaged value stored as a Filtered Value.
Upon completion of each Burst Average, the Filtered Value is transmitted from the PIC16F18855 EUSART peripheral to the PIC18LF25K50 on the MPLAB Xpress Evaluation Board which converts the EUSART transmission to USB and transmits over the USB connection to a computer running a Terminal program for display.
MPLAB® Xpress IDE Edition - Episode 9 - Basic Capacitive Sensing Using the ADCC
Hardware Tools (Optional)
|Tool|| About|| Purchase|
| Windows|| Linux|| Mac OSX|
Cloud Integrated Development Environment
| Windows|| Linux|| Mac OSX|
Project and Source Files
To follow along with these steps, MPLAB® Xpress should be open and the user should have logged in so that the MPLAB Code Configurator plug-in can be used.
Create a new project in MPLAB® Xpress for a PIC16F1855 using the MPLAB Xpress Evaluation Board called burstAvgCapSense.
Instructions are below if this is your first project.
Open the MPLAB® Code Configurator (MCC).
Instructions are below if this is your first project.
Inside of the MCC, the default parameters for the System resource can be used.
Expand the ADCC heading in the Device Resources and double-click on the ADCC peripheral to add to the Project Resources.
Configure the ADCC peripheral as follows:
- Select Burst_averaging_mode from the Operating drop-down menu.
- Leave the Clock Source and Clock in default settings.
- Set the Acquisition Time to 1 (10 x TAD = 20 µs) . This is the time that the ADCC will wait after being triggered before performing the Analog-to-Digital conversion to allow any capacitance connected to the input of the peripheral to fully charge to the average voltage. The sensor is a capacitor and will therefore contribute to this capacitance. Additionally, the internal sample and hold capacitor that is part of the peripheral input needs to be taken into account. A precise charging time could be calculated using Equation 23-1 in the datasheet, but to keep this tutorial simple and because this isn't a time-sensitive application, a more than adequate value of 10 is used.
NOTE: This delay is normally handled using software algorithms on traditional Analog-to-Digital Converters requiring that the user first write and then debug code. The Acquisition Time feature of the ADCC automates this task in hardware.
- Select Right in the Result Alignment drop-down menu to obtain a value between 0 and 1023.
The next step involves using the powerful Computational capabilities of the ADCC. These unique features replace algorithms that were traditionally done in software requiring the user to write and debug code.
Expand the ADCC Computational Feature section of the Configuration window to reveal additional parameters. Configure as follows:
- Set the Repeat value to 32 so that the ADC input signal will be sampled 32 times with each ADCC trigger.
- Configure the Acc Right Shift to 5. This will right shift the accumulated result of the 32 samples by 5 bit positions which is the equivalence of dividing the accumulated result by 25 = 32.
The next steps simply configure the ADCC to disconnect the input signal from the ADCC input to prepare the system to read the capacitive sensor. During this time, the port logic is driven to the default VSS to ensure a known state exists on the input before reconnecting the sensor and reading the voltage.
Expand CVD Features section and configure as follows:
- Set the Precharge Time to 10. This means that the internal capacitance of the ADCC peripheral will be allowed 10 x 2.0 µs (ADCC clock settings) = 20 µs to fully charge or discharge depending on the Precharge Polarity while the path to the capacitive sensor is charged or discharged to the opposite polarity.
NOTE: Again, this Precharge Time is normally implemented using software algorithms on devices with traditional Analog-to-Digital Converters requiring that the user first write and then debug code. The CVD Features of the ADCC automates these tasks in hardware.
NOTE: The value of 10 was arbitrarily selected to provide more than adequate time for the Precharge phase. The user may wish to explore Section 23.4 “Capacitive Voltage Divider (CVD) Features” of the PIC16F18855 datasheet for more information on this feature.
- All other settings can remain at default.
The converted values of the ADCC input signal will be displayed on the host computer in a Terminal program. Therefore, the EUSART peripheral needs to be configured.In the Device Resources, area scroll down to locate the EUSART peripheral and expand. Double-click on the EUSART to add the peripheral to the Project Resources.
Select the EUSART peripheral under the Peripheral heading in the Project Resources pane to open the peripheral configuration.
Configure the EUSART peripheral as follows:
- Enable Transmit
- Set the Baud Rate to 9600
- Redirect STDIO to USART to enable the use of printf statements in the application.
- This will cause stdio.h to be included in EUSART header file to allow the use of common I/O functions such as printf. More information on printf and other stdio functions can be found online.
In the Pin Manger section, connect the following peripheral signals to the indicated Microcontroller pins by clicking on the blue unlock button. This will cause it to turn to a green locked symbol for the associated signal row and pin columns:
- Connect Analog Input Channel ANx (ANC7) signal to the RC7 pin
- Connect the EUSART TX signal to pin RC0 and the RX signal to pin RC1
Select the Pin Module under System in the Project Resources pane. Click in field under the Custom Name column of the RC7 row and change the name to SENSOR.The Pin Module should resemble the figure below:
Click the Generate button in MCC to create the appropriate header and source files for this configuration. A main.c file will also be generated for the project.
Upon successful code generation the “Generation Completed” notification should appear. Select “OK” to close the window.
Click on the main.c source file in the Project pane to open the file and scroll through the code to locate the while(1) loop inside of the main(). Just before the while(1) line declare and initialize to zero a 16-bit variable called adccVal that will hold the converted ADCC input signal.
uint16_t adccVal = 0; //16-bit variable to hold adcc conversion result
Next, a line of code will be needed to start an ADCC conversion of the voltage present on the pin connected to the capacitive sensor created at the beginning of this project. The MPLAB Code Configurator generates a number of functions for your use to configure and manage the peripheral. To locate functions created for the ADCC, navigate to the adcc.h file in the Project window under the burstAvgCapSense > Header Files > MCC Generated Files folder and double-click to open.
Scroll through the adcc.h file to locate the ADCC_StartConversion() function declaration and description. Highlight the function name and arguments and copy using CTRL + C (Windows).
Return to main.c and paste the ADCC_StartConversion() immediately following the adccVal variable declaration using CTRL + V (Windows). Inside the curved brackets, replace the arguments with SENSOR since this is the custom name given to that ADCC channel earlier in the project.
Earlier in the project the ADCC peripheral was configured in Burst Average Mode to complete 32 ADCC conversions of the voltage on the SENSOR input channel. Therefore, code will be needed to check when 32 conversions have completed using an if statement. The number of counts can be checked using the ADCC function (again, found in the adcc.h file) ADCC_GetCurrentCountofConversions().
Copy the function, return to main.c, scroll to the while(1) loop inside main() and paste the function inside an if statement that checks if the conversion count is 32. Remove the void from inside the curved brackets of the function.
If the conversion count is 32, then the current ADCC conversion value will be printed to the serial port for transmission over the USB to the host computer. Since the conversion has been averaged, it will be stored in the variable Filtered Value. Filtered Value can be obtained using the adcc.h function ADCC_GetFilterValue().
Copy the ADCC_GetFilterValue() and add to the printf statement inside of the if statement. Remove void from inside of the curved brackets. The printf shown includes:
- newline command \n
- return carriage command \r
- displays value obtained by ADCC_GetFilterValue() as an unsigned value %u. Otherwise, the output will show signed (positive and negative) values.
This application will perform only one conversion and print only once to the serial port. To continuously generate a new conversion once a previous Burst Average conversion has completed, simply add another ADCC_StartConversion(SENSOR); call immediately after the printf statement inside of the if statement.
The MPLAB Xpress Evaluation Board should be connected to an available USB port on the host computer through a USB cable to the micro B connector on the board. Drivers should install successfully the first time the board is connected and may take a minute or so. The connection is shown in the picture. No other components are required.
Program the MPLAB Xpress board by dragging the project .hex file from the downloads section of your browser and dropping the file onto the XPRESS drive.
The Programmer LED on the Xpress board should quickly flash from green to red and then back to green indicating that the .hex file was successfully programmed to the PIC16F18855.
At this point the Microcontroller and peripherals are configured to read a capacitive sensor and then transmit the converted value over the USB to the computer. Now the actual sensor must be created and then connected to the MPLAB Xpress Development Board.
The following materials were used to create the sensor in this application.
To create the sensor:
Secure a piece of paper to a non-conductive surface like a wooden desk or counter top using adhesive tape.
Draw a capacitive sensor similar to image below using capacitive paint. Capacitive paint or ink vendors can be easily found online. Recipes are also available should the user wish to make their own.
Attach one end of the wire to the capacitive pad once the paint has dried and the other end to pin RC7 on the MPLAB Xpress Evaluation Board. Adhesive tape shown here secures the wire to table and then a large daub of conductive paint covers the end of the wire to connect to the capacitive pad. Obviously, for a more robust application, the wire should be secured to the application surface using a fastener or even hot glue in addition to the daub of conductive paint.
Open a terminal program on the host computer select Connect. In this example the free program called CoolTerm is used.
Once communication is established, the terminal window should display the text enclosed in the printf statement followed by the current Filtered Value.
The sensor alone should output a much higher value than that returned when a finger is introduced.
Note how the values change depending on how much of the finger is on the capacitive sensor or if different items come in contact with the sensor. Remember capacitance is related to surface area (i.e. press harder on the sensor and more of the finger pad makes contact) and material.
Although this is a simplified implementation of capacitive sensing, the reader should keep in mind that values measured on the input of the ADCC peripheral will be very much dependent on a number of parameter including, but not limited to:
- Sensor size
- Material that the sensor is made of
- Material that sensor is drawn onto
- Conductive material in the sensor's proximity
- Environmental conditions
- Size of object that comes in contact with sensor
This type of sensor implementation could be used for any number of sensing applications. However, if you wish to implement an actual touch button we strongly advise you to refer to resources available at Microchip Technology's Touch and Input Sensing Solutions homepage.
Table of Contents