2022-05-10 02:18 AM
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:
```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:
Can anybody help me?
2022-05-10 03:32 AM
"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?
2022-05-10 03:43 AM
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
2022-05-10 03:45 AM
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
2022-05-10 03:50 AM
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.
2022-05-10 04:09 AM
then you need to look more closely at exactly what they did...
EDIT
https://www.google.com/search?q=microcontroller+capacitance+measurement
2022-05-10 04:11 AM
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 ...
2022-05-10 04:32 AM
Believe me when I tell you that I looked through 20 pages 5 times :D
2022-05-10 04:32 AM
Sadly I don't have a oscilloscope..
2022-05-10 05:55 AM
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?