Current sensing with brushed DC motors

The project is a double door 10 ft. gate, powered by 12V DC motors with threaded spindles.
The UNO controls 4 relays with 30A contacts.
As a safety feature to protect both the mechanism and persons or objects between the doors I have included current sensing with ACS712's connected to analog inputs.

A simple routine takes 3 samples for each motor, the highest value is compared with a MaxCurrent field.
I am not interested in the actual Amps, just the output of the sensors, so I use no conversion. In this experimental phase the sketch writes the sensor values to an LCD display.

With the motors not running, the Arduino reads a value of approx. 528. With a motor powered the values vary wildly between 680 and 470. The value below 528 means current is flowing in the wrong direction, possibly because of the mass of the gate and friction in the hinges.
At the present I use 800 in the MaxCurrent field, but that value is too high to offer real protection: the drives with spindles has enough force to cause serious damage before 800 is reached.

Is there a better way to obtain an average current reading without long delays?

void getCurrent() //finds the highest of 3 current measurements
{
  leftCurrent = analogRead (leftcurrentPin); //to be measured on each loop
  tempCurrent = leftCurrent; //store the value
  delay(5);
  leftCurrent = analogRead (leftcurrentPin); //read again
  if (leftCurrent >= tempCurrent) //if higher, store it
  tempCurrent = leftCurrent;
  delay(5);
  leftCurrent = analogRead (leftcurrentPin); //read the 3rd time
  if (leftCurrent >= tempCurrent) //if higher, store this one
  tempCurrent = leftCurrent;
  leftCurrent = tempCurrent; //This is the highest value
  lcd.setCursor(0,0);
  lcd.print (leftCurrent);
  rightCurrent = analogRead (rightcurrentPin); //first reading
  tempCurrent = rightCurrent;
  delay(5);
  rightCurrent = analogRead (rightcurrentPin); //second reading
  if (rightCurrent >= tempCurrent)
  tempCurrent = rightCurrent;
  delay(5);
  rightCurrent = analogRead (rightcurrentPin); //third reading
  if (rightCurrent >= tempCurrent)
  tempCurrent = rightCurrent;
  rightCurrent = tempCurrent; //This is the highest value
  lcd.print (rightCurrent);
}

Personally, I would go about it differently.
First off, I would use a for loop which radically reduces the size of the code.
Secondly, I’m not sure that I would extract only the largest reading. I would probably just average the 3 readings. Because the Maximum value of an int is many times larger than the value you can get from a single analogRead, I would just sum them up and then divide when done.
Thirdly, especially if you are concerned about how long everything takes, I wouldn’t use all those delay(5) statements. I understand that you don’t want to read too close to each other. But couldn’t you interleave the readings?

Something like:

void getCurrent()
{ leftCurrent = 0;
rightCurrent = 0;
for(int i = 0; i<3;i++)
{ leftCurrent += analogRead(leftcurrentPin);
rightCurrent += analogRead(rightcurrentPin);
if(i<3)
delay(3);
}
leftCurrent = leftCurrent / 3;
rightCurrent = rightCurrent / 3;
}
If you really want to extract only the largest reading, then appropriate changes would, of course be necessary.

How do you measure the currents? Circuit diagram?

I'd calculate the mean value of the readings. If the current decreases again after a peak, you can get a false trigger otherwise.

I don't think that monitoring the current will help with threaded spindles. Apply some acceptable force to the doors, and find out the related current difference. Would you dare to hold a finger into the gap, and trust your controller to deactivate the motor in good time? IMO your finger will cause less resistance to the motor than whatever dirt or friction in the door mechanism will cause in normal operation. For less painful tests I'd use apples, oranges or eggs, or a spring scale for the determination of the real cut-off force.

Measuring current is a useful device to prevent damage to the motor but I would not rely on it for preventing damage to humans.

I think you need some form of independent force sensor.

...R

There also is a taut steel wire on the edge of one door. A micro switch (NC) is tripped when the wire touches an object during closing.

Other objects like balls, bicycles, garbage bags etc. sometimes get between an opening door and the stone wall; these I want to detect by current sensing. It has also happened that a limit switch failed and the drive tried to commit suicide.

The ASC712 is in the negative leads of the relay. It functions by measuring the magnetic field around a very small copper conductor and is designed for AC and DC up to 50 Khz. The output at rest is 1/2 Vcc.

Did you ever check the current signal with a scope?

IMO the bandwidth of the ASC712 should be limited, to something well below 10Hz, to remove noise from bouncing/sparkling brushes. It would be better to filter the current before the sensor, but that's hard with a 1.2mOhm resistance. Some impedance may work better there.

CDK1:
It has also happened that a limit switch failed and the drive tried to commit suicide.

IMHO current sensing to provide some sort of overload cut-off makes sense to protect the drive mechanism. But that is very different from protecting against injury to people or other animals.

Could you use an ultrasonic sensor to detect obstacles?

Could you organise the gate so that at the normal fully opened position there is space behind it for the sorts of things that are likely to get behind it - such as a football or bicycle.

…R

DrDiettrich:
Did you ever check the current signal with a scope?

IMO the bandwidth of the ASC712 should be limited, to something well below 10Hz, to remove noise from bouncing/sparkling brushes. It would be better to filter the current before the sensor, but that's hard with a 1.2mOhm resistance. Some impedance may work better there.

No, I never checked it, but I have a pretty good idea what it looks like. It starts with a tremendous spike followed by a lot of noise. All brushed DC motors behave that way.
Just sensing stalling current would be much easier, but that is approx. 30 Amps, which means a force well over a ton! In fact the drive is too heavy for this project, but I don't want to start all over again.

The ASC712 has a provision to reduce bandwidth with an external capacitor in an RC filter. I bought it as an "electronic brick" that has a tiny smd cap without markings. Replacing it with something larger is difficult, so I prefer a software solution.

CDK1:
Replacing it with something larger is difficult, so I prefer a software solution.

Easy to piggy-back an smd cap on top of the existing (1n?) cap.
You could also add an RC filter between sensor and analogue pin.
Try 10k/100n, and filter more in software if needed.
Leo..

Robin2:
Could you organise the gate so that at the normal fully opened position there is space behind it for the sorts of things that are likely to get behind it - such as a football or bicycle.

…R

This is a picture of the left part of the gate. The wall is at least 5 centuries old, I reinforced it with concrete when I installed the gate 20 years ago.
With the gate fully opened there is almost no space left.
I have no bike and do not play with balls, but there are tourists…

Image from Reply #9 so we don’t have to download it. See this Image Guide

9d0f912877ed90d70460b05e986166f6b446de08.jpg

…R

Consult your accident insurance for terms and prices for your door mechanism :-]

For best safety I'd suggest a controlled door lock and let the visitors open the door manually. Closing by some passive door closer, and an alert when the door is not properly closed for some time.

A pair of strain gauges somewhere on the gate or fixed frame holding the actuator might give you a more reliable feedback on what forces happen there.

CDK1:
The project is a double door 10 ft. gate, powered by 12V DC motors with threaded spindles.
The UNO controls 4 relays with 30A contacts.
As a safety feature to protect both the mechanism and persons or objects between the doors I have included current sensing with ACS712's connected to analog inputs.

I have a similar, commercially produced, system on my gates in France. There are no limit switches and it relies entirely on monitoring the motor current to detect the limits. The system is a little smaller than yours being a 2.5 metre opening with two gates. Each motor uses about 2 A when operating and 8 A in stall condition. The motor stops about 2 s after stall detection. I guess the stall current and thus the torque is limited to some extent by the current sensing resistor. I must admit I haven't tried inserting a finger between the gates when they are closing!

There is an IR beam to prevent the gates closing if the beam is broken.

Russell.

DrDiettrich:
For best safety I'd suggest a controlled door lock and let the visitors open the door manually. Closing by some passive door closer, and an alert when the door is not properly closed for some time.

We had manual operation during the summer months after a tourist damaged one drive. Even then not everybody seemed to understand how it worked. Now I want to improve things and again make use of the remotes we have in our cars.
The system works flawlessly as long as there are no visitors.

russellz:
I have a similar, commercially produced, system on my gates in France. There are no limit switches and it relies entirely on monitoring the motor current to detect the limits. The system is a little smaller than yours being a 2.5 metre opening with two gates. Each motor uses about 2 A when operating and 8 A in stall condition. The motor stops about 2 s after stall detection. I guess the stall current and thus the torque is limited to some extent by the current sensing resistor. I must admit I haven't tried inserting a finger between the gates when they are closing!

There is an IR beam to prevent the gates closing if the beam is broken.

Russell.

Very interesting!
Is there protection in case objects are placed against the wall the gates turn to?
Does the IR beam work in bright sunlight?
How much current draws the installation at rest?
Does it use threaded spindles or a rack and pinion?

Gate opener project here.

Diagram in post#27.
Leo…

CDK1:
Is there protection in case objects are placed against the wall the gates turn to?
Does the IR beam work in bright sunlight?
How much current draws the installation at rest?
Does it use threaded spindles or a rack and pinion?

There is no specific protection for objects placed against the wall. I have fixed a solid stop in the ground in concrete. The current sense circuit is adjusted to trip at just over the normal running current.
The IR beam works in sunshine as it uses pulsed IR and detects the pulses rather than continuous. I did however mount the detector on the shady side and the transmitter on the sunny side. Its purpose is to prevent the gates from closing on a vehicle.
It draws about 40 mA at rest.
It uses square threaded rods with nylon nuts sliding along them.

It is a 12 V system with a small (30 cm x 60 cm) solar panel charging a car battery.

Russell.