Know where you're going?

1: Control the Servos
2: Wiring Servos
3: Run the program in startup
Navigate

Crack your fingers 'cause its programming time.

Step 1: Control the Servos

After booting up your Raspberry Pi, open the Terminal. This should be located on the desktop. In it, type

sudo idle3

This will open a program for editing and writing code. Click File -- New. In your new document, paste this code. Comments preceded by a # are unnecessary.

DOWNLOAD
#!/usr/bin/python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(12,GPIO.OUT)
GPIO.setup(7,GPIO.OUT)
y = 1
z = 1
x = 1
a = 1
b = 1
c = 1

while True:
        for x in range(100):                #dimmer-switch to speed setting
                GPIO.output(7,1)
                time.sleep(0.001)
                GPIO.output(7,0)
                time.sleep(0.001)
        time.sleep(1)

        try:
                 print("First throw, 1/4")  
                 time.sleep(1)
                 for z in range (200) :     #Releases the first disc
                        GPIO.output(12,1)
                        time.sleep(0.0005)
                        GPIO.output(12,0)
                        time.sleep(0.0005)
                 time.sleep(.1)
                 for y in range (200) :     #Resets the servo
                        GPIO.output(12,1)
                        time.sleep(0.0015)
                        GPIO.output(12,0)
                        time.sleep(0.0015)

                 print("Second throw 1/4")
                 time.sleep(3)
                 for z in range (200) :     #Releases the second disc
                        GPIO.output(12,1)
                        time.sleep(0.0005)
                        GPIO.output(12,0)
                        time.sleep(0.0005)
                 time.sleep(.1)
                 for y in range (200) :     #Resets the servo
                        GPIO.output(12,15)
                        time.sleep(0.0015)
                        GPIO.output(12,0)
                        time.sleep(0.0015)
                 x = 1
                 for x in range(200):       #drill to speed setting
                        GPIO.output(7,1)
                        time.sleep(0.001)
                        GPIO.output(7,0)
                        time.sleep(0.001)

                 print("Third throw, 3/4")
                 time.sleep(3)
                 for z in range (200) :     #Releases the third disc
                        GPIO.output(12,1)
                        time.sleep(0.0005)
                        GPIO.output(12,0)
                        time.sleep(0.0005)
                 time.sleep(.1)
                 for y in range (200) :     #Resets the servo
                        GPIO.output(12,15)
                        time.sleep(0.0015)
                        GPIO.output(12,0)
                        time.sleep(0.0015)

                 print("Fourth throw 3/4")
                 time.sleep(3)              
                 for z in range (200) :     #Releases the fourth disc
                        GPIO.output(12,1)
                        time.sleep(0.0005)
                        GPIO.output(12,0)
                        time.sleep(0.0005)
                 time.sleep(.1)
                 for y in range (200) :     #Resets the servo
                        GPIO.output(12,15)
                        time.sleep(0.0015)
                        GPIO.output(12,0)
                        time.sleep(0.0015)
                 print("Reload")
                 time.sleep(30)             #Wait time between sequences
                                            #This can be changed freely
        except KeyboardInterrupt:
                GPIO.cleanup()
                

Preform Ctrl + S on your keyboard to save this code. Use whatever name you want, but you will need it again later. Naming it “servo” would seem to be the easiest name.

Step 2: Wiring Servos

(You will need one power cord for this step. This can be from any device.) You will also need to know the numbering system for the GPIO pins. These are shown by the following diagram, with the left and front sides being the edges of the Pi.

First, the dimmer-switch servo. Connect all three of the wires coming from the servo to jumper wires. Connect each jumper wire to these GPIO pins on the Raspberry Pi. They are labeled based on the corresponding servo wire.

White - 7; Red (control) - 4; Black (ground) - 14

The colors of the wires on your servo may not be exactly the same. In this case, base your connections off the functions of the wires shown above.

Next, the feed servo. First, strip about ⅜ inch of insulation off of the power supply wire. If there is a connector, such as a USB, this will need to be cut off as well. This can be done by simply taking a pair of scissors and cutting the USB part of the power supply off In doing this you will find the two wires, usually one is black and one is red you only need the black one. Now, connect each of the servo wires to a jumper wire. Connect the power wires on the power supply to jumper wires as well. If there are three wires, you will not need the third as it is for data. Make the following connections, again labeled as the corresponding servo and power supply wires.

Brown (ground) - 25; Orange (power) - Red (On power supply); Yellow (control) - 12; Black (On power supply, ground) - 6

Step 3: Run the program on startup

This step will involve numerous commands in the Terminal that you may not understand. I will try to explain what the commands do, but just keep following this tutorial, and it should work in the end.

First enter in the Terminal:

cd bbt

Terminal operates in a preset directory, and this command changes the directory to the folder “bbt.” Next, type

nano launcher.sh

This will create a file in the bbt directory called “launcher.sh.” This program will eventually do what it says: launch the servo program. Type this code:

cd /
cd home/pi/bbt
sudo python nameOfYourProgramFromStep1.py
cd /

Preform Crtl + X -> Shift + Y -> Enter, to save the code.

Now to make your script executable, type in this command:

chmod 755 launcher.sh

Now, you will need a log to store any outputs or errors in. Navigate back to your default directory using

cd

In the terminal. Create a log file using

mkdir logs

Now for actually making the script run on startup. Type in the Terminal

sudo crontab -e

This will open the crontab file. Anywhere in this file, enter (all one line)

@reboot sh /home/pi/bbt/launcher.sh >/home/pi/logs/cronlog 2>&1

This will tell the Raspberry Pi to run your program on startup. Do Ctrl + X, Enter to save.

To test the full program, now that you have the code written, servos connected, and have set the program to run on startup, enter into the Terminal

sudo roboot

Wait for the Raspberry Pi to restart, and your program should run automatically! If for some reason it doesn’t work, type the following commands into the Terminal:

cd logs

cat cronlog

In this file it should say “First throw, ¼. Second throw ¼…” etc. If this is the case, recheck your wiring, as this is probably the issue. If there is an error displayed in the log, check that you did step 1 correctly.

Congratulations! You have now completed the “indoor” part of construction!

To Construction

About | Credits - Home Page Image by Joshua Earle, Looking Man Mountains, 3/20/15, Creative Commons Attribution
- Materials Page Image by Caleb George Morris, Blur Chilling City 1698, 3/20/15, Creative Commons Attribution