How do I code this?: If DC motor draws 150% more amps now than 1s ago = Stop

I have made a device that opens and closes using DC motors. As a safety precaution, I want the motors to stop if they draw more amps than needed. This way they will stop if they encounter interference.

The problem is that due to the design the amp draw needed is inconsistent through the movement and the pinch dangers are numerous. The best way I can figure to manage this is if the Arduino constantly reads the amps and compares that reading to the reading just before and if there is a sharp increase the motors will stop and reverse for a couple seconds.

Thoughts? Is this a good idea? Can it be done?

Hi, and welcome to the forum.

How are you controlling the motors now.
There might already be a current output on your motor driver.
Or you have to use an external current shunt (low value resistor).
The Arduino can measure shunt voltage (motor current), and act upon that.
Make sure you read forum rules first (on top of every main page of the forum).
Leo..

That approach can save the motor and controller from damage but won't prevent a nasty pinch to a human, industrial controls use "both hands to enable" or devices like light curtains to prevent such mayhem.

Wawa:
Hi, and welcome to the forum.

How are you controlling the motors now.
There might already be a current output on your motor driver.
Or you have to use an external current shunt (low value resistor).
The Arduino can measure shunt voltage (motor current), and act upon that.
Make sure you read forum rules first (on top of every main page of the forum).
Leo..

Thank you. I read through the rules last night. Thanks for the suggestion.

I am controlling the motors with an Adafruit Motor Shield v 2.3 and Arduino Uno. I'll have to do more research to see if the motor shield gives output current or not. I may have more questions about the shunt voltage later, but I'll do some research first.

Assuming the Arduino can know the real-time current drawn by each motor is there a code function or formula that will take the current input and return the % change in real-time? This way I can tell the motors to stop if current draw increases more than let's say 20% in 500ms.

...
   current_draw = analogRead(sensorpin);
   if (current_draw > 15*last_current_draw/10) panic();  //50% increase current draw?
   last_current_draw = current_draw;
...

I would be inclined to use a pair of low-pass filter for this:

float filteredAmpsSlow;
float filteredAmpsFast;

void loop() {
  // its important to take a sample at known, regular intervals
  if(its time to take another sample) {
    float currentAmps = take_a_sample();

    filteredAmpsSlow = 0.99 * filteredAmpsSlow + 0.01 * currentAmps;
    filteredAmpsFast = 0.75 * filteredAmpsFast + 0.25 * currentAmps;


    if(filteredAmpsFast - filteredAmpsSlow > CURRENT_SPIKE) {
      omg_shut_everything_down();
    }

  }
}

You can think of the two filters as each being a capacitor/resistor network. They will follow along after the incoming data, but it takes them a few samples to catch up. Consequently, they filter out noise in the data and tend to reflect the average values. One reacts faster than the other, so the difference gives you information about how fast the data is changing. The nice thing is that you don't need a circular buffer or anything like that.

The important variables are the sample rate, the sensitivity set by AMPS_SPIKE, and the low-pass sensitivity set by the .99/.01 and the .75/.25 ratios of the two low-pass filters.

I'd be inclined to put a data series in a spreadsheet column simulating the data you will get in normal running and in a fault situation and twiddle the three numbers until the algorithm properly detects the condition you are looking for.

Problem is that that shield doesn't have motor current sensors.
OP can try to measure incoming motor supply (+) with e.g. an ACS712.
Leo..

Wawa:
Problem is that that shield doesn't have motor current sensors.
OP can try to measure incoming motor supply (+) with e.g. an ACS712.
Leo..

So, we'd be looking at purchasing one of these or something similar.

Yes.
Select one that matches motor current (higher A/D resolution).
There is a 5A, 20A and 30A version.
Leo..

Thank you for the information. I hope to have time this week to try what's been suggested here. I'll post the results.