cancel
Showing results for 
Search instead for 
Did you mean: 

Measuring capacitance STM32L0-Disco

JRubė.1
Associate II

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?

13 REPLIES 13

"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?

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

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

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.

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

EDIT

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

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 ...

Believe me when I tell you that I looked through 20 pages 5 times :D

Sadly I don't have a oscilloscope..

RetroInTheShade
Associate III

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?