Current and mains voltage readings Atmega2560 - Page 1
Forum section: electronics / general

author: dariotzvir
author_rank: Newbie
timestamp: September 17, 2021, 04:50:08 pm
content: 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

author: Doctorandus_P
author_rank: Super Contributor
timestamp: September 21, 2021, 08:27:47 pm
content: 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.

author: ledtester
author_rank: Super Contributor
timestamp: September 22, 2021, 01:26:12 am
content: Quote from: dariotzvir on September 17, 2021, 04:50:08 pmHave 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.pdfA simpler option is to just get one of these devices:Modifying the Peacefair PZEM-004T AC Comms Module (with schematic) - TheHWCave-https://youtu.be/qRsjsenvlJAAvailable 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

author: fchk
author_rank: Frequent Contributor
timestamp: September 24, 2021, 06:56:50 am
content: Quote from: dariotzvir on September 17, 2021, 04:50:08 pmAVR is too expensive for its computing power. Use ARM or PIC32.There are a lot of better options:- specialized analog front ends likehttps://www.microchip.com/en-us/product/mcp3911- energy measurement ICs likehttps://www.cirrus.com/products/cs5490/that do the whole rms and real power calculation internally.fchk

author: dariotzvir
author_rank: Newbie
timestamp: October 06, 2021, 01:09:57 am
content: 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));}(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

author: WattsThat
author_rank: Frequent Contributor
timestamp: October 06, 2021, 03:10:41 am
content: Another item not suitable for the task at hand is the ACS712. The commonly sold ACS712 breakout boards are even worse, given the pc trace spacings and no board cutouts.

author: dariotzvir
author_rank: Newbie
timestamp: October 06, 2021, 11:29:52 am
content: They are very bad, the ground plain extends to the mains terminals block and has no clearence between the tracks, and despite of the rating of the chip(5, 20 or 30A) you will have the same 10A terminal block, they are on my list of things that I have to repacle but when I searched to buy proper current transformers the shipping was like 30USD for 3USD of parts and yeah I kinda forgot.

author: Kleinstein
author_rank: Super Contributor
timestamp: October 06, 2021, 12:55:32 pm
content: If programmed right, the true RMS part does not need that much processing power. Just avoid floating points and do the math with integers. With 10 bits ADC reading the square would alreay be more than 16 bit and thus with C code 32 bit variable that can also be used to sum up the result. The sums can be done in real time, as the data come in. The only slow part is doing the root, but this only needs to be done once per reading and thus usually everay 100 ms or so. AFAIF the AVR needed only a small fraction of it's speed, like 1/4 of the cycle with 8 MHz clock.One can even take care of a DC offset afterwords. So the inital math can use a crude offset and than to the correction for the actual average when doing the final result.The Arduino framework is just slow and may not be suitable for the job.The Mega256 should be a OK. It gets a bit tricky with a tiny that does not have HW mulitplication.