Value Balancing using their deviation as a basis

Okay so I'm going to give an analogy:

Let's say that there are 9 rocks, and the weight of these rocks are the output values of a sensor (current sensor to be exact, in Amperes), and I also have three scales (branch circuits).

When there is nothing plugged into an outlet, the current sensor reads 0 Amps, or let's say 0 Kilograms.

If there is something plugged into an outlet (outlet 1), the current sensor reads the current draw, let's call it I(Outlet1), and there are 9 outlets, their current values are I(Outlet2) for the 2nd, I(Outlet3) for the 3rd, and so on up to the 9th outlet.

My question is, is there a library, or function, or example code, or something, that can balance these values, like, put rocks on the scales so that the scales have a low %difference?

The process that I'm thinking of is that there are 9 values, right? And the code iterates three different combinations of these values, so that their deviation from one another is within tolerance, say 5%.

It is also okay to have one of these branch circuits are not loaded, or just one is loaded if the value of the current is too low.

Any help would be appreciated, thanks!

You have to be much more concrete about what you are trying to achieve for a reasonable answer.

It sounds a bit like a load cell problem where you are trying to calculate the weight of an object, where that weight is distributed over a number of load cells.

To start with:
What arduino are you using ?
What sensors are you attempting to connect to it ?

how do you control the current (A) in each outlet?

6v6gt:
What arduino are you using ?

I'm using the Arduino Mega

6v6gt:
What sensors are you attempting to connect to it ?

The specific sensor is the PZEM-004T-100A, the value I am working with is the current reading from the sensor's current transformer.

gcjr:
how do you control the current (A) in each outlet?

There are outlets and you can plug anything that uses 220V into it (appliances, tools, etc)

I tried putting it in simpler terms as I have done so with my groupmates, in order for others to understand the problem I am working with. It's basically a problem of what different combination of rocks will evenly distribute their weight between three scales? Something like that.

The project itself is a PLC-Based Overload Protection System via Automatic Load Transfer. The way it works is when loads are plugged into an outlet (say, an appliance, power tools, etc), the system will do its best to distribute the current draw between three branch circuits.

If there are any more questions I will try my best to answer them.

Thanks in advance, everyone!

Start by dropping the ridiculous "rocks" thing. You're measuring current, not rocks, not weight. Using rocks and current in the same sentence just muddies the waters. People here are perfectly capable of understanding current, and current measurement. Forget about "rocks", and describe what you're REALLY trying to do, what you're REALLY measuring, and how, and what parameters the Arduino is capable of changing, and how.

Regards,
Ray L.

Each device draws current according to the applied voltage and its own impedance.

Unless you intend to control the applied voltage, end of story.

RayLivingston:
describe what you're REALLY trying to do, what you're REALLY measuring, and how, and what parameters the Arduino is capable of changing, and how.

Basically the Arduino reads the current reading from the sensors (PZEM-004T-100A). There are 9 sensors, so there are 9 current readings. These 9 current readings (or a few depends on how many things are plugged in) are then sent to one of three branch circuits, in order to balance the current readings across the three (or two, if the current is too low) branches. The parameters the Arduino is capable of changing is the branch circuit the outlet is connected to (via SSRs).

jremington:
Each device draws current according to the applied voltage and its own impedance.

Unless you intend to control the applied voltage, end of story.

Yes, I know. Please read my above explanation of the working principle in order to understand the project. Thanks

crizangel:
Please read my above explanation of the working principle in order to understand the project.

it's obviously not clear enough. can you explain some more

For each load:
if moving that load from its current branch circuit to one of the other branch circuits would make the total load on each of those two branches more similar, move that load to the other branch.

For example if Branch1 is providing 5 amps, Branch2 is providing 8 Amps, and Branch3 is providing 6 Amps you would move a 2 Amp load from Branch 2 to Branch 1 to make the totals 7, 6, and 6 but if there were only two 4-Amp load on Branch2 you would not move either of them to Branch1 because that would make them 9,4,6 an the difference between 9 and 4 is greater than the difference between 5 and 8.

It now looks like you have a network of power circuits with some "weak links" in it.
You want, using an Arduino mega and PZEM-004T-100A sensors, to start measuring the power usage in this network and start to regulate it using SSRs.

Can you now draw a diagram of the network topology?

crossed with @johnwasser

For more precision:

For each combination of two branch circuits:
Calculate the load on each branch (sum the loads on that branch circuit).
Calculate the difference in load between the two branch circuits.
On the branch circuit with the heavier load:
Find and move the largest load less than half the load difference.

gcjr:
it's obviously not clear enough. can you explain some more

Okay.

The machine consists of 9 outlets, the sensors, the solid state relays (SSRs), and the Arduino.

Initially (and obviously), the current readings from the sensors read 0 amperes (all we care about is the current, disregard voltage and power).

When there are loads plugged into the outlets (appliances, tools, anything that plugs into an outlet), they draw a certain amount of current. Each outlet has 3 SSRs, and each SSR is connected to one of three branch circuits. The purpose of the SSRs are to change what branch circuit the outlet is connected to.

The power analyzers (sensors) read the current in each outlet via a current transformer and sends it to the Arduino as a number (in amperes).

Using some code (which I need help with), the Arduino figures out which branch circuit each outlet needs to connect to in order to evenly distribute the current.

For example, across the 9 outlets, there are four 5 ampere loads, three 3 ampere loads, and two 8 ampere loads.

The code iterates the best way to connect the outlets to one of three branch circuits, so that the loads are evenly distributed across the three branch circuits.

In this case,
Branch Ckt 1: BC2: BC3:
2 - 8A 1 - 3A 2 - 3A
2 - 5A 2 - 5A

Total: BC1: 16A, BC2: 13A, and BC3: 16A

There may be some other combination, but this was the best that I could came up with.

Then the process is complete and the circuit will then be rebalanced once there is a noticeable change in current in at least one outlet.

johnwasser:
For each load:
if moving that load from its current branch circuit to one of the other branch circuits would make the total load on each of those two branches more similar, move that load to the other branch.

For example if Branch1 is providing 5 amps, Branch2 is providing 8 Amps, and Branch3 is providing 6 Amps you would move a 2 Amp load from Branch 2 to Branch 1 to make the totals 7, 6, and 6 but if there were only two 4-Amp load on Branch2 you would not move either of them to Branch1 because that would make them 9,4,6 an the difference between 9 and 4 is greater than the difference between 5 and 8.

I think you hit the nail on the head, there. But please try to read my above explanation to see if it matches with your understanding. Thanks!

6v6gt:
Can you now draw a diagram of the network topology?

If by network topology you mean the circuit diagram, I have attached a somewhat complete circuit diagram.

NOTE: The wiring from the convenience outlet to the BCs are incorrect, the correct one is that each row of SSRs connect to a BC, so row 1 connects to BC1, 2 to 2, and 3 to 3. Also, the L terminal for the outlets are all connected. Only the N terminal is the one that jumps from BC to BC.

I imagine it would be possible to come up with an even more complicated solution, considering that the problem is rather simple.

The circuitry you propose would never be practical, and a PLC is obviously not required, so Is there some general point to this exercise?

sound like you want to pick one branch from each outlet that minimizes the variation between outlets.

you could do a brute force search and keep track of which set of branches has the least variation

for each branch on outlet 1
for each branch on outlet2
for each branch on outet3
calc variation
if (variation < bestSoFar)
capture this set of variations
bestSoFar = variation

OK. The diagram from post #11 is clear.
Now we have to go back to the purpose of the whole exercise.

If you go to an professional electrician and you say you want nine 230volt sockets installed, he will produce a plan which corresponds with the regulations which govern electrical installations in that country. There will be no current measurement, there will be no SSRs and no Arduino. Naturally, the regulations normally foresee a "worst case scenario"

What you appear to have is a "knowledge" of the loading of the various circuits and want to design something which "intelligently" handles loading and does some sort of dynamic switching so as to avoid handling the "worst case scenario" mentioned above.

Is that a fair assessment of this activity ?

6v6gt:
If you go to an professional electrician and you say you want nine 230volt sockets installed, he will produce a plan which corresponds with the regulations which govern electrical installations in that country.

That sounds like the sensible thing to do.

It will also be instructive to ask where the power is coming from that supplies the 9 sockets.?

In the normal course of things it will all come from the electricity grid. And the electricity grid will see the total load regardless of which socket the loads are connected to. So why not design the system so each socket can take the maximum load that might be connected to it?

...R