In this step you'll determine the pulse width values for the locked and unlocked servo positions of the box. Make sure the hardware is assembled, the software dependencies are installed, and power is applied to both the Raspberry Pi and servo before you continue. Connect to the Raspberry Pi in a terminal session and type the following to run an interactive python shell as root:
At the python shell type in the following to load the RPIO library and its servo control class (note you don't need to type the >>>, it's just a reference for what you should see):
>>> from RPIO import PWM
>>> servo = PWM.Servo()
Now you can call the set_servo function to send pulse width values to the servo which will move it to different positions. The pulse width value you send can range from 1000 to 2000 microseconds, with a value of 1500 being the center position of the servo. If you're curious, you can find more information on servos in this tutorial.
Make sure the box is open so you can see the servo and it's free to move, then execute this command to move the servo to the center position (pulse width of 1500 microseconds):
>>> servo.set_servo(18, 1500)
Note that if your servo is connected to a different GPIO port of the Raspberry Pi, change the first parameter from 18 to the appropriate value.
Try sending different pulse width values between 1000 and 2000 to find the values which move the latch to the locked and unlocked position. Don't forget you can adjust the position of the servo horn and latch on the servo if necessary. You can see the values I found for my hardware below:
Latch in the unlocked position with a servo pulse width of 2000 micro seconds.
Latch in the locked position with a servo pulse width of 1100 micro seconds.
In the directory with code for this project, open the file config.py in a text editor. This file defines the configuration for the project and might require a few changes for your hardware. Specifically you can adjust the following values:
- LOCK_SERVO_PIN - this should be the GPIO number of the Raspberry Pi which is connected to the signal line of the lock servo. This project is built to use GPIO 18 by default.
- LOCK_SERVO_UNLOCKED - this should be the pulse width value (in microseconds) for the unlocked position of the latch. Use the value you determined in the previous step.
- LOCK_SERVO_LOCKED - this should be the pulse width value for the locked position of the latch.
- BUTTON_PIN - this should be the GPIO number which is connected to the push button on the box. This project is built to use GPIO 25 by default.
Continue on to run the box software and finish the project.