Tuesday, June 8, 2010

Motor Driver Activate!





Here's a link to the motor driver I got:
http://www.sparkfun.com/commerce/product_info.php?products_id=9670

It supports 2 motors, backwards and forwards. Now you might ask, why did I need a motor driver? 2 reasons.

1.) The Arduino doesn't have enough current output to handle a motor without it wearing down the Arduino.
2.) I need to fire the motor forwards to unlock the door and then backwards to lock it again. You do this by changing the polarity on the motor, which is done using an H-Bridge.

I originally wanted to build my own H-Bridge, but after researching it I realized it was over my head, so I bought a nice, already put together motor driver. This thing is so easy to use.

I supply it power. Set pin ENA to high to enable motor 1. Then you set IN1 to high to turn the motor one way and IN2 to high to turn it another way. That's it!

I edited the code, only the following section changed:


// Opens door and turns on the green LED for setDelay seconds
void openDoor( int setDelay )
{
setDelay *= 200; // Sets delay in seconds
digitalWrite(powerPin, LOW); // Turn off blue LED
digitalWrite(failPin, LOW); // Turn off red LED
digitalWrite(passPin, HIGH); // Turn on green LED

digitalWrite(ENA, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN1, HIGH); // Unlock door!

delay(1000); // Hold door lock open for 5 seconds
digitalWrite(ENA, LOW);

delay(10000);
digitalWrite(ENA, HIGH);
digitalWrite(IN1, LOW); // Relock door
digitalWrite(IN2, HIGH);

delay(1000);
digitalWrite(passPin, LOW);
digitalWrite(ENA, LOW);
}


EDIT: Ok that was a lie, this was added to the beginning also to define the pins.


#define ENA 2
#define IN1 3
#define IN2 4

1 comment:

  1. I just want to say thank you for posting this code. :)

    ReplyDelete