Electronics > Microcontrollers

Current and mains voltage readings Atmega2560

(1/2) > >>

dariotzvir:
Hi, recently I started working in a small local company where I live, and so I`m devolping a controlled power plug to be able to monitor and control any device plug to it and then you can control it over internet when you are not on site. I`m currently using an Arduino Mega because is what I learnt in school,  there are five plugs and each one has an ACS712 connected for current and for voltage a ZMPT101B which is a little current transformer with a differential opamp at the output, and then using the internal ADC.
Everything  is smooth up to there with everything that is loaded in the firmware I have to little room to make the readings, since a loop of the program is like 15mS (meassured from the Serial monitor of the IDE) and then sampling the 6 inputs takes other 7mS and so if I want to make anything like a "Trms" it´s kinda hard because the method that I´m using currently is a low-pass software filter and a standard devitation and if I have too much time between the readings they go to hell or the integration time of the filter (correct me if it`s the wrong term) it`s too long and so if you have a voltage or current spike you will never see it because of the long time of integration.
I don´t pretend to build a 5 digit multimeter with an atMega2560 but I wanted to know since I`m quite lost, about another way or reference of measuring to get a decent sort of trms to not only meassure the peaks of the wave and then take the rms. Thanks for your time

Doctorandus_P:
You have been thoroughly duped by the arduino framework.

Arduino sucks beginners into blinking led programs and teaches them very sloppy programing, and when they need to do more then blink a few LED's their projects start to fall apart.

To get from "arduino" to "decent programming" you have to completely change course, plow your way through some documentation and read a few good books and practice with that info.

The proper way is to divide a program into a bunch of small tasks. You let ISR's handle the input and output, and the ISR's read from, and write to buffers. Then, in your main program you either keep a keen eye on your buffers, or you wait and react to signals from ISR's.

One of the goals is to keep the interrupts small, because they get executed at "random" intervals. The "bulk" of the code is executed in the main loop, which has (or should have) not very strict timing constraints.

This means that tasks like "sampling the inputs"  and "reading Serial Data" or Writing Serial Data" is all done by the ISR's in 100us (or even smaller) chunks, and is also done at exactly the right moment. Your main loop is (almost) free to do the filtering algorithms and other house keeping tasks.

Managing such ISR's and the way how they interact with buffers and the main loop is where real programming starts.

but also, depending how many samples you take, calculating RMS values and filtering can take a serious chunk out of the available clock cycles, but I don't know how much. You should benchmark such algorithms, but make a careful distinction between the uC doing something useful and wasting valuable cycles in a delay() loop. (Which is sloppy programming "taught" by "arduino" to "beginners" who have to un-learn it later.)

If the processor speed is really a bottleneck, there are plenty of options, both with microcontrollers running at several hundred MHz and with microcontrollers having built-in floating point instructions.

ledtester:

--- Quote from: dariotzvir on September 17, 2021, 04:50:08 pm ---I don´t pretend to build a 5 digit multimeter with an atMega2560 but I wanted to know since I`m quite lost, about another way or reference of measuring to get a decent sort of trms to not only meassure the peaks of the wave and then take the rms. Thanks for your time

--- End quote ---

Have a look at this app note for ideas on how to perform the measurements:

http://ww1.microchip.com/downloads/en/AppNotes/Atmel-2566-Single-Phase-Power-Energy-Meter-with-Tamper-Detection_Ap-Notes_AVR465.pdf

A simpler option is to just get one of these devices:

Modifying the Peacefair PZEM-004T AC Comms Module (with schematic) - TheHWCave
- https://youtu.be/qRsjsenvlJA

Available on ebay/aliexpress/banggood.

There is also a follow-up video on the accuracy:

How accurate is the Peacefair PZEM-004T AC Comms Module?
- https://youtu.be/j0_y8dPfpKc

fchk:

--- Quote from: dariotzvir on September 17, 2021, 04:50:08 pm ---Hi, recently I started working in a small local company where I live, and so I`m devolping a controlled power plug to be able to monitor and control any device plug to it and then you can control it over internet when you are not on site. I`m currently using an Arduino Mega because is what I learnt in school,  there are five plugs and each one has an ACS712 connected for current and for voltage a ZMPT101B which is a little current transformer with a differential opamp at the output, and then using the internal ADC.
Everything  is smooth up to there with everything that is loaded in the firmware I have to little room to make the readings, since a loop of the program is like 15mS (meassured from the Serial monitor of the IDE) and then sampling the 6 inputs takes other 7mS and so if I want to make anything like a "Trms" it´s kinda hard because the method that I´m using currently is a low-pass software filter and a standard devitation and if I have too much time between the readings they go to hell or the integration time of the filter (correct me if it`s the wrong term) it`s too long and so if you have a voltage or current spike you will never see it because of the long time of integration.
I don´t pretend to build a 5 digit multimeter with an atMega2560 but I wanted to know since I`m quite lost, about another way or reference of measuring to get a decent sort of trms to not only meassure the peaks of the wave and then take the rms. Thanks for your time

--- End quote ---

AVR is too expensive for its computing power. Use ARM or PIC32.

There are a lot of better options:
- specialized analog front ends like https://www.microchip.com/en-us/product/mcp3911
- energy measurement ICs like https://www.cirrus.com/products/cs5490/ that do the whole rms and real power calculation internally.

fchk

dariotzvir:
Yeah, arduino ecosystem it´s not meant for this sorts of things even from my reduced knowledge I already can see the problems of not having a debbuger, strange things that happen with the linker(i think) but also it´s not worth the time of the project (not mine because i would prefer to do it in a better way) to change the enviroment of maybe the hardware, idk too much insecurities.
And in what ISRs respects they were something that i tried but in a very basic way of just putting the call for the adc and the filter onto the ISR so it was trash obsiously, maybe a better move would be to ditch the:

--- Code: ---void ISR()
{
    filter.input(analogRead(pin));
}

--- End code ---
(for reference sake) and just store the ADC reading, and maybe do it with port manipulation myself so i cant put aside the innecesary checks of analogRead()
Thanks for the response

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod