Arduino Linux

Controlling a Lock with an Arduino and a Servo

December 15, 2010

The Chronobox – Part 2

The other day I wrote about a project I was building that was a time based lockbox which I have now dubbed the “Chronobox.” Much like the Reverse Geocache this lockbox can only be opened from the inside and that will only happen if a certain set of parameters are met. In this case, that parameter is time.

Today, rather than focusing on Fritzing, I’ll be starting to cover some builds I’ve done on the protoshield to get familiar with each of the components needed for this project as well as to try out a few different locking mechanisms I’ve had on my mind.  The first will be a simple circuit used to control a servo which will open a latch. My setup is just a standard blue Arduino Duemilanove and a Proto Shield from Adafruit with a mini breadboard fastened on top.  I also have a motor shield from Adafruit as well but I need to remove the current headers and replace them with stackable headers before its of any use to me, why all shields font come with these by default is beyond me.  My final configuration will likely be built out on a Hardcopy (which is apparently now known as the Protino).

Wiring the circuit was quite simple, I took 3 break away header pins and slit the center divider down to the middle and pushed it into the header from the servo which then allowed me to plug the header into the bottom right of the mini breadboard.Arduino Servo/Pushbutton - SchematicThe black wire was ran to the ground rail and the red wire run to the 5v rail (if you’re not using a proto shield you can just run them directly to the ground and 5v pins on the arduino). The yellow control wire was run to on of the Digital PWM pins (Pin 9 in my case). The switch was wired by running a ground wire with a 1k Ohm resistor bridging the gap and a wire run to the 5v rail. Another line is then run from Digital pin 10 to a position between the 1k Ohm resistor and the push button. When the button is idle the current will run from the ground, through the resistor into the input pin giving you a reading of low and when you press the button 5v is sent through the circuit into a the digital pin which will cause it to read high. The resistor is there to stop the 5v current from running directly to the ground instead of the digital pin.

Servo-Pushbutton 02 (Fritzing)

The code is pretty straight forward and is a combination of the Sweep and Button examples that came with the platform. The pushbutton will be set to pin 10 for input and the LED pin will be pin 9 set for output and the servo will be attached to pin 5. If you aren’t using the Protoshield you can skip the LED or just use the built in LED and 1k Ohm resistor on pin 13 instead. When the button is pressed the LED pin will be set high and using the servo library I’ll be rotating between two positions which through experimentation I’ve determine to be just the right arc to lock or unlock the box.

// @author : Brad Fettes <http://www.fettesps.com>
// @date   : Dec 04, 2010
// @thanks : Sweep by BARRAGAN <http://barraganstudio.com>
//		   : Button by Tom Igoe <http://www.arduino.cc/en/Tutorial/Button>

#include <Servo.h>

int inputPin = 10;      // Push button input pin
int btnVal = 0;         // Current value of pushbutton

int ledPin = 5;			// Pin for Green LED built into proto shield

int outputPin = 9;      // Servo pin
int servo_pos = 0;      // Current servo position
int servo_pos_a = 20;   // Position destination a
int servo_pos_b = 65;   // Position destination b
int servo_state = 0;    // 0 = position a, 1 = position b

Servo mrservo;  // create servo object to control the servo

void setup() {
  pinMode(inputPin, INPUT); // Push Button
  pinMode(ledPin, OUTPUT); // LED

  mrservo.attach(outputPin);  // attaches the servo on pin 9 to the servo object

  // Debug messages via serial monitor
  Serial.begin(9600);
  Serial.println("Starting...");
}

void loop() {
  // Check to see if button is LOW or HIGH (5v)
  btnVal = digitalRead(inputPin);

  if(btnVal == LOW) {
    Serial.println("Button: LOW");
    digitalWrite(ledPin, LOW);

    if(servo_state) {
      Serial.println("Position: a");
    } else {
      Serial.println("Position: b");
    }
  }

  if(btnVal == HIGH) {
    Serial.println("Button: HIGH");
    digitalWrite(ledPin, HIGH);

    if(servo_state) {
      Serial.println("Position: a");
    } else {
      Serial.println("Position: b");
    }

    // Move from position a to position b
    if(servo_state == 0) {
      // I don't know how to concatenate
      Serial.print(servo_pos_a); Serial.print(" to "); Serial.println(servo_pos_b);

      for(servo_pos = servo_pos_a; servo_pos < servo_pos_b; servo_pos += 1) {
        mrservo.write(servo_pos);
        delay(15);                       // waits 15ms for the servo to reach the position
      }
      servo_state = 1;

    } else {  // Move from position b to position a

      Serial.print(servo_pos_b); Serial.print(" to "); Serial.println(servo_pos_a);

      for(servo_pos = servo_pos_b; servo_pos >= servo_pos_a ; servo_pos -= 1) {
        mrservo.write(servo_pos);
        delay(15);
      }
      servo_state = 0;
    }
  }

  // Separate each cycle's output
  Serial.println(); Serial.println();
  delay(250);
}

Here’s a video out the servo with a meccano arm attached rotating between positions A and B:

I sculpted a prototype out of a small box I had lying around. It isn’t perfectly to scale as it’s not nearly as tall as my lockbox but the base dimensions are about the same. After building a few latches out of cardboard and tacks I found a design I liked and made a more permanent latch with some mecanno like materials I had. I affixed it to the front of the box and hot glued a servo in place and wrote some code to try out the lock and see how it works.

I ran into a bit of an issue where when it’s in its reseting state the weight of the latch pulls down on the pivot point causing the longer shaft to lift up over the center line of the pivot point so when it pushes forward again the latch spins the wrong way. In order to prevent this from happen I took another random piece of meccano and glued it under the latch so it had a resting post. After a few tests runs I was satisfied with this solution.

I’m still not sure I’m happy with this latching method, so I think I’ll be testing out some other builds before I choose which method I’ll use in my final build. One idea that was suggested to me, which made me chuckle because it was so simple I couldn’t believe I hadn’t thought of it, is to just attach the latch itself to a servo and mount it higher up, that way you don’t have joints which could fault on you and leave you with a lockbox jammed shut. I’ve been considering this idea as well as doubling up and having two servos locking and unlocking the box, as I don’t want someone to be able to pull it open with brute force. On top of that I’m resisting the urge to take the Dremel to the back casing on the key lock which is built into the cash box so I can see what’s going on in there, and possibly control it with a servo instead of a key. The built in lock is very solid and you’d never be able to pull it open with brute force.

Download the Project Files:

Frizing Project
Arduino Code

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.