Unit 03 Section 02 Lesson 10



Motorized Door Lock

Assigned activities

In this activity you will develop a motorized door locking mechanism and a visual basic interface to control the lock.

Activity 1
Part 1

Required Resources

  • Phidget advanced servo controller
  • 1 Servo motor
  • 3D printed parts:
    • motor holder with rack gear
    • Pinion gear (to fit servo motor)
    • Plastic hinge (2 parts)
    • Plastic hinge pin
    • Lock Latch on second plastic panel
    • Lock mechanism base (to hold lock vertical for display)
  • Hardware kit (screws)

Lock Fabrication

Locate the main motor holder with rack gear assembly. It will have a half hinge attached to the back of the assembly panel.

Locate the servo motor and the pinion gear.

Carefully press the pinion gear onto the motor shaft and secure with the screw. (make sure you remove the screw from the motor first!)

IMPORTANT: Rotate the pinion gear by hand clockwise until it stops. Now mount the motor to the motor holder using 2 servo motor screws as shown below. The screws will cut threads in the plastic so don't over tighten! If you do add 2 more screws.

 Locate the assembly base (used to hold the lock assembly vertical).

Turn the motor holder so that the assembly is vertical with the motor at the bottom. Attach the assembly base. Make sure the long side of the base is as shown.

Next Locate the latch panel (it will have the second part of the hinge). Locate the hinge pin if it is not in the hinge and slide it in the hinge as shown below.

Next attach the latch panel hinge to the motor panel hinge as shown in the next image. Then swing the panel to the closed position so that the rack gear lines wup with the latch

Circuit Configuration

Finally, attach the servo motor wire lead to the advanced servo controller (Port 0) as shown. The black wire should line up on the same side as the B and the yellow wire on the side showing the letter W (some servo motors have a white wire). Attach the 12 volt power supply and the USB cable from your computer.

Create the Graphical User Interface

Note: Save your project at frequent intervals!

Run Visual Basic 6.  Select a Standard Exe.

  • Draw a Frame on the form.
     
  • Draw the following controls on the frame. A suggested layout follows.
    • 2 Command buttons
    • 1 Text box (Place the text box above and between the 2 bottons)

Change the properties for the following:

  • Frame1
    • Caption - Lock Control
    • Font - Arial, 14pt, bold
    • Backcolor - something appropriate
       
  • Text1
    • Text - Delete the default text (Text1)
    • Font - Arial, Bold, 14
       
  • Command 1
    • Caption - LOCK
    • Font - Arial, 12pt, bold
       
  • Command 2
    • Caption - UNLOCK
    • Font - Arial, 12pt, bold

Code the Interface

  • You will be using the Phidget Servo therefore the Phidget library must be referenced. Open the Project menu and select References.

    Scroll down and check Phidget Library 2.1. Click OK.
     
  • Open the code window. Start by selecting General...Declarations in the small windows. Enter the following code:

 Dim WithEvents Servo As PhidgetAdvancedServo

  • Code the Form_Load procedure:

Set Servo = New PhidgetAdvancedServo
Call Servo.Open

  • Select Servo....OnAttach from the small windows at the top of the code form and code the following. (Note: Servo will be in the left window and OnAttach in the right.)
  • Servo.ServoType(0) = PHIDGETCOM_SERVO_HITEC_HS322HD
    Servo.VelocityLimit(0) = 100

  • Make sure you are in the code window. Add a sub procedure by clicking on the Tools menu and selecting Add Procedure. Name the procedure DoorLock.
  • Add a second sub procedure and name it DoorUnLock
  • Code the DoorLock procedure with the following:

Servo.Engaged(0) = True
Servo.Position(0) = 90
Do
Loop Until CInt(Servo.Position(0)) = 90
Text1 = "LOCKED"
Text1.BackColor = vbGreen
Servo.Engaged(0) = False

  • Code the DoorUnLock procedure with the following:

Servo.Engaged(0) = True
Servo.Position(0) = 180
Do
Loop Until CInt(Servo.Position(0)) = 180
Text1 = "UNLOCKED"
Text1.BackColor = vbRed
Servo.Engaged(0) = False

  • Code the Command1 Button (LOCK) with the following:

DoorLock

  • Code the Command2 Button (UNLOCK)with the following:

DoorUnLock

Test the System

Save the project (and form) using a suitable name. Run the program and make sure the latch and the rack gear are lined up. Click the Lock and UnLock buttons to make sure the mechanism works.

Additional Challenges - Adding an RFID Reader

Today, many hotels use programmed cards for keyless entry systems. The following code can be added to your project to simulate this type of doorlock. Make sure you have the Tag Number of at least one of your RFID tag devices. You can use the Phidget Control panel with the RFID reader attached to the computer to see the tag number on a tag device (credit card, keychain, or donut). Make sure you close the Phidget Monitor before running

  • To the General...Declarations code segment add the following (shown in red)

Dim WithEvents Servo As PhidgetAdvancedServo, WithEvents RFID as PhidgetRFID

  • In the Form...Load code segment add the following (again shown in red).

Set Servo = New PhidgetAdvancedServo
Call Servo.Open
Set RFID = New PhidgetRFID
Call RFID.Open

  • Add a Timer to your form. It doesn't matter where as it will be invisible when the application run
  • In the Timer1 properties window make the following changes:
    • Interval 5000
    • Enabled False

Add the following code:

  • From the small windows at the top of the code window, select RFID in the left window and make sure OnAttach shows up in the right window. Add the following code to the RFID OnAttach code segment:

RFID.AntennaOn = True
RFID.LEDOn = True

  • Again select RFID from the small left window and from the right window select OnTag. In the RFID_OnTag code segment add the following code: Note replace the NN with the actual card code of one of the RFID cards, keychains or donuts you will be using. Make sure the actual number is between the quotes. 

If TagNumber = "NN" Then
  DoorUnLock
  Timer1.Enabled
End If

  • Enter the following in the Timer1 code segment:

DoorLock
Timer1.Enabled = False

Save the project. Run the program. Make sure the mechanism is locked.

Slowly pass the RFID device over the RFID reader. The lock should unlock. Make sure the RFID tag is removed from the reader. The door should lock in 5 seconds. You can change this time period by changing the Timer1 Interval. Remember 1000 = 1 second.

 

2016 CDLI