Author Topic: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset  (Read 3340 times)

0 Members and 1 Guest are viewing this topic.

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Hello everyone,

I am working on a project using the STM32F030C6 https://www.st.com/en/microcontrollers-microprocessors/stm32f030c6.html microcontroller to measure mains RMS voltage. The voltage divider output is connected to the ADC (channel 0 pin), and I am taking 800 samples at a 125µs interval to calculate the RMS value.

The issue I am facing is that after restarting the microcontroller, the ADC readings seem to deviate, which impacts the accuracy and precision of the RMS voltage calculation. Here's what I have tried so far:
1) ADC Calibration: I perform ADC calibration on power-up in my code.
2)Increased ADC Sampling Time: I changed the ADC sampling time from 1.5 cycles to 71.5 cycles, but this did not resolve the issue.

Despite these steps, the problem persists. My question is:
1) Could this issue be related to my schematic?
2) What other potential causes or troubleshooting steps should I consider for stabilizing the ADC values?

Thank you for any insights or suggestions!

Schematic:
2397925-0
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 4371
  • Country: Earth
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #1 on: October 10, 2024, 01:03:43 pm »
I suggest you first debug ADC with a DC measurement, not RMS.
Then when it will works as expected, you can debug RMS code.
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #2 on: October 11, 2024, 10:18:43 am »
I am sure my code is correct. I am guessing the problem is with the schematic. May be very high input impedance to the adc pin is causing this issue.
 

Offline tom66

  • Super Contributor
  • ***
  • Posts: 7625
  • Country: gb
  • Professional HW / FPGA / Embedded Engr. & Hobbyist
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #3 on: October 11, 2024, 12:01:02 pm »
The 100nF cap should help with the sampling current, however it's possible if you sample the pin too often you are drawing too much current on the input.

I second the suggestion to test with DC, but do so if possible with similar 510kohm ladder in series. For instance if you put 30VDC on the input what do you read pre and post reset.  That may give you an idea.

You can measure the DC voltage at the ADC pin and see what changes after reset, if anything.

Also mains voltage is typically measured from L to N, assuming your MCU is grounded if you are debugging it with a PC, you will get a less accurate reading as current flows down N and causes an offset relative to PE.  I am not familiar with India's electrical infrastructure but if it is anything like UK/Europe then PE and N are tied together closely at the building power entrance but then are distinct points.
 

Offline Dave

  • Super Contributor
  • ***
  • Posts: 1356
  • Country: si
  • I like to measure things.
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #4 on: October 11, 2024, 07:52:23 pm »
Fill a large buffer in RAM with ADC samples, pause on a breakpoint and extract the data from there. Then process the data on your computer to confirm you're sampling what you expect to be sampling.
<fellbuendel> it's arduino, you're not supposed to know anything about what you're doing
<fellbuendel> if you knew, you wouldn't be using it
 
The following users thanked this post: thm_w

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 8058
  • Country: ca
  • Non-expert
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 

Offline tom66

  • Super Contributor
  • ***
  • Posts: 7625
  • Country: gb
  • Professional HW / FPGA / Embedded Engr. & Hobbyist
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #6 on: October 11, 2024, 10:22:41 pm »
Measure with a scope first. I don't understand the purpose of the 10k/10k divider to 3.3V. Are you trying to AC couple the signal?


https://electronics.stackexchange.com/questions/244667/how-to-measure-mains-ac-voltage-with-an-adc-from-a-microcontroller
https://www.eevblog.com/forum/beginners/ac-voltage-measurement-with-a-microcontroller/
https://www.electro-tech-online.com/threads/measuring-ac-voltages-using-mcu.117073/

The 10k-10k resistor divider biases the ADC pin to 1.65V. This is then drawn either side of this voltage by the AC signal.  Since there's effectively 5kohm impedance to ground (3.3V is close enough to ground in this case) it forms roughly a 500:1 voltage divider but with the centre point at 1.65V.  You should see between 2.3V and 1.0V at the ADC pin, assuming 230VAC mains.

I have used this circuit before and it works fine for measuring mains voltage, though you have to be careful with the layout to make sure the resistors aren't overstressed or at risk of breaking down because there's nothing between the MCU pin and mains otherwise.  At a previous job, a guy loaded all of our boards with a 1k resistor instead of 1Mohm.  As soon as the boards were tested the PIC had a hole blown right in it!
 
The following users thanked this post: thm_w

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 15462
  • Country: de
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #7 on: October 12, 2024, 06:57:25 am »
The capacitors shown would filter out much of the AC part. The capacitors shown in the schematics should be removed or much smaller (pF range). If higher frequency ( > 10 kHz) performance is critical one may have to add capacitors in parallel to the resistive divider. Good accuracy would than need a trim.
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 4371
  • Country: Earth
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #8 on: October 12, 2024, 08:05:56 am »
Connect known voltage source on the input and record sample recording (with no skips) then extract it to PC and analyze what is going on
 

Offline SteveThackery

  • Super Contributor
  • ***
  • Posts: 1059
  • Country: gb
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #9 on: October 12, 2024, 10:42:06 am »
What is C1 for? I think you could dump it. Maybe it's charge-up time is causing bad readings until it reaches equilibrium. I would try it without.

Also, I don't know what C2 is for.  The ADC has its own sample-and-hold circuit.

I think all you need are the resistors: then it will be a pure voltage-divider-plus offset, with no phase shifting going on. If you need to absorb noise on the mains, you could either do that in software, or put C2 back in but adjust its value until the noise is sufficiently reduced, rather than guessing at a nice round number.

Myself I would do it in software.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6542
  • Country: es
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #10 on: October 13, 2024, 02:05:45 am »
I'd lower or remove the caps too, and buffer the signal using an op-amp so the adc pin sees a much lower impedance.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #11 on: October 13, 2024, 06:39:52 am »
The 100nF cap should help with the sampling current, however it's possible if you sample the pin too often you are drawing too much current on the input.

I second the suggestion to test with DC, but do so if possible with similar 510kohm ladder in series. For instance if you put 30VDC on the input what do you read pre and post reset.  That may give you an idea.

You can measure the DC voltage at the ADC pin and see what changes after reset, if anything.

Also mains voltage is typically measured from L to N, assuming your MCU is grounded if you are debugging it with a PC, you will get a less accurate reading as current flows down N and causes an offset relative to PE.  I am not familiar with India's electrical infrastructure but if it is anything like UK/Europe then PE and N are tied together closely at the building power entrance but then are distinct points.
Yes, I am sampling the pin quite frequently—800 samples spaced 125µs apart to calculate the RMS value. I’ll test with DC using a similar 510kΩ resistor ladder in series as suggested and measure the voltage before and after reset.

For debugging, I’m using a laptop that's isolated from mains. Thanks for pointing out the potential grounding issues; I’ll keep that in mind.
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #12 on: October 13, 2024, 06:47:19 am »
Fill a large buffer in RAM with ADC samples, pause on a breakpoint and extract the data from there. Then process the data on your computer to confirm you're sampling what you expect to be sampling.
I’m currently calculating RMS by using totalSamples += (adcRead - dcOffset) * (adcRead - dcOffset); (totalSamples is uint64_t variable) and only perform the final RMS calculation after collecting all 800 samples. Unfortunately, filling a large buffer in RAM isn’t feasible with the STM32F030C6, as it has limited memory, and I also need to scale this approach for 3-phase RMS calculations.
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #13 on: October 13, 2024, 06:50:53 am »
Measure with a scope first. I don't understand the purpose of the 10k/10k divider to 3.3V. Are you trying to AC couple the signal?


https://electronics.stackexchange.com/questions/244667/how-to-measure-mains-ac-voltage-with-an-adc-from-a-microcontroller
https://www.eevblog.com/forum/beginners/ac-voltage-measurement-with-a-microcontroller/
https://www.electro-tech-online.com/threads/measuring-ac-voltages-using-mcu.117073/
I don’t see any distortion on the oscilloscope. The 10k/10k divider to 3.3V is being used to add a DC bias to the signal.
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #14 on: October 13, 2024, 07:00:39 am »
Measure with a scope first. I don't understand the purpose of the 10k/10k divider to 3.3V. Are you trying to AC couple the signal?


https://electronics.stackexchange.com/questions/244667/how-to-measure-mains-ac-voltage-with-an-adc-from-a-microcontroller
https://www.eevblog.com/forum/beginners/ac-voltage-measurement-with-a-microcontroller/
https://www.electro-tech-online.com/threads/measuring-ac-voltages-using-mcu.117073/

The 10k-10k resistor divider biases the ADC pin to 1.65V. This is then drawn either side of this voltage by the AC signal.  Since there's effectively 5kohm impedance to ground (3.3V is close enough to ground in this case) it forms roughly a 500:1 voltage divider but with the centre point at 1.65V.  You should see between 2.3V and 1.0V at the ADC pin, assuming 230VAC mains.

I have used this circuit before and it works fine for measuring mains voltage, though you have to be careful with the layout to make sure the resistors aren't overstressed or at risk of breaking down because there's nothing between the MCU pin and mains otherwise.  At a previous job, a guy loaded all of our boards with a 1k resistor instead of 1Mohm.  As soon as the boards were tested the PIC had a hole blown right in it!
Yes, you're right. The 10k-10k divider biases the ADC pin to 1.65V, and I’m seeing the expected voltage range at the pin. I’ve been careful with the resistor ratings to avoid any breakdown, but I’m open to suggestions. Do you have any recommendations for improving the schematic or increasing the safety margin for the resistors, especially given the direct connection to mains?

In the code, I’m using totalSamples += (adcRead - dcOffset) * (adcRead - dcOffset);, where totalSamples is a uint64_t variable. After gathering 800 samples, I calculate the RMS value. If you have suggestions to optimize this or improve how I handle the ADC input, I’d really appreciate it.

Additionally, if you know any reference links for measuring mains voltage or improving ADC performance, that would be very helpful. Lastly, any advice on PCB layout to prevent overstressing components or breakdown would also be great.
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #15 on: October 13, 2024, 07:06:51 am »
The capacitors shown would filter out much of the AC part. The capacitors shown in the schematics should be removed or much smaller (pF range). If higher frequency ( > 10 kHz) performance is critical one may have to add capacitors in parallel to the resistive divider. Good accuracy would than need a trim.
I don’t see any reduction in amplitude or distortion on the oscilloscope. The low-pass RC filter’s cutoff frequency is 1591.55 Hz, which is well above the 50 Hz mains signal. However, I’ll try using capacitors in the pF range as suggested and see how it affects the performance.
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #16 on: October 13, 2024, 07:08:09 am »
Connect known voltage source on the input and record sample recording (with no skips) then extract it to PC and analyze what is going on
Okay, I’ll try connecting a known voltage source to the input and record the samples without skips, then extract and analyze the data on the PC. Thanks for the suggestion!
 

Offline Atlan

  • Frequent Contributor
  • **
  • Posts: 645
  • Country: sk
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #17 on: October 13, 2024, 07:08:25 am »
Rectify it with 1 diode, smooth it with a capacitor, measure the peak value, you don't need 800 samples, form 10. Calculate rms from the peak.  As you have it now, you are probably measuring something else and not a sine wave.
It would be ideal to connect an oscilloscope and see what the signal is on the adc pin.
FNIRSI 1013D Always provide a picture or video with the problem where the parameters of the oscilloscope are visible, and a picture of the diagnostic screen with the values.
Firmware is here (or not) https://github.com/Atlan4/Fnirsi1013D/tree/main/latest%20firmware%20version
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #18 on: October 13, 2024, 07:16:37 am »
What is C1 for? I think you could dump it. Maybe it's charge-up time is causing bad readings until it reaches equilibrium. I would try it without.

Also, I don't know what C2 is for.  The ADC has its own sample-and-hold circuit.

I think all you need are the resistors: then it will be a pure voltage-divider-plus offset, with no phase shifting going on. If you need to absorb noise on the mains, you could either do that in software, or put C2 back in but adjust its value until the noise is sufficiently reduced, rather than guessing at a nice round number.

Myself I would do it in software.
C1 is a bypass decoupling capacitor, and both C1 and C2 are there to suppress high-frequency noise. However, I understand your point, and I'll try removing both capacitors and see if it affects the readings.
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #19 on: October 13, 2024, 07:20:57 am »
I'd lower or remove the caps too, and buffer the signal using an op-amp so the adc pin sees a much lower impedance.
I will try removing the capacitors first and see the results. I've been considering using an op-amp as a last option. What do you think about the LMV358 rail-to-rail op-amp?https://www.ti.com/lit/ds/symlink/lmv324.pdf
 

Offline ShubhamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #20 on: October 13, 2024, 07:33:41 am »
Rectify it with 1 diode, smooth it with a capacitor, measure the peak value, you don't need 800 samples, form 10. Calculate rms from the peak.  As you have it now, you are probably measuring something else and not a sine wave.
It would be ideal to connect an oscilloscope and see what the signal is on the adc pin.
I don't want to use the peak-to-RMS calculation method because it won't work with other waveforms, like quasi-sine waves. I'm trying to create a true RMS measurement. I'm confident that I'm measuring RMS since I'm getting the expected results in both simulations and practical tests before resetting the MCU. I will connect the oscilloscope to the ADC pin when the divider output is connected, and I will share a picture of the waveform.
 

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 15462
  • Country: de
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #21 on: October 13, 2024, 08:52:52 am »
The cross over frequency for the low pass is indeed in the 1.5 kHz range, so should not effect the mains reading too much.
For testing I would stay away from mains but use a transformer and a low voltage, like 24 V and use less divider. Adjusting the divider and going to a higher voltage would be only after most testing and SW corrections are done.

One issue could be with the way how the offset is handled. If not correct (e.g. due to DC drift) small AC readings would be too high from the extra DC part. One can do a correction of the DC offset even after the summation over some 100 ms:  the RMS value without the DC part is sqrt ( sum(V²) - (sum(V)/N)²). Besides the DC part there is also a noise background, that can be an issue for small readings.

I point to check is if the µC / ADC  actually runs at the expected speed.

If the time is not exactly at 100 ms (or the mains frequency is rel. far off) there can be some error depending on the starting phase. Ideally one would start about 1/8 of a period off a zero crossing and thus around 70% of the peak voltage, so that the missing or added part would still be close to the correct RMS value.

A possible problem can be a digital input at the µC that is floating. This can cause extra variations in the supply current.

The LMV358 has a limited input voltage range. One would have to use it with a little gain (e.g. inverting amplifier or active divider), not just as a buffer.
As a buffer a RR OP-amp like MCP6001 would be more suitable, though with some input cross over that can add some error. Most RR OP-amps have this issue, though to different degree.
 

Offline SteveThackery

  • Super Contributor
  • ***
  • Posts: 1059
  • Country: gb
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #22 on: October 13, 2024, 09:33:45 am »
I don’t see any reduction in amplitude or distortion on the oscilloscope. The low-pass RC filter’s cutoff frequency is 1591.55 Hz, which is well above the 50 Hz mains signal.

You are right - I should have done that calculation myself before commenting.

A simple question - in your software,  are you waiting until C1 is charged up before making your measurements?
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5697
  • Country: mn
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #23 on: October 13, 2024, 11:22:07 am »
FYI - LTspice is your friend..
Readers discretion is advised..
 

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 15462
  • Country: de
Re: STM32F030C6 ADC RMS Voltage Measurement Inconsistency After Reset
« Reply #24 on: October 13, 2024, 12:17:47 pm »
1.7 V RMS may be just a little too much for the 3.3 V supply range.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf