cancel
Showing results for 
Search instead for 
Did you mean: 

Measuring capacitance STM32F103 using HAL_Library

RSabl
Associate II

Hi, i'm trying to measure capacitance using a single GPIO pin ( found an article online a guy used this method to calculate the diode reverse current so i'm guessing its possible to measure capacity aswell). Firstly i use the pin as output and charge the capacitor, now i'd like to set the pin to input and measure the time it passes for the capacitor to discharge via the pin. (untill the input becomes 0). The issue is that i cannot change GPIO pin direction dynamicly using HAL , i know that using the standard lib i need to adjust the value of MODER register, but i'd like to do it using HAL if its possible? What should be the procedure. I'm a really newbie regarding ARM architecture or electronics in general, so this question may sound a bit dumb :).

4 REPLIES 4
S.Ma
Principal

Simple tricks have hidden challenges. Is this for a single setup or larger produced devices?

The pin itself has its own capacitance in pF (datasheet) with tolerances.

So actually, you'll be measuring 2 caps in parallel.

If you use another N/C gpio, you could differentiate and maybe discriminate the external cal.

The time you will measure will need fine resolution, so speed is of the essence.

Using HAL may not give the precise predictive timings needed.

Now detection of the low level when discharge will need the low level to be precise.

The datasheet may not provide precise value unless you measure it for a particular part at particular voltage and temperature (called PVT parameters).

So yes, you will have to manually play with GPIO HW registers, and trigger an EXTI interrupt to capture the timestamps start/stop.

Some STM32 have touch by capacitive switching method. Maybe it's a way to run the measurement, although it may require extra pins.

Using ADC and halving the charges between ADC sample and hold cap with external's maybe another way...

RSabl
Associate II

Well, first of all thank you for your answer. I'm doing this for a school project, i have a water tank i need to regulate using a custom made capacitive water level sensor (when the water is at its lowest it has around 40pF capacity), somehow i need to get the measurements on the MCU, my first idea was measuring the RC time constant with ADC, and a PWM generated pulse , but that required external circutry specificly OP amp because for some reason if i added a 1MOhm resistor in series with the capacitive sensor maximum voltage on the capacitor from a 3.3V source was around 1.3V, and the ADC pin got the remaining. I'm only a student so i dont really know alot about theese things, so i figured it was because of the ADC pin impedance . So i've tried to find an easier solution, all i've come up with was either using a IC 555 timer to generate frequency inverse to the capacity of the sensor and measure clock pulses with stm or using the method mentioned in the question. What do you think could be the right approach to this problem? I wanted to make whole project using HAL libraries since i have alot of communication with another STM which uses I2C for LCD, SPI to Raspberry Pi ect. and it seemed alot easier to configure things using CubeMX and HAL .

S.Ma
Principal

Maybe re-use the 555 and use an STM32 Timer in input capture mode to measure the period and duty cycle? This seems the simplest way forward.

RSabl
Associate II

Yeah im planing on doing exactly that, thank you alot for your support.