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

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
Associate II

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

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

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? :)