H bridge current sensing

Hi I need help with my project, I have written code for an H bridge to control a motor. I am measuring current through the motor with an ACS712 and when the current goes above 3 amps I want the motor to stop. I have got it to work but I need it so when I reverse the motor it cancels the over current sense and work in the other direction.

Here is my code so far:

const int analogIn = A0; // ACS712 In
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue = 0;
int ACSoffset = 2156;
int LED = 13; 
double Voltage = 0;
double Amps = 0;

const int switchPin = 2;    // switch input
const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9;    // H-bridge enable pin





void setup(){

 pinMode(motor1Pin, OUTPUT);
 pinMode(motor2Pin, OUTPUT);
 pinMode(enablePin, OUTPUT);
 pinMode(LED, OUTPUT);
 

 digitalWrite(enablePin, HIGH);





}


void loop(){

 RawValue = analogRead(analogIn);
 Voltage = (RawValue / 1023.0) * 5000; // Gets you mV
 Amps = ((Voltage - ACSoffset) / mVperAmp);

 // if the switch is high, motor will turn on one direction:
   if (digitalRead(switchPin) == HIGH) {
     digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
     digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
  
     
   }
   // if the switch is low, motor will turn in the other direction:
   if (digitalRead(switchPin) == LOW) {
     digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
     digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low

     }

   if (Amps>3) digitalWrite (enablePin, LOW); // When current above 3 Amps turn off H bridge



   }

How about if the amps > 3 && switch pin is high shut off motor? Then if the switch is low that if won't run so the motor won't shut down.

Thanks for reply, that will work as well :slight_smile: what I need is when switch pin is high, spin motor in one direction until current goes above 3 amps then stop and stay stopped, then when the switch pin goes low send motor in the other direction until current goes above 3 amps then stop and stay stopped.

Hopefully I have explained it OK

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom..... :slight_smile:

Hi here is the schematic for my circuit :slight_smile:

When you reverse the motor, the current should return to normal because the motor is no longer stalled.

Hi,
Can you complete your diagram please, including the H-Bridge please.
Where are all the gnds connected.
Why does the ACS712 have a 5V and 12V connection.

Do you have the ACS712 bypassed?

It worries me that you want to continually run a motor to current limit to know when it has stopped.
Is it stopping in the same place everytime, if so then use a limit switch to detect the end of travel.

What is your application.

Tom... :slight_smile:

Hi the ACS712 has 5v input for the logic side as it outputs inbetween 0v and 5v. The 12v input is from a battery to the H bridge so it can detect the current going through the motor. The motor will not have continuous current through it as when it hits the end stops the current will shoot up very quickly, once the arduino has detected that I want it to turn the enable pin off on the H bridge. The motor will not be drawing any current once it has been turned off.

Then when the button is pressed I need the motor to spin the other way and do the exact same thing.

The motor is quite big and can handle the current going through it without a problem.

The H bridge I am using is this http://www.ebay.co.uk/itm/43A-BTS7960B-DC-Motor-smart-car-Driver-H-Bridge-PWM-Fast-Braking-For-Arduino-/171379578805?hash=item27e704d7b5

The ACS712 I am using is this 5x(acs712 5a Range Current Sensor Module for Arduino Pic UK W3z1 for sale online | eBay

Hi,

Okay..

What is your application.

Tom.... :slight_smile:

I work for a company that makes electric steps for vehicles, at the moment they use limit switches to detect when the step has reached the end of travel, but they don't want to uses switches anymore, so to make the product simpler we want to stop the step by reading the current through the motor instead :slight_smile: