SlideShare a Scribd company logo
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
Most read
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
Most read
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
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

What's hot (20)

Wi-Fi Esp8266 nodemcu
Wi-Fi Esp8266 nodemcu Wi-Fi Esp8266 nodemcu
Wi-Fi Esp8266 nodemcu
creatjet3d labs
 
Simple Presentation On Raspberry pi
Simple Presentation On Raspberry piSimple Presentation On Raspberry pi
Simple Presentation On Raspberry pi
Sakkar Chowdhury
 
Li fi technology ppt
Li fi technology pptLi fi technology ppt
Li fi technology ppt
Rohith Palakurthi
 
1. Introduction to Embedded Systems & IoT
1. Introduction to Embedded Systems & IoT1. Introduction to Embedded Systems & IoT
1. Introduction to Embedded Systems & IoT
IEEE MIU SB
 
Arduino
ArduinoArduino
Arduino
vipin7vj
 
Intro to Arduino
Intro to ArduinoIntro to Arduino
Intro to Arduino
avikdhupar
 
Internet of Things (IOT)
Internet of Things (IOT)Internet of Things (IOT)
Internet of Things (IOT)
Kunal Adhikari
 
Raspberry Pi ppt.pptx
Raspberry Pi ppt.pptxRaspberry Pi ppt.pptx
Raspberry Pi ppt.pptx
ushabharathisb1
 
Sensors in IOT
Sensors in IOTSensors in IOT
Sensors in IOT
ATS SBGI MIRAJ
 
Internet of things using Raspberry Pi
Internet of things using Raspberry PiInternet of things using Raspberry Pi
Internet of things using Raspberry Pi
Yash Gajera
 
IOT and Characteristics of IOT
IOT and  Characteristics of IOTIOT and  Characteristics of IOT
IOT and Characteristics of IOT
AmberSinghal1
 
IoT
IoTIoT
IoT
Ananth Kumar
 
AVR Fundamentals
AVR FundamentalsAVR Fundamentals
AVR Fundamentals
Vinit Vyas
 
8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller ppt
Rahul Kumar
 
Microprocessor
MicroprocessorMicroprocessor
Microprocessor
aaina_katyal
 
IoT sensor devices
IoT sensor devicesIoT sensor devices
IoT sensor devices
Roman Staszewski
 
ARDUINO AND ITS PIN CONFIGURATION
 ARDUINO AND ITS PIN  CONFIGURATION ARDUINO AND ITS PIN  CONFIGURATION
ARDUINO AND ITS PIN CONFIGURATION
soma saikiran
 
CONTROLLING HOME APPLIANCES WITH IOT,BLYNK APP & NODE MCU
CONTROLLING HOME APPLIANCES WITH IOT,BLYNK APP & NODE MCUCONTROLLING HOME APPLIANCES WITH IOT,BLYNK APP & NODE MCU
CONTROLLING HOME APPLIANCES WITH IOT,BLYNK APP & NODE MCU
VINAY KUMAR GUDELA
 
Embedded c
Embedded cEmbedded c
Embedded c
Ami Prakash
 
embedded system and iot.pptx
embedded system and iot.pptxembedded system and iot.pptx
embedded system and iot.pptx
SanjanaN25
 
Simple Presentation On Raspberry pi
Simple Presentation On Raspberry piSimple Presentation On Raspberry pi
Simple Presentation On Raspberry pi
Sakkar Chowdhury
 
1. Introduction to Embedded Systems & IoT
1. Introduction to Embedded Systems & IoT1. Introduction to Embedded Systems & IoT
1. Introduction to Embedded Systems & IoT
IEEE MIU SB
 
Intro to Arduino
Intro to ArduinoIntro to Arduino
Intro to Arduino
avikdhupar
 
Internet of Things (IOT)
Internet of Things (IOT)Internet of Things (IOT)
Internet of Things (IOT)
Kunal Adhikari
 
Internet of things using Raspberry Pi
Internet of things using Raspberry PiInternet of things using Raspberry Pi
Internet of things using Raspberry Pi
Yash Gajera
 
IOT and Characteristics of IOT
IOT and  Characteristics of IOTIOT and  Characteristics of IOT
IOT and Characteristics of IOT
AmberSinghal1
 
AVR Fundamentals
AVR FundamentalsAVR Fundamentals
AVR Fundamentals
Vinit Vyas
 
8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller ppt
Rahul Kumar
 
ARDUINO AND ITS PIN CONFIGURATION
 ARDUINO AND ITS PIN  CONFIGURATION ARDUINO AND ITS PIN  CONFIGURATION
ARDUINO AND ITS PIN CONFIGURATION
soma saikiran
 
CONTROLLING HOME APPLIANCES WITH IOT,BLYNK APP & NODE MCU
CONTROLLING HOME APPLIANCES WITH IOT,BLYNK APP & NODE MCUCONTROLLING HOME APPLIANCES WITH IOT,BLYNK APP & NODE MCU
CONTROLLING HOME APPLIANCES WITH IOT,BLYNK APP & NODE MCU
VINAY KUMAR GUDELA
 
embedded system and iot.pptx
embedded system and iot.pptxembedded system and iot.pptx
embedded system and iot.pptx
SanjanaN25
 

Viewers also liked (14)

Raspberrypi best ppt
Raspberrypi best ppt Raspberrypi best ppt
Raspberrypi best ppt
SOMRAJ GAUTAM
 
10 Great Tips for Business Owners
10 Great Tips for Business Owners10 Great Tips for Business Owners
10 Great Tips for Business Owners
Seggy Segaran
 
Custard pi 7 user information
Custard pi 7 user informationCustard pi 7 user information
Custard pi 7 user information
Seggy Segaran
 
Raspberry Pi Base - A flexible support frame for Raspberry Pi projects
Raspberry Pi Base - A flexible support frame for Raspberry Pi projectsRaspberry Pi Base - A flexible support frame for Raspberry Pi projects
Raspberry Pi Base - A flexible support frame for Raspberry Pi projects
Seggy Segaran
 
MemoryPAT
MemoryPATMemoryPAT
MemoryPAT
Seggy Segaran
 
溫溼度數據統計
溫溼度數據統計溫溼度數據統計
溫溼度數據統計
裕凱 夏
 
Python and the internet of things
Python and the internet of thingsPython and the internet of things
Python and the internet of things
Adam Englander
 
Basic Electronics - Ohm's Law
Basic Electronics - Ohm's LawBasic Electronics - Ohm's Law
Basic Electronics - Ohm's Law
Seggy Segaran
 
Basic Electronics - Resistors
Basic Electronics - ResistorsBasic Electronics - Resistors
Basic Electronics - Resistors
Seggy Segaran
 
Python in raspberry pi
Python in raspberry piPython in raspberry pi
Python in raspberry pi
Sudar Muthu
 
MicroPython簡介
MicroPython簡介 MicroPython簡介
MicroPython簡介
Max Lai
 
Gettiing Started with IoT using Raspberry Pi and Python
Gettiing Started with IoT using Raspberry Pi and PythonGettiing Started with IoT using Raspberry Pi and Python
Gettiing Started with IoT using Raspberry Pi and Python
Martin Christen
 
Raspberry pi : an introduction
Raspberry pi : an introductionRaspberry pi : an introduction
Raspberry pi : an introduction
LTG Oxford
 
Raspberry pi
Raspberry pi Raspberry pi
Raspberry pi
Anija Nair
 
Raspberrypi best ppt
Raspberrypi best ppt Raspberrypi best ppt
Raspberrypi best ppt
SOMRAJ GAUTAM
 
10 Great Tips for Business Owners
10 Great Tips for Business Owners10 Great Tips for Business Owners
10 Great Tips for Business Owners
Seggy Segaran
 
Custard pi 7 user information
Custard pi 7 user informationCustard pi 7 user information
Custard pi 7 user information
Seggy Segaran
 
Raspberry Pi Base - A flexible support frame for Raspberry Pi projects
Raspberry Pi Base - A flexible support frame for Raspberry Pi projectsRaspberry Pi Base - A flexible support frame for Raspberry Pi projects
Raspberry Pi Base - A flexible support frame for Raspberry Pi projects
Seggy Segaran
 
溫溼度數據統計
溫溼度數據統計溫溼度數據統計
溫溼度數據統計
裕凱 夏
 
Python and the internet of things
Python and the internet of thingsPython and the internet of things
Python and the internet of things
Adam Englander
 
Basic Electronics - Ohm's Law
Basic Electronics - Ohm's LawBasic Electronics - Ohm's Law
Basic Electronics - Ohm's Law
Seggy Segaran
 
Basic Electronics - Resistors
Basic Electronics - ResistorsBasic Electronics - Resistors
Basic Electronics - Resistors
Seggy Segaran
 
Python in raspberry pi
Python in raspberry piPython in raspberry pi
Python in raspberry pi
Sudar Muthu
 
MicroPython簡介
MicroPython簡介 MicroPython簡介
MicroPython簡介
Max Lai
 
Gettiing Started with IoT using Raspberry Pi and Python
Gettiing Started with IoT using Raspberry Pi and PythonGettiing Started with IoT using Raspberry Pi and Python
Gettiing Started with IoT using Raspberry Pi and Python
Martin Christen
 
Raspberry pi : an introduction
Raspberry pi : an introductionRaspberry pi : an introduction
Raspberry pi : an introduction
LTG Oxford
 
Ad

Similar to Raspberry Pi Using Python (20)

RaspberryPi_Workshop and Programming with python.
RaspberryPi_Workshop and Programming with python.RaspberryPi_Workshop and Programming with python.
RaspberryPi_Workshop and Programming with python.
gnanithanagula
 
Raspberry Pi Introductory Lecture
Raspberry Pi Introductory LectureRaspberry Pi Introductory Lecture
Raspberry Pi Introductory Lecture
Syed Umaid Ahmed
 
Raspberry pi
Raspberry piRaspberry pi
Raspberry pi
Pravesh Sahu
 
manual_2020_Cyber Physical System.pdf
manual_2020_Cyber Physical System.pdfmanual_2020_Cyber Physical System.pdf
manual_2020_Cyber Physical System.pdf
ssuserc5ee4c
 
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
divijareddy0502
 
Python and the Raspberry Pi
Python and the Raspberry PiPython and the Raspberry Pi
Python and the Raspberry Pi
Rachel Wang
 
Not so hard hardware
Not so hard hardwareNot so hard hardware
Not so hard hardware
richardgault
 
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
Raspberry Pi GPIO Tutorial - Make Your Own Game ConsoleRaspberry Pi GPIO Tutorial - Make Your Own Game Console
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
RICELEEIO
 
LED Blinking Using Raspberry Pi
LED Blinking Using Raspberry PiLED Blinking Using Raspberry Pi
LED Blinking Using Raspberry Pi
Arjun R Krishna
 
4. GPIO Access
4. GPIO Access4. GPIO Access
4. GPIO Access
Mayank Joneja
 
ScratchGPIO, Raspberry Pi & BerryClip
ScratchGPIO, Raspberry Pi & BerryClipScratchGPIO, Raspberry Pi & BerryClip
ScratchGPIO, Raspberry Pi & BerryClip
David Dryden
 
RaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMING
RaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMINGRaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMING
RaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMING
Asif Iqbal
 
Intro to the raspberry pi board
Intro to the raspberry pi boardIntro to the raspberry pi board
Intro to the raspberry pi board
Thierry Gayet
 
Raspberry Pi ppt.pptx
Raspberry Pi ppt.pptxRaspberry Pi ppt.pptx
Raspberry Pi ppt.pptx
ushabharathisb1
 
Raspberry Pi 4.pdf
Raspberry Pi 4.pdfRaspberry Pi 4.pdf
Raspberry Pi 4.pdf
Engineering Funda
 
Hands on Raspberry Pi - Creative Technologists
Hands on Raspberry Pi - Creative TechnologistsHands on Raspberry Pi - Creative Technologists
Hands on Raspberry Pi - Creative Technologists
bennuttall
 
Shine a little LED
Shine a little LEDShine a little LED
Shine a little LED
geekinlibrariansclothing
 
Embedded Systems: Lecture 9: The Pi Control ARM
Embedded Systems: Lecture 9: The Pi Control ARMEmbedded Systems: Lecture 9: The Pi Control ARM
Embedded Systems: Lecture 9: The Pi Control ARM
Ahmed El-Arabawy
 
Python-in-Embedded-systems.pptx
Python-in-Embedded-systems.pptxPython-in-Embedded-systems.pptx
Python-in-Embedded-systems.pptx
TuynLCh
 
Getting Started with Raspberry Pi
Getting Started with Raspberry PiGetting Started with Raspberry Pi
Getting Started with Raspberry Pi
yeokm1
 
RaspberryPi_Workshop and Programming with python.
RaspberryPi_Workshop and Programming with python.RaspberryPi_Workshop and Programming with python.
RaspberryPi_Workshop and Programming with python.
gnanithanagula
 
Raspberry Pi Introductory Lecture
Raspberry Pi Introductory LectureRaspberry Pi Introductory Lecture
Raspberry Pi Introductory Lecture
Syed Umaid Ahmed
 
manual_2020_Cyber Physical System.pdf
manual_2020_Cyber Physical System.pdfmanual_2020_Cyber Physical System.pdf
manual_2020_Cyber Physical System.pdf
ssuserc5ee4c
 
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
divijareddy0502
 
Python and the Raspberry Pi
Python and the Raspberry PiPython and the Raspberry Pi
Python and the Raspberry Pi
Rachel Wang
 
Not so hard hardware
Not so hard hardwareNot so hard hardware
Not so hard hardware
richardgault
 
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
Raspberry Pi GPIO Tutorial - Make Your Own Game ConsoleRaspberry Pi GPIO Tutorial - Make Your Own Game Console
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
RICELEEIO
 
LED Blinking Using Raspberry Pi
LED Blinking Using Raspberry PiLED Blinking Using Raspberry Pi
LED Blinking Using Raspberry Pi
Arjun R Krishna
 
ScratchGPIO, Raspberry Pi & BerryClip
ScratchGPIO, Raspberry Pi & BerryClipScratchGPIO, Raspberry Pi & BerryClip
ScratchGPIO, Raspberry Pi & BerryClip
David Dryden
 
RaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMING
RaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMINGRaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMING
RaspberryPI PPT WITH ALL THE DETAILS OF PROGRAMMING
Asif Iqbal
 
Intro to the raspberry pi board
Intro to the raspberry pi boardIntro to the raspberry pi board
Intro to the raspberry pi board
Thierry Gayet
 
Hands on Raspberry Pi - Creative Technologists
Hands on Raspberry Pi - Creative TechnologistsHands on Raspberry Pi - Creative Technologists
Hands on Raspberry Pi - Creative Technologists
bennuttall
 
Embedded Systems: Lecture 9: The Pi Control ARM
Embedded Systems: Lecture 9: The Pi Control ARMEmbedded Systems: Lecture 9: The Pi Control ARM
Embedded Systems: Lecture 9: The Pi Control ARM
Ahmed El-Arabawy
 
Python-in-Embedded-systems.pptx
Python-in-Embedded-systems.pptxPython-in-Embedded-systems.pptx
Python-in-Embedded-systems.pptx
TuynLCh
 
Getting Started with Raspberry Pi
Getting Started with Raspberry PiGetting Started with Raspberry Pi
Getting Started with Raspberry Pi
yeokm1
 
Ad

Recently uploaded (20)

Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 

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