April 30, 2012 AT 9:44 am

ASK AN EDUCATOR! “How can I control a security gate with my Arduino?”

Freddie asks:

I have a question about Arduino DC Motor Control:

To make the motor run forward and backwards is not a problem.
Also I am not concerned about the speed of the motor.
The H Bridge configuration is also not a problem.

But, to time the motor on and off seems not to be a regular discussion on the net.

I need to open and close a security gate simply by using a push button (like those tiny square ones always used with arduino diagrams and tutorials).
If the button is pressed momentarily, the motor needs to run one direction for e.g. five seconds and stop in its tracks and must wait for my next button press.
If I press the button again, the motor must reverse direction to its default position (same timing span ) and stop in its tracks again. So, it is purely timed on and off in either direction.

On the net are lots of code available for the control of the H bridge including the button state. But with all of those codes the motor directions are not timed. They simply forward or reverse continuously.

With my limited knowledge of coding in C (Arduino), I some how feel the delay instruction is not the way to resolve the issue (or can it be possible?), but rather to make use of the timer of the Arduino. Either options put me in the dark.

Can you advise?

Absolutely!

Rather then trying to time the Arduino to successfully open and close you gate, I would recommend using limit switches such as these. Use two, one to let the Arduino know that the gate is open, and the other to let it know the gate is closed. Then use a weatherproof pushbutton to tell the Arduino to “turn the motor CCW until the limit switch trips, indicating the gate is open” or “turn the motor CW until other limit switch trips, indicating the gate is closed.”

The other problem I see is that your security gate might run on AC rather then DC. In order for your Arduino to control the direction of an AC motor, you will need more complicated circuitry and it is considerably more dangerous. An option would be to use a AC Reversing Solid State Relay as it can be directly controlled by the microcontroller. Unless you are a trained electrician, I would NOT recommend interfacing your Arduino to an AC source.

Below is a rough schematic for your circuit and a bit of code that should get you started!

Code:

const int activate = 2; //set up our button constants
const int gateOpen = 3;
const int gateClosed = 4;
const int bridgeOneA = 5;
const int bridgeTwoA = 6;
const int enable = 7;
const int statLED = 13;
unsigned long currentTime = 0;
boolean gateState = false; //false = closed true = open
void setup() {
  Serial.begin(9600);
  pinMode(activate, INPUT); //set up I/O
  pinMode(gateOpen, INPUT);
  pinMode(gateClosed, INPUT);
  pinMode(bridgeOneA, OUTPUT);
  pinMode(bridgeTwoA, OUTPUT);
  pinMode(enable, OUTPUT);
  digitalWrite(enable, LOW); //make sure H-Bridge is off
  pinMode(statLED, OUTPUT); //setup our status LED
}
void loop() {
  if (digitalRead(activate) == HIGH && gateState == false) { //check to see if the button is pressed and the gate is closed
    digitalWrite(enable, HIGH); //enable h-bridge
    digitalWrite(bridgeOneA, HIGH); //configure for CW rotation
    digitalWrite(bridgeTwoA, LOW);
    while(1){ //run motor until switch is tripped
      if (digitalRead(gateOpen) == LOW) { //check switch state
        gateState = true;
        digitalWrite(statLED, LOW); //turn off LED
        digitalWrite(enable, LOW); //disable h-bridge
        digitalWrite(bridgeOneA, LOW); //reset h-bridge config
        break;
      }
      if (millis() > currentTime + 500) { //flash status LED once
        digitalWrite(statLED, HIGH);
        delay(500);
        currentTime = millis();
      }
      else {
        digitalWrite(statLED, LOW);
      }
    }
  }
  if (digitalRead(activate) == HIGH && gateState == true) { //check to see if the button is pressed and the gate is open
    digitalWrite(enable, HIGH);
    digitalWrite(bridgeOneA, LOW); //configure for CCW rotation
    digitalWrite(bridgeTwoA, HIGH);
    while(1){
      if (digitalRead(gateOpen) == LOW) {
        gateState = false;
        digitalWrite(statLED, LOW);
        digitalWrite(enable, LOW);
        digitalWrite(bridgeTwoA, LOW);
        break;
      }
      if (millis() > currentTime + 500) { //flash status LED once
        digitalWrite(statLED, HIGH);
        delay(500);
        currentTime = millis();
      }
      else {
        digitalWrite(statLED, LOW);
      }
    }
  }
}

I haven’t tried out the code, but it should get you started in the right direction. The idea is that when the button is pressed, the Arduino configures and activates the h-bridge, moving the gate until the “open” limit switch trips and stops the motor. Then when the button is pressed again, the Arduino configures and activates the h-bridge, moving the gate until the “closed” limit switch trips and stops the motor again. There is a also small bit of code that flashes the LED during the time the motor is in motion using the mills() counter.

I hope this has helped to steer you in the right direction and good luck with your project!

Don’t forget, everyone is invited to ask a question!

Click here!

“Ask an Educator” questions are answered by Adam Kemp, a high school teacher who has been teaching courses in Energy Systems, Systems Engineering, Robotics and Prototyping since 2005.


“D is for Diode” – Circuit Playground Episode 4 is out now! CLICK HERE TO WATCH!

Have an amazing project to share? Join the SHOW-AND-TELL every Wednesday night at 7:30pm ET on Google+ Hangouts.

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Learn resistor values with Mho’s Resistance or get the best electronics calculator for engineers “Circuit Playground”Adafruit’s Apps!



No Comments

No comments yet.

Sorry, the comment form is closed at this time.