SlideShare a Scribd company logo
5
Most read
8
Most read
9
Most read
RASPBERRY PI – USING PYTHON
2ND JUNE 2013
www.sf-innovations.co.uk
IDLE3 – Python Shell
Launch IDLE3 from the Raspberry GUI. This will allow us to experiment with
Python commands.
At the >>> prompt type
print („hello world‟)
You will see “hello world” on the screen.
At the >>> prompt type
20 + 5
You will see “25” on the screen.
This is a way of executing Python commands immediately. A programme is
simply a collection of commands executed consecutively.
www.sf-innovations.co.uk
Writing a Python program
Under the file tab in IDLE3, click on new window. You are now ready to
write a program. For now type the following exactly.
For x in range (1,10);
y=x * x
print “x=“, x, “square of x=“, y
Under the file tab, click on save and call the file “squarex”.
Launch LXTerminal from the Raspberry Pi GUI. This will bring you back to
the Linux command line prompt: pi@raspberrypi - $
Type “sudo python squarex.py” to run your program.
You will see
X = 1 square of x = 1
X = 2 square of x = 4
...
X = 9 square of x = 81
That‟s it. You‟ve written your first Python program.
www.sf-innovations.co.uk
Some notes on Python
You 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 the
same program using the command line prompt, it worked fine.
Sudo – stands for “super user do”. With Linux, this gives you the right
privileges to run a Python program.
Comments - If you want to add comments to your program, then use # at
the start. For example, you could have started the program on the previous
page with
# program to work out squares of numbers from 1 to 9.
Using libraries – Many standard functions are available as libraries in
Python. These can be used by using the “import” command and will save
you a lot of time in programming.
For example “import time” will bring in a library which can be used for time
delays. “import random” will bring in a random number generator.
www.sf-innovations.co.uk
Turning an led on and off
We are going to use the GPIO port on the Raspberry Pi for this. To make the
connection easier, the Custard Pi 1 breakout board is used. This plugs
straight into the GPIO connector, provides easy screw terminal connection
and protects the Raspberry Pi from accidental damage.
The Raspberry Pi is mounted on the Custard Pi B prototyping base.
www.sf-innovations.co.uk
Hardware connections
We are using pin 11 of the GPIO port. This is available on connector J2 of the
Custard 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 power
connector J3.
J3
J2
www.sf-innovations.co.uk
Connecting the led
When 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 of
the 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 important
to limit the current. This is done by using a 330 ohm resistor, in series
with the led.
From pin 11
From 0V
330 ohm resistor
Long leg of led
www.sf-innovations.co.uk
Python program to flash led
Type in the program steps below. The text behind the # explains what the
code does, but you do not have to enter this.
Import RPi.GPIO as GPIO # import GPIO library
Import time #import time library
GPIO.setmode(GPIO.BOARD) #use board pin numbers
GPIO.setup(11, GPIO.OUT) #setup pin 11 as output
For x in range (0,10): #repeat for x=0 to 9
GPIO.output(11, True) #set pin 11 high
time.sleep(0.2) #wait 0.2 seconds
GPIO.output(11, False) #set pin 11 low
time.sleep(0.2) #wait 0.2 seconds
GPIO.cleanup() #tidy up GPIO port
Import sys #exit program
Sys.exit()
Save the file as “ledonoff.py”.
www.sf-innovations.co.uk
Download code
Trying out the program
Open LXTerminal and type “sudo python ledonoff.py” 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. When
you 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 last
command 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 5
times.
Well done. You have just managed to write some Python code to flash an
led.
www.sf-innovations.co.uk
Reading a switch
We are going to wire a switch to pin 12 of the GPIO port only flash the led
when this is pressed.
....... (same first 3 lines as before)
GPIO.setup(11, GPIO.OUT) #setup pin 11 as output
GPIO.setup(12, GPIO.IN, pull_up_dpwn=GPIO.PUD_UP) #pull up resistor
While True: #repeat forever
input1=GPIO.input(12) #read status of pin 12 into “input1”
if input1==False: #is pin 12 false (low)
print “button pressed” #then button is pressed
For x in range (0,10): #repeat for x=0 to 9
GPIO.output(11, True) #set pin 11 high
time.sleep(0.2) #wait 0.2 seconds
GPIO.output(11, False) #set pin 11 low
time.sleep(0.2) #wait 0.2 seconds
Note: The indentation is important in Python.
www.sf-innovations.co.uk
Download code
Hardware setup
The picture below shows the switch set-up. One side of it is connected to pin
12 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 0V
Pin 12
www.sf-innovations.co.uk
Trying out the program
Save the file as “ledonoffsw.py” and then run from LXTerminal by typing
“sudo python ledonoffsw.py 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 terminate
when this second switch is pressed instead of having to use CTRL & C.
www.sf-innovations.co.uk
Summary
Hope this presentation has been useful in getting started with Python on
the Raspberry Pi. The book “Programming the Raspberry Pi” by Simon
Monk is a useful introduction to Python.
Keep an eye on our website www.sf-innovations.co.uk for any updates to
this presentation, new Custard Pi layers or new presentations.
www.sf-innovations.co.uk

More Related Content

PPTX
Addressing sequencing
PDF
IoT Networking
PPTX
microcontroller vs microprocessor
PPTX
Internet of Things Using Arduino
PPTX
halfadder & halfsubtractor using 4:1 MUX
PPTX
ARM Processors
PPTX
Internet of things using Raspberry Pi
DOCX
Embedded System
Addressing sequencing
IoT Networking
microcontroller vs microprocessor
Internet of Things Using Arduino
halfadder & halfsubtractor using 4:1 MUX
ARM Processors
Internet of things using Raspberry Pi
Embedded System

What's hot (20)

PPT
IoT with Arduino
PDF
Introduction to Microcontroller
PPT
Internet of Things and its applications
PPTX
Assembly Language
PPTX
Raspberry Pi
PPTX
Calm technology
PDF
Bit pair recoding
PDF
Verilog full adder in dataflow & gate level modelling style.
PPTX
PPTX
Arduino and its hw architecture
PPTX
Computer organization
PPTX
Instruction Set Architecture
PPTX
Chapter 4 Embedded System: Application and Domain Specific
PPTX
Introduction to 8085 microprocessor
PDF
Assembler directives and basic steps ALP of 8086
PPTX
Internet of Things (IoT) - Seminar ppt
PPTX
Internet-of-things- (IOT) - a-seminar - ppt - by- mohan-kumar-g
PPTX
Introduction to IOT
PPT
Microprocessor ppt
PPT
ARM Architecture
IoT with Arduino
Introduction to Microcontroller
Internet of Things and its applications
Assembly Language
Raspberry Pi
Calm technology
Bit pair recoding
Verilog full adder in dataflow & gate level modelling style.
Arduino and its hw architecture
Computer organization
Instruction Set Architecture
Chapter 4 Embedded System: Application and Domain Specific
Introduction to 8085 microprocessor
Assembler directives and basic steps ALP of 8086
Internet of Things (IoT) - Seminar ppt
Internet-of-things- (IOT) - a-seminar - ppt - by- mohan-kumar-g
Introduction to IOT
Microprocessor ppt
ARM Architecture
Ad

Viewers also liked (14)

PPT
Raspberrypi best ppt
PPTX
10 Great Tips for Business Owners
PPTX
Custard pi 7 user information
PPTX
Raspberry Pi Base - A flexible support frame for Raspberry Pi projects
PPTX
MemoryPAT
PPTX
溫溼度數據統計
PPTX
Python and the internet of things
PPTX
Basic Electronics - Ohm's Law
PPTX
Basic Electronics - Resistors
PPTX
Python in raspberry pi
PDF
MicroPython簡介
PDF
Gettiing Started with IoT using Raspberry Pi and Python
PPT
Raspberry pi : an introduction
PPT
Raspberry pi
Raspberrypi best ppt
10 Great Tips for Business Owners
Custard pi 7 user information
Raspberry Pi Base - A flexible support frame for Raspberry Pi projects
MemoryPAT
溫溼度數據統計
Python and the internet of things
Basic Electronics - Ohm's Law
Basic Electronics - Resistors
Python in raspberry pi
MicroPython簡介
Gettiing Started with IoT using Raspberry Pi and Python
Raspberry pi : an introduction
Raspberry pi
Ad

Similar to Raspberry Pi Using Python (20)

PPTX
Raspberry pi led blink
PPTX
[5]投影片 futurewad樹莓派研習會 141218
PDF
4. GPIO Access
PPTX
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
PDF
Getting Started With Raspberry Pi - UCSD 2013
PPT
RaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMING
PDF
DeviceHub - First steps using Intel Edison
PPTX
Python-in-Embedded-systems.pptx
PDF
Hands on Raspberry Pi - Creative Technologists
PDF
Getting Started with Raspberry Pi - USC 2013
PPTX
Introduction to pcDuino
PDF
Raspberry Pi 4.pdf
PDF
manual_2020_Cyber Physical System.pdf
PDF
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
PPTX
ScratchGPIO, Raspberry Pi & BerryClip
PPTX
pcDuino Presentation at SparkFun
ODP
OpenGurukul : Language : Python
ODP
Introduction to Raspberry Pi and GPIO
PPTX
Introduction to Python Programming – Part I.pptx
PDF
Raspberry pi pico projects raspberry pi projects
Raspberry pi led blink
[5]投影片 futurewad樹莓派研習會 141218
4. GPIO Access
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
Getting Started With Raspberry Pi - UCSD 2013
RaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMING
DeviceHub - First steps using Intel Edison
Python-in-Embedded-systems.pptx
Hands on Raspberry Pi - Creative Technologists
Getting Started with Raspberry Pi - USC 2013
Introduction to pcDuino
Raspberry Pi 4.pdf
manual_2020_Cyber Physical System.pdf
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
ScratchGPIO, Raspberry Pi & BerryClip
pcDuino Presentation at SparkFun
OpenGurukul : Language : Python
Introduction to Raspberry Pi and GPIO
Introduction to Python Programming – Part I.pptx
Raspberry pi pico projects raspberry pi projects

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Encapsulation theory and applications.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
KodekX | Application Modernization Development
PDF
cuic standard and advanced reporting.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
A Presentation on Artificial Intelligence
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Encapsulation theory and applications.pdf
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Building Integrated photovoltaic BIPV_UPV.pdf
KodekX | Application Modernization Development
cuic standard and advanced reporting.pdf
The AUB Centre for AI in Media Proposal.docx
A Presentation on Artificial Intelligence
Reach Out and Touch Someone: Haptics and Empathic Computing
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Empathic Computing: Creating Shared Understanding
NewMind AI Monthly Chronicles - July 2025
Network Security Unit 5.pdf for BCA BBA.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Raspberry Pi Using Python

  • 1. RASPBERRY PI – USING PYTHON 2ND JUNE 2013 www.sf-innovations.co.uk
  • 2. IDLE3 – Python Shell Launch IDLE3 from the Raspberry GUI. This will allow us to experiment with Python commands. At the >>> prompt type print („hello world‟) You will see “hello world” on the screen. At the >>> prompt type 20 + 5 You will see “25” on the screen. This is a way of executing Python commands immediately. A programme is simply a collection of commands executed consecutively. www.sf-innovations.co.uk
  • 3. Writing a Python program Under the file tab in IDLE3, click on new window. You are now ready to write a program. For now type the following exactly. For x in range (1,10); y=x * x print “x=“, x, “square of x=“, y Under the file tab, click on save and call the file “squarex”. Launch LXTerminal from the Raspberry Pi GUI. This will bring you back to the Linux command line prompt: pi@raspberrypi - $ Type “sudo python squarex.py” to run your program. You will see X = 1 square of x = 1 X = 2 square of x = 4 ... X = 9 square of x = 81 That‟s it. You‟ve written your first Python program. www.sf-innovations.co.uk
  • 4. Some notes on Python You 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 the same program using the command line prompt, it worked fine. Sudo – stands for “super user do”. With Linux, this gives you the right privileges to run a Python program. Comments - If you want to add comments to your program, then use # at the start. For example, you could have started the program on the previous page with # program to work out squares of numbers from 1 to 9. Using libraries – Many standard functions are available as libraries in Python. These can be used by using the “import” command and will save you a lot of time in programming. For example “import time” will bring in a library which can be used for time delays. “import random” will bring in a random number generator. www.sf-innovations.co.uk
  • 5. Turning an led on and off We are going to use the GPIO port on the Raspberry Pi for this. To make the connection easier, the Custard Pi 1 breakout board is used. This plugs straight into the GPIO connector, provides easy screw terminal connection and protects the Raspberry Pi from accidental damage. The Raspberry Pi is mounted on the Custard Pi B prototyping base. www.sf-innovations.co.uk
  • 6. Hardware connections We are using pin 11 of the GPIO port. This is available on connector J2 of the Custard 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 power connector J3. J3 J2 www.sf-innovations.co.uk
  • 7. Connecting the led When 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 of the 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 important to limit the current. This is done by using a 330 ohm resistor, in series with the led. From pin 11 From 0V 330 ohm resistor Long leg of led www.sf-innovations.co.uk
  • 8. Python program to flash led Type in the program steps below. The text behind the # explains what the code does, but you do not have to enter this. Import RPi.GPIO as GPIO # import GPIO library Import time #import time library GPIO.setmode(GPIO.BOARD) #use board pin numbers GPIO.setup(11, GPIO.OUT) #setup pin 11 as output For x in range (0,10): #repeat for x=0 to 9 GPIO.output(11, True) #set pin 11 high time.sleep(0.2) #wait 0.2 seconds GPIO.output(11, False) #set pin 11 low time.sleep(0.2) #wait 0.2 seconds GPIO.cleanup() #tidy up GPIO port Import sys #exit program Sys.exit() Save the file as “ledonoff.py”. www.sf-innovations.co.uk Download code
  • 9. Trying out the program Open LXTerminal and type “sudo python ledonoff.py” 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. When you 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 last command 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 5 times. Well done. You have just managed to write some Python code to flash an led. www.sf-innovations.co.uk
  • 10. Reading a switch We are going to wire a switch to pin 12 of the GPIO port only flash the led when this is pressed. ....... (same first 3 lines as before) GPIO.setup(11, GPIO.OUT) #setup pin 11 as output GPIO.setup(12, GPIO.IN, pull_up_dpwn=GPIO.PUD_UP) #pull up resistor While True: #repeat forever input1=GPIO.input(12) #read status of pin 12 into “input1” if input1==False: #is pin 12 false (low) print “button pressed” #then button is pressed For x in range (0,10): #repeat for x=0 to 9 GPIO.output(11, True) #set pin 11 high time.sleep(0.2) #wait 0.2 seconds GPIO.output(11, False) #set pin 11 low time.sleep(0.2) #wait 0.2 seconds Note: The indentation is important in Python. www.sf-innovations.co.uk Download code
  • 11. Hardware setup The picture below shows the switch set-up. One side of it is connected to pin 12 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 0V Pin 12 www.sf-innovations.co.uk
  • 12. Trying out the program Save the file as “ledonoffsw.py” and then run from LXTerminal by typing “sudo python ledonoffsw.py 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 terminate when this second switch is pressed instead of having to use CTRL & C. www.sf-innovations.co.uk
  • 13. Summary Hope this presentation has been useful in getting started with Python on the Raspberry Pi. The book “Programming the Raspberry Pi” by Simon Monk is a useful introduction to Python. Keep an eye on our website www.sf-innovations.co.uk for any updates to this presentation, new Custard Pi layers or new presentations. www.sf-innovations.co.uk