Skip to main content
JRubė.1
Associate
May 10, 2022 9:18 AM UTC
Question

Measuring capacitance STM32L0-Disco

I'm trying to measure capacitance with my STM32L053-Disco

I'm using ADC and one output pin to charge the capacitor. I tried measuring time it takes for capacitor to charge to 62.3%.

The problem that I'm facing is that the values that I'm getting from ADC never show that it gets any charge.

I use 1 nF capacitor and 1MOhm resistor

 This is my circuit:

 0693W00000NpgdJQAR.png 

```lang-c

while (1)

{

  // Get ADC value

  HAL_ADC_Start(&hadc);

  HAL_ADC_PollForConversion(&hadc, 1000);

  raw = HAL_ADC_GetValue(&hadc);

  real = (raw*3.3)/4096;

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

  //status == HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);

  if (real >= 3.3*0.632)

  {

    HAL_ADC_Stop(&hadc);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);

    time2 = HAL_GetTick();

  }

  if (real <= 0.12)

  {

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

    time1 = HAL_GetTick();

  }

  time3 = (time2 - time1)/1000;

  c = time3/res;

  // Convert to string and print

  sprintf(TxBuffer,"%7.3f V\n", real);

  HAL_UART_Transmit(&huart1, (uint8_t*)TxBuffer, strlen(TxBuffer), HAL_MAX_DELAY);

  // Pretend we have to do something else for a while

  HAL_Delay(100);

}

```

I never get to measuring capacitance because these are the ADC values that I get:

0693W00000NpgdJQAR.png 

Can anybody help me?

This topic has been closed for replies.

13 replies

Andrew Neil
Super User
May 10, 2022 10:32 AM UTC

"the values that I'm getting from ADC never show that it gets any charge"

So the first question must be: how are you sure that it is actually getting any charge?

Note that solderless breadboards have notoriously high stray capacitance, although that shouldn't be a problem relative to a 1nF test capacitor.

How does the ADC input impedance compare to 1M Ohm ... ?

Does the STM32L053 have a comparator?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
JRubė.1
JRubė.1Author
Associate
May 10, 2022 10:43 AM UTC

So the first question must be: how are you sure that it is actually getting any charge?

That's the thing, I'm not sure about that at all. It doesn't matter if capacitor is plugged in or not, the readings are always the same

Andrew Neil
Super User
May 10, 2022 11:11 AM UTC

Then you need to do some basic tests on that!

Do you have an oscilloscope?

You could try using a larger capacitor, with a charge time you can watch on a meter - but, again, you're going to have to be careful with the meter loading relative to your charging resistor ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
waclawek.jan
Super User
May 10, 2022 10:45 AM UTC

This is probably not a viable method. ADC is loading its input by the sampling capacitor, every time you start a conversion.

Try to have a look at the comparators, as Andrew said above.

JW

JRubė.1
JRubė.1Author
Associate
May 10, 2022 10:50 AM UTC

Well actually I know that someone succeeded doing a capacitor meter using ADC and one GPIO_Output pin, that's why I tried using this method because it seemed like the easiest approach.

Andrew Neil
Super User
May 10, 2022 11:09 AM UTC

then you need to look more closely at exactly what they did...

EDIT

https://www.google.com/search?q=microcontroller+capacitance+measurement

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
RetroInTheShade
Associate
May 10, 2022 12:55 PM UTC

On the basis that your ADC is working you should be able to get an approximate value.

Looking at your code, your loop is running slow. The rise time of your 1MOhm / 1nF will be a couple of milliseconds. You have a 100millisecond delay plus uart tx in the loop. If you tighten your loop and dump samples into buffer, do you see voltage across capacitor rising?

JRubė.1
JRubė.1Author
Associate
May 10, 2022 2:38 PM UTC

I shortened my delay - still nothing.

I know that this is basic of electronics but I'm gonna ask anyway, I've looked up a lot of examples of people doing this capaciter meter and they are always measuring 3.3*0.623, the thing is when I plug in a big resistor I could never reach that value, it just stays at 0.144 and nothing ever changes..

JRubė.1
JRubė.1Author
Associate
May 10, 2022 2:42 PM UTC

Also my ADC for some reason doesn't start from 0, it starts at about 0.05V, any ideas why that could be happening?

LCE
Principal III
May 10, 2022 3:22 PM UTC

Offset.

You cannot expect to get exactly 0.000 V or 3.300 V from a GPIO.

Additionally, you might have some leakage current from the ADC input creating that voltage together with the 1M resistor.

LCE
Principal III
May 10, 2022 3:26 PM UTC

Start with checking your ADC function:

replace the capacitor with another 1M resistor, then depending on the GPIO setting, you should measure about 0V or 1/2 of VCC.

If that is not the case, your ADC control does not yet what it should.

OR the 1M might be too high for the ADC's input impedance. That's the case when GPIO is high and ADC voltage is sign. below 1/2 of VCC.

Better check with a multimeter, even your wiring could be wrong. Sure you're on the right IOs? :)