introduction · web viewpython is a widely used general-purpose, high-level programming language...

13
TRAFFIC LIGHT KIT INSTRUCTION MANUAL

Upload: trinhkiet

Post on 29-Jun-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

TRAFFIC LIGHT KITINSTRUCTION MANUAL

www.PiShop.co.za

Page 2: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

INTRODUCTION

Welcome to the Official PiShop Pre-Built Traffic Light Kit Instruction manual, thank-you for the purchase, the purpose of this document is to provide you with the detail needed in order to get your new Traffic Light Kit up and running.

HARDWARE PREREQUISITES

You will need the following items not included with this kit:

Raspberry Pi Model B+ (this kit also works with other models but the documentation is based on the B+)

A Micro SD Card USB Keyboard USB Mouse Screen A working internet connection (for required software downloads) HDMI or 3.5mm Jack RCA Cable

SOFTWARE PREREQUISITES

You will need the following software components in order to follow the steps detailed in this manual:

RASPBIAN

Raspbian is a free operating system based on Debian Linux optimized for the Raspberry Pi hardware. An operating system is the set of basic programs and utilities that make your Raspberry Pi run. Raspbian is also the most popular of the Operating Systems available for the Raspberry Pi and therefore is the most widely used for tutorials.

IF YOU HAVE A BLANK MICRO SD CARD

DOWNLOAD NOOBS

Using a computer with an SD card reader, or a USB card Reader, visit the Raspberry Pi Foundation Downloads page at http://www.raspberrypi.org/downloads/.

Click on the Download ZIP button under ‘NOOBS (offline and network install)’, and select a folder to save it to.

Once the download completes; extract the files from the zip.

FORMAT YOUR SD CARD

Visit the SD Association’s website and download SD Formatter 4.0 for either Windows or Mac. (https://www.sdcard.org/downloads/formatter_4/)

Follow the instructions to install the software. Insert your SD card into the computer or laptop’s SD card reader and make a note of the drive letter

allocated to it, e.g. G:/ In SD Formatter, select the drive letter for your SD card and format it.

COPY NOOBS FILES

Once your SD card has been formatted, drag all the files in the extracted NOOBS folder and drop them onto the SD card drive.

Page 2 of 10

Page 3: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

The necessary files will then be transferred to your SD card. When this process has finished, safely remove the SD card and insert it into your Raspberry Pi.

IF YOU HAVE AN SD CARD PRE-LOADED WITH NOOBS:

Plug in your keyboard, mouse, monitor cables and SD Card. Now plug in the USB power cable to your Pi. Your Raspberry Pi will boot, and a window will appear with a list of different operating systems that

you can install. For this kit we require that you use Raspbian – tick the box next to Raspbian and click on Install.

Raspbian will then run through its installation process. Note this can take a while. When the install process has completed, the Raspberry Pi configuration menu (raspi-config) will load.

Here you are able to set the time and date for your region. You can exit this menu by using Tab on your keyboard to move to Finish.

IF YOU ALREADY HAVE RASPBIAN INSTALLED:

You will see a black screen with a login prompt The default login for Raspbian is username pi with the password raspberry. Note you will not see any

writing appear when you type the password. This is a security feature in Linux. Login and execute the following commands, waiting for them to finish before moving onto the next.

o sudo apt-get updateo sudo apt-get upgradeo sudo apt-get dist-upgrade

Load the graphical user interface by executing startx

Figure 1: Raspbian Desktop

PYTHON

Page 3 of 10

Page 4: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

Python is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad adoption.

Raspbian comes with Python pre-installed out of the box, so we need not perform any special actions in order to make use of this software.

GEANY

Geany is an IDE (Integrated Development Environment) using the GTK2 toolkit and it’s the tool we’ll be using to edit and execute our python script which will be powering the kit.

Open a new terminal (Appendix A) and type the following command: sudo apt-get install geanythis will connect to the internet in order to download and install Geany, if prompted as to whether you want to install the software; select “Y”. Wait for the process to reach 100%

Figure 2: Geany Installation

CONNECTING THE KIT

Before we run the code we have to connect the kit to the GPIO on the Pi. The pins are numbered from 1 to 40. Pin 1 will be the pin on the side to the inside of the board on the left, have a look at the specifications of your specific model that will tell you which wire should be connected to which pin.

Page 4 of 10

Page 5: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

GETTING THE CODE

DOWNLOAD THE CODE FROM PISHOP

FROM YOUR RASPBERY PI

Open a new Terminal (Appendix A) and type the following command:wget http://www.pishop.co.za/kits/trafficlight/code/TrafficLightV1.0.pyThis will download the code directly from the PiShop website and save it as TrafficLightV1.0.py in your /home/pi directory.

TYPE THE CODE OUT

As the title suggests you can type out the following code section directly into Geany;

# Traffic Light Kit V1.0# PiShop# http://www.pishop.co.za/kits/trafficlight/code/TrafficLightV1.0.py

# Hit F5 in Geany to run

import sysimport RPi.GPIO as GPIO # import the GPIO libraryimport time # import the time library

# print - will send a message to the terminal

Page 5 of 10

Page 6: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

try:GPIO.setmode(GPIO.BOARD) # set GPIO to use pins by number on

boardGPIO.setwarnings(False)

except Exception as e: # catch errorprint ("An Error occurred during setting the GPIO mode")# print where error occurredprint e # print error

try:GPIO.setup (11, GPIO.OUT) # set GPIO pin11 as output (Red)GPIO.setup (13, GPIO.OUT) # set GPIO pin13 as output (Yellow)GPIO.setup (15, GPIO.OUT) # set GPIO pin15 as output (Green)

except Exception as e: # catch errorprint ("An Error occurred pin assignment") # print where error

occurredprint e # print error

answ = "0" # define varible and set value to 0answ = raw_input("r0,r1,y0,y1,g0,g1 or x(exit) ") # require input from user and put answer in variable

while answ != "x": # check input from user for exit command

if answ == "r1": # if input was r1 then:GPIO.output(11,True) # set pin 11 to on(high)

if answ == "r0": # if input was r0 then:GPIO.output(11,False) # set pin 11 to off(low)

if answ == "y1":GPIO.output(13,True)

if answ == "y0":GPIO.output(13,False)

if answ == "g1":GPIO.output(15,True)

if answ == "g0":GPIO.output(15,False)

answ = raw_input("r0,r1,y0,y1,g0,g1 or x(exit) ")#request user input again and loop back to while statement# until input = x for exit

EXECUTING THE CODE

Geany can be opened either via the GUI as seen below, or via the terminal, but because our code makes use of the GPIO functionality of the Raspberry Pi we need super user privileges; it’s better to open Geany via the Terminal.

1. Open a new terminal window2. Execute: sudo geany

Page 6 of 10

Page 7: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

Figure 3: Geany Running

Once Geany is running, from the File menu, select Open and navigate to /home/pi, then select the TrafficLightV1.0.py file we downloaded earlier.

Hit the F5 key on your keyboard to execute the code. (You should see an image very similar to the one below)

Figure 4: Running Code

Page 7 of 10

Page 8: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

Now it’s your turn! Play with the options and change the code to suit your needs.

PISHOP CODE INTRODUCTION

The PiShop code in V1.0 provides the following features:

Command Descriptionx Exits the programr1 Switches on the red LEDr0 Switches off the red LEDy1 Switches on the yellow LEDy0 Switches off the yellow LEDg1 Switches on the green LEDg0 Switches off the green LED

FURTHER READING AND SPECIAL THANKS

This instruction manual, while written and developed by PiShop would not have been possible without the information provided by the following sources:

The Raspberry Pi Foundation:http://www.raspberrypi.org/

The SD Association:http://www.sdcard.org/

Raspbianhttp://www.raspbian.org/

Wikimedia Foundation (Wikipedia)http://www.wikipedia.org

Some very useful linux basics that will help you with Raspbian:http://www.raspberrypi.org/documentation/linux/usage/commands.md

Take some time now to try out some of the other code versions available for this kit:

http://www.pishop.co.za/kits/trafficlight/code/TrafficLightV2.0.py http://www.pishop.co.za/kits/trafficlight/code/TrafficLightV3.0.py http://www.pishop.co.za/kits/trafficlight/code/TrafficLightV4.0.py

CONCLUSION

At this time you should feel very comfortable with your new Traffic Light Kit and the Raspberry Pi. If you have any questions or comments we would be very happy if you got in touch with us on [email protected]

Page 8 of 10

Page 9: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

APPENDIX A: WORKING WITH THE TERMINAL

The Linux terminal acts as the medium for input and output operations for a Linux system. A Linux terminal is similar to command line in Microsoft Windows but it differs in that it can perform any operation on the system.

OPENING A NEW TERMINAL

To open a new terminal simply double click on the LXTerminal icon in the top right of your desktop.

Figure 5: Raspbian Desktop (showing LXTerminal)

NAVIGATING IN THE TERMINAL

Some very useful terminal basics can be found at: http://www.raspberrypi.org/documentation/usage/terminal/

Page 9 of 10

Page 10: Introduction · Web viewPython is a widely used general-purpose, high-level programming language which focuses on code readability and simplistic syntax to allow ease of use broad

OFFICIAL PISHOP TRAFFIC LIGHT KIT INSTRUCTION MANUALWWW.PISHOP.CO.ZA

Figure 6: LXTerminal Running

CLOSING THE TERMINAL

There are two ways in which you can exit the terminal. Either you can execute the command exit or click the X in the top right corner of the window.

If you wish to close a program but not exit the terminal, hit ctrl c on your keyboard.

Page 10 of 10