I’m trying to migrate Emon from arduino platform to STM32. I’m not an expert in STM32 therefore I’m using STM32duino (STM32F103C8). Seems that scatch could be easily adopted to new platform with minor modifications. However I’ve faced strange behavior, which I’ve never seen before on arduino
My first thought is it’s noise pickup. That’s hardware. Either the bias supply is noisy, or the noise could be getting in via the ADC reference input from the power supply. A small (0.1 μF) non-electrolytic capacitor in parallel with the 10 μF will answer the first, changing your power source or heavily smoothing the 3.3 V / 5 V (or both) power rails should show whether it is noise on the supply.
My second thought is, as emonLib was intended for a 16-bit processor with a 10-bit ADC, have you got the correct scaling factors? That is, if you have a 1 V rms signal measured across R3, does it read your “calibration factor” current of 111.1 A?
Thanks for recommendation. I think that capacitor will help to have smooth and stable results. I will check it in the evening.
Regarding your second suggestion, for me it is not really clear how to adjust emonlib to 32bit processor and 12bit ADC. I tried to add analogReadResolution(12) but it does not work with my board. Than I found in the internet that for STM32 processors 12bit is a default value and there is no need to define it in the scatch. Could you help me with the direction of where to look for the correct scaling factor?
Regarding calibration value, you are completely right! In my arduino projects I’m using 60.6. Here I took simple example.
Yes, you are looking at two different - but related - things. If the scale factors are wrong, then the current you see will be wrong. That could mean that the ~1.2 A that you measure is not the actual noise. If (say) you are dividing by 1024 where you should be dividing by 4096, then your 1.2 A is only 0.3 A, and that is the sort of number we can see. Here’s how to check:
If ADC_BITS is set properly by the compiler, then emonLib should get the scaling for the 12-bit ADC correct. Have you looked in emonlib.h, have you set __arm__, or has your choice of board automatically set it? Try printing ADC_BITS - it should be 12 for you,
22 Ω is the burden we use in the 3.3 V emonTx and emonPi, for 100 A with a 100 A : 50 mA c.t. With a 33 Ω burden, your maximum current will be ~ 66.7 A.
But whatever the problem is, you are right, it should read zero with no current flowing - or with no c.t. connected. The reality is, it always will read a small current, because the real world is not perfect.
I received the first of my SCT-013-000’s today and it might be another week before the Arduino shield and EmonTx V3 arrives. So I built the circuit as per the docs, firstly for a Nano and then a Leonardo.
I used a 99 Ohm and 47 Ohm resistor in parallel for the burden resistor as I don’t have anything close to 33 Ohm. Online calcs show this should be 31.7 Ohm and I measured it at 31.9 so pretty close. I am using 470K bias pair and not using the OEM adaptor yet but will add that later.
Used the calibration as shown above by @mel from the docs and the results without the CT connected and 5V from the Arduino:
You should never need to change anything inside the library. It should be possible to adjust everything that you must to cater for most normal situations by using the ‘setter’ methods.
Yes, 30 - 50 mA is the sort of number we usually see with ‘breadboard’ builds. The emonTx can get a bit lower, due to a careful pcb layout.
I thought @mel said he had no input connected.
He could have a ‘noisy’ electrolytic capacitor - unlikely, but they’re not unknown.
Guys, I found where was part of my problem. Thanks to @Robert.Wall
I added ceramic capacitor and result still floating. Than I checked all connections and found bad contact.
I’ve noticed that this deviation is present everywhere:
on arduino and 5v power supply
on stm32 and 3.3v power supply
In addition other people has the same deviation. It seems to be stable - around 0.15 ±noise
Where it comes from? How to reduce it? Currently I’m reducing it with correction like @GeorgeB showed.
if (Irms0<k) {i0 = 0;} else {i0 = (Irms0-k)*v;}
where is k is a correction coefficient. In my case it is equals 0.18
You started a parallel thread How to enable 12bit for STM32F103
that logically continues from the beginning part of this one. Separating this across two threads will waste everyone’s time.
What you need to do is determine whether __arm__ or anything else that indicates that you have a 12-bit ADC is set, then use that to switch ADC_BITS from 10 to 12. What you appear to have done will cause bad results for any sketch you compile for a 10-bit ADC. Do not forget that the same library is used in every sketch, unless you fork your own version, which you keep just for the STM board. And if you do that, you can leave out the test for __arm__ and simply write #define ADC_BITS 12