Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Raspberry Pi Using Python


Published on

This presentation takes the viewer step by step to controlling an led and reading a switch using Python.

Published in: Technology
  • @TimteoMendes
    Hi Timóteo, Thanks for this useful feedback.
    I have now amended the PowerPoint to include downloads of the actual code, so this tyoe of error should be eliminated.
    Also see here for more downloads.
    Are you sure you want to  Yes  No
    Your message goes here
  • Hello,
    I really like your work is well done and nice explain.
    But you have a problem that cost me about 4h of developing because you use capital letters in the sart of the prog that i was not expecting to be a problem.
    When load it in python always receave a syntax error.
    Trie to upgrade gpio as all the sistem and the problem remains.
    Please change your code in the presentation so no one have the same problem as me.
    Are you sure you want to  Yes  No
    Your message goes here

Raspberry Pi Using Python

  2. 2. IDLE3 – Python ShellLaunch IDLE3 from the Raspberry GUI. This will allow us to experiment withPython commands.At the >>> prompt typeprint („hello world‟)You will see “hello world” on the screen.At the >>> prompt type20 + 5You will see “25” on the screen.This is a way of executing Python commands immediately. A programme issimply a collection of commands executed
  3. 3. Writing a Python programUnder the file tab in IDLE3, click on new window. You are now ready towrite a program. For now type the following exactly.For x in range (1,10);y=x * xprint “x=“, x, “square of x=“, yUnder the file tab, click on save and call the file “squarex”.Launch LXTerminal from the Raspberry Pi GUI. This will bring you back tothe Linux command line prompt: pi@raspberrypi - $Type “sudo python” to run your program.You will seeX = 1 square of x = 1X = 2 square of x = 4...X = 9 square of x = 81That‟s it. You‟ve written your first Python
  4. 4. Some notes on PythonYou can run programs using the “run module” option under the run tab.However when I tried this I got programming syntax errors. When I ran thesame program using the command line prompt, it worked fine.Sudo – stands for “super user do”. With Linux, this gives you the rightprivileges to run a Python program.Comments - If you want to add comments to your program, then use # atthe start. For example, you could have started the program on the previouspage with# program to work out squares of numbers from 1 to 9.Using libraries – Many standard functions are available as libraries inPython. These can be used by using the “import” command and will saveyou a lot of time in programming.For example “import time” will bring in a library which can be used for timedelays. “import random” will bring in a random number
  5. 5. Turning an led on and offWe are going to use the GPIO port on the Raspberry Pi for this. To make theconnection easier, the Custard Pi 1 breakout board is used. This plugsstraight into the GPIO connector, provides easy screw terminal connectionand protects the Raspberry Pi from accidental damage.The Raspberry Pi is mounted on the Custard Pi B prototyping
  6. 6. Hardware connectionsWe are using pin 11 of the GPIO port. This is available on connector J2 of theCustard Pi 1 and is labelled as pin 11. This is shown on the image below.The 0V (or Gnd) connection is the centre pin of the 3 pin powerconnector
  7. 7. Connecting the ledWhen pin 11 is True (taken high) the voltage on it will be almost 3.3V.This needs to go to the positive side of the led. This is the longer leg ofthe led. The other side of the led goes to 0V (Gnd).Note: If you connect 3.3V across an led it will burn out. So it is importantto limit the current. This is done by using a 330 ohm resistor, in serieswith the led.From pin 11From 0V330 ohm resistorLong leg of
  8. 8. Python program to flash ledType in the program steps below. The text behind the # explains what thecode does, but you do not have to enter this.Import RPi.GPIO as GPIO # import GPIO libraryImport time #import time libraryGPIO.setmode(GPIO.BOARD) #use board pin numbersGPIO.setup(11, GPIO.OUT) #setup pin 11 as outputFor x in range (0,10): #repeat for x=0 to 9GPIO.output(11, True) #set pin 11 hightime.sleep(0.2) #wait 0.2 secondsGPIO.output(11, False) #set pin 11 lowtime.sleep(0.2) #wait 0.2 secondsGPIO.cleanup() #tidy up GPIO portImport sys #exit programSys.exit()Save the file as “” code
  9. 9. Trying out the programOpen LXTerminal and type “sudo python” to run the program.The led should flash ten time at a fairly fast rate.Try changing the time.sleep line from 0.2 seconds to 0.5 seconds. Whenyou run the program, it should flash ten times, but fairly slowly this time.Tip: To rerun the program, press the upwards arrow to re-enter the lastcommand on the screen and then press return to run it.Now change the x in range command from 0,10 to 0,5 to flash the led just 5times.Well done. You have just managed to write some Python code to flash
  10. 10. Reading a switchWe are going to wire a switch to pin 12 of the GPIO port only flash the ledwhen this is pressed........ (same first 3 lines as before)GPIO.setup(11, GPIO.OUT) #setup pin 11 as outputGPIO.setup(12, GPIO.IN, pull_up_dpwn=GPIO.PUD_UP) #pull up resistorWhile True: #repeat foreverinput1=GPIO.input(12) #read status of pin 12 into “input1”if input1==False: #is pin 12 false (low)print “button pressed” #then button is pressedFor x in range (0,10): #repeat for x=0 to 9GPIO.output(11, True) #set pin 11 hightime.sleep(0.2) #wait 0.2 secondsGPIO.output(11, False) #set pin 11 lowtime.sleep(0.2) #wait 0.2 secondsNote: The indentation is important in code
  11. 11. Hardware setupThe picture below shows the switch set-up. One side of it is connected to pin12 of the Custard Pi boards. The other side is connected to the 0V (GND)connection.The pull up option used when setting up pin 12 as an input keeps this high,unless pulled low by the external switch.TO 0VPin
  12. 12. Trying out the programSave the file as “” and then run from LXTerminal by typing“sudo python at the command line prompt.Whenever the switch is pressed, the led should flash 10 times.To come out of this program loop, press CTRL & C at the same time.See if you can add another switch and modify the program to terminatewhen this second switch is pressed instead of having to use CTRL &
  13. 13. SummaryHope this presentation has been useful in getting started with Python onthe Raspberry Pi. The book “Programming the Raspberry Pi” by SimonMonk is a useful introduction to Python.Keep an eye on our website for any updates tothis presentation, new Custard Pi layers or new