RaspberryPi_Workshop and Programming with python.

gnanithanagula 17 views 45 slides Jun 09, 2024
Slide 1
Slide 1 of 45
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45

About This Presentation

Introduction to Raspberry Pi


Slide Content

Introductionto theRaspberryPi
Jani Kalasniemi

Agenda
• FIRST STEPS
• Whatis a RaspberryPi?
• Setting up your Raspberry Pi
• Using your Raspberry Pi as a Desktop
Computer
• PROGRAMMING WITH PYTHON
• Start Programming in Python
• Write your own Function
• Using GPIO to drive outputs and read inputs
• MAKING A DESKTOP APPLICATION
• Learnaboutevent-driven programming
• Createa simple GraphicalUser Interface(GUI)

FIRST STEPS

•A credit-card-sizedcomputer
•Runs onseveraloperating systems
RaspbianWE WILL USE RASPBIAN
Windows 10 IoTCore
RetroPie
OpenElec
•Possibleto connectwitha variety
ofsensors to interactwiththe
physicalworld.
• Make decisionsbasedonprocessingof
gatheredsensor data
Whatis a RaspberryPi?

GettingstartedwithyourRaspberryPi
Basic 1-2-3
1.Connect theRaspberryPi
to thekeyboard, mouse
and thescreen.
2.Plug in the power and wait
for it to boot.
3.Username: pi
Password: raspberry
(shouldnot be needed)

Using youRaspberryPi as a Desktop Computer
•Try to access the internet through your Raspberry Pi and find a
picture that reflects your group. Hands up when you are done, for
help if needed!
•Save thispictureto thedesktop.

Communicatingthroughtheterminal
•Start the terminal by going to
Accessories and then clicking
on the Terminal program.
•Run throughthecommands
and geta feelfor howto
changedirectories, and list its
contents.

PROGRAMMING WITH PYTHON

Programming in Python3
•General-purpose programming language used for scientific and
numerical applications as well as desktop and web applications.
•Open source language that has a lot of online resources for problems
you might come across.
•We are using ThonnyIDE for programming with Python3 in our
Raspberry Pi 3 Model B
•https://stackoverflow.comis yourbest friendfor programming
troubles

The GPIO
layout
General
Purpose
Input /
Output

UsetheGPIO to drive outputs and readinputs
•Build the circuit in the figure.
Use 470 Ohm resistor
•Next we will make a program
that makes one LED blink on
and off, and the other dim up
and down in idensity when the
Button is pressed.

UsetheGPIO to drive outputs and readinputs

UsetheGPIO to drive outputs and readinputs

Commonerrorsand debugging
•Python is sensetive to intendation.
•Make sure that the variables and the functions have the same exact
writing. Watch outfor smalland Capital letters!
•Rememberthatpythonstarts countingfrom zero.
i.ewhenusingloops.

Test yourprogram and whatyouhave made!
Questions?

Write youownFunctions

Makinga sin(x) functionto getsomeoutput

Shouldreturnsomethinglike this

Using a sensor withtheRaspberryPi
•The GPIO on the Raspberry Pi can only handle a maximum of 3.3V,
but many of the available sensors can return a signal of 5V to the
GPIO. This will damage the Raspberry Pi.
•How do we deal with this?
•Resistors connected as voltage dividers.
•Important to be aware of this.

MeasuringDistancewitha UltrasonicSensor: HC-SR04
•The Trigger sends a burst of
sound that bounces and hits the
Echo.
•If we measure the time it takes
from sending to receiving we can
calculate the distance.
•NOTE: Two different resistors
here

Setting up theRaspberryPi

Nowweneeda functionto calculatethedistance
•With the sensor we can measure the time it takes for a signal to be
sent out and reflected back.
•We need to convert the elapsed time to distance by using the speed
of sound in air and multiplying it with the time divided by two
(Because it travels to the object, and back).

The distancefunction

Makingit loop untilwetell it to stop

If theprogram is not giving anymeasurements
•Check the wiring!
•Check you code one more time.
•Check the resistors
•Check the wiring!

Questions?

MAKING A DESKTOP APPLICATION

IntroducingTkinter: A GUI Toolkit for Python
•De facto standard GUI for Python
•Toolkit for GUI programmingin Python. Not theonlyone, butthemost
commonlyused one.
•Enablesyouto buildGUI’swithbuttons, sliders, dropdowns, and
otherinteractionsfor theuseroftheprogram/deviceyoubuild.

Eventdriven programming.
•Whencreatinga GUI youuseeventdriven programming.
•In theGUI all buttons, sliders, etc, areknownas Widgets.
•Whena widgetis used it make an eventin youcodestart.

•Wewillcreatea GUI
thatenablesusto
turn theLED onand
offwitha buttonin a
GUI.
•Usethe470 Ohm
resistorin thecircuit.
Createa simple GraphicalUser Interface(GUI)

Buildingthecodefor thisGUI

BuildingtheGUI

BuildingtheGUI
•Nowweneeddo decidewhattheGUI willactuallydo.
1.Wewillcreatea buttonthatwillswitchtheLED onand off
2.Wealsohave to make an exit buttonso thatwhenweexit the
program theLED turns off, and theGPIO port is reset to defaultas
input ports.

Creatingthewidgettriggeredevent: ledToggle()

Creatingthewidgettriggeredevent: close()

Cretingthetogglebutton, akaa Widget

Creatingtheexit button

Whathappensifweexit theprogram throughthewindow
exit?
This exit

Makingsure thatwehave a cleanexit

Whathappensifyourun yourprogram and press a GUI
button?

Making the program run after we exit it on purpose
Will thelast line be printed? If so, when?

Test yourprogram and whatyouhave made!
Questions?

Summary–Whathave wedone?
•IntroducedtheRaspberryPi and used it as a computer
•Learned simple commands to use in terminal
•Introduced the GPIO and how to get inputs from the physical world,
interpret them, and create output from input
•Used a XX sensor to read and evaluate data from your environment
•Introduced the Tkinter package and created a simple, event-driven,
GUI

Resources
•RaspberryPi GPIO examples:
https://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/
•Python Documentation:
https://www.python.org/doc/
•Tkinter Documentation:
https://www.tutorialspoint.com/python/python_gui_programming.htm
Tags