Nothing Special   »   [go: up one dir, main page]

IoT GTU Study Material Presentations Unit-6 21052022055855PM

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

IOT and Applications

GTU # 3160716

Unit-6 
Arduino and Raspberry Pi

Prof. Kalpesh H Surati


Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
Kalpesh.surati@darshan.ac.in
+91 99250 10033
Raspberry Pi
What is Raspberry Pi? Raspberry Pi is a single-board computers,
developed by Raspberry Pi Foundation in
association with Broadcom and perhaps
the most inspiring computer available
today.
Because of its low cost and open design,
the model became far more popular than
anticipated.
It is widely used to make gaming devices,
f it ness gadgets, weather stations, and
much more.
I n 2012, t h e c o m pa ny l a u nc h e d t h e
Raspberry Pi and the current generations of
regular Raspberry Pi boards are Zero, 1, 2, 3,
and 4.

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 2


Architecture, Layout and Interface of Raspberry Pi
Processor
HDMI Port
USB Port
Ethernet Port
SD Card Slot
Camera Connector
Composite Video
Output
Audio Output
Micro USB Power
GPIO Pins
Status LEDs

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 3


Operating System for Raspberry Pi
Raspberry Pi needs an operating system to work.
Raspberry Pi OS (previously called Raspbian) is an of fic ial supported operating
system.
Raspberry Pi Imager is the quick and easy way to install Raspberry Pi OS and other
operating systems to a microSD card, ready to use with your Raspberry Pi.
Download and install Raspberry Pi Imager to a computer with an SD card reader.
Put the SD card you'll use with your Raspberry Pi into the reader and run Raspberry
Pi Imager.

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 4


NOOBS
NOOBS (New Out Of the Box Software) is an exclusive install manager for OS
installation process in the SD card of Raspberry Pi.
NOOBS is extremely useful for beginners as it helps in easy OS installation without
configuration process.
Different operating system that could be installed using NOOBS are.
• Raspbian
• OpenELEC
• Pidora
• RaspBMC
• RISC OS
• Arch Linux ARM

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 5


GPIO and the 40-pin Header
A powerful feature of the Raspberry Pi is the row of GPIO (general-purpose
input/output) pins along the top edge of the board.
A 40-pin GPIO header is found on all current Raspberry Pi boards (unpopulated on
Raspberry Pi Zero, Raspberry Pi Zero W and Raspberry Pi Zero 2 W).
Prior to the Raspberry Pi 1 Model B+ (2014), boards comprised a shorter 26-pin
header.
O n e o f t h e t h i n gs t h a t m a ke s t h e
Raspberry Pi better for learning
electronics than most other computers
is its ability to control the voltage on
several of its easily accessible pins.
GIOP header contains outputs for 3.3V,
5V, Ground, and lots of General Purpose
Input/Output (GPIO) pins!

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 6


Programming
Python is a powerful programming language that’s easy to use easy to read and
write and, with Raspberry Pi
Python syntax is clean, with an emphasis on readability, and uses standard
English keywords.
To control hardware connected to the Raspberry Pi we will use Python.
One should be familiar with some of the basics of Python, including literals,
variables, operators, control flow, scope, and data structures.
We'll use the RPi.GPIO module to control all the GPIO of Raspberry Pi

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 7


Python Programming for GPIO
Import the Rpi.GPIO module
import RPi.GPIO as GPIO
Pin Numbering Declaration
After you've included the RPi.GPIO module, the next step is to determine which of the two
pin-numbering schemes you want to use:
 GPIO.BOARD -- Board numbering scheme. The pin numbers follow the pin numbers on header P1.
 GPIO.BCM -- Broadcom chip-specif ic pin numbers. These pin numbers follow the lower-level
numbering system defined by the Raspberry Pi's Broadcom-chip brain.
To spec ify in your c od e whic h number- system is being used , use the
GPIO.setmode() function.
GPIO.setmode(GPIO.BCM)
Both the import and setmode lines of code are required, if you want to use
Python.

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 8


Python Programming for GPIO
Setting a Pin Mode
Similler to declare a "pin mode" in Arduino before you can use it as either an input or output.
To set a pin mode, use the setup([pin], [GPIO.IN, GPIO.OUT] function.
So, if you want to set pin 18 as an output,
GPIO.setup(18, GPIO.OUT)
Outputs
Digital Output
To write a pin high or low, use the GPIO.output([pin], [GPIO.LOW, GPIO.HIGH]) function.
For example, if you want to set pin 18 high, write:
GPIO.output(18, GPIO.HIGH)
Writing a pin to GPIO.HIGH will drive it to 3.3V, and GPIO.LOW will set it to 0V.
Alternative to GPIO.HIGH and GPIO.LOW, you can use either 1, True, 0 or False to set a pin
value.

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 9


Python Programming for GPIO
Outputs
PWM ("Analog") Output
PWM on the Raspberry Pi is about as limited as can be -- one, single pin is capable of it: 18
(i.e. board pin 12).
To initialize PWM, use GPIO.PWM([pin], [frequency]) function.
To make the rest of your script-writing easier you can assign that instance to a variable.
Then use pwm.start([duty cycle]) function to set an initial value.
For example...
pwm = GPIO.PWM(18, 1000)
pwm.start(50)

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 10


Python Programming for GPIO
Inputs
If a pin is configured as an input, you can use the GPIO.input([pin]) function to read its value.
The input() function will return either a True or False indicating whether the pin is HIGH or
LOW.
You can use an if statement to test this, will read pin 17 and print whether it's being read as
HIGH or LOW
if GPIO.input(17):
print("Pin 11 is HIGH")
else:
print("Pin 11 is LOW")

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 11


Python Code for Blinking LED
Blink.py
1 import time
2 import RPi.GPIO as GPIO
3 led_pin = 12 # Pin definitions
4
5 GPIO.setmode(GPIO.BCM) # Use "GPIO" pin numbering
6
7 GPIO.setup(led_pin, GPIO.OUT) # Set LED pin as output
8
9 while True: # Blink forever
10 GPIO.output(led_pin, GPIO.HIGH) # Turn LED on
11 time.sleep(1) # Delay for 1 second
12 GPIO.output(led_pin, GPIO.LOW) # Turn LED off
13 time.sleep(1) # Delay for 1 second
14

Prof. Kalpesh H Surati #3160716 (IoT) Unit 6 – Arduino and Raspberry Pi 12

You might also like