What is a Raspberry Pi?
The Raspberry Pi is a low cost, credit-card sized computer that plugs into a
computer monitor or TV, and uses a standard keyboard and mouse.
It is a capable little device that enables people of all ages to explore computing, and
to learn how to program in languages like Scratch and Python.
It’s capable of doing everything you’d expect a desktop computer to do, from
browsing the internet and playing high-definition video, to making spreadsheets,
word-processing, and playing games.
provides a set of GPIO (general purpose input/output) pins that allow you to control
electronic components for physical computing and explore the Internet of Things
(IoT).
Raspberry Pi GPIO Header
Raspberry pi 3 Model B
PIN GROUP PIN NAME DESCRIPTION
POWER SOURCE +5V, +3.3V, GND and Vin +5V -power output
+3.3V -power output
GND – GROUND pin
COMMUNICATION UART(Universal Asynchronous
UART Interface(RXD, TXD) [(GPIO15,GPIO14)]
INTERFACE Receiver Transmitter)used for
interfacing sensors and other devices.
SPI Interface(MOSI, MISO, CLK,CE) x SPI (Serial Peripheral Interface) used
2 for communicating with other boards
[SPI0-(GPIO10 ,GPIO9, GPIO11 ,GPIO8)] or peripherals.
[SPI1--(GPIO20 ,GPIO19, GPIO21 ,GPIO7)]
TWI Interface(SDA, SCL) x 2 [(GPIO2, TWI (Two Wire Interface) Interface
GPIO3)] can be used to connect peripherals.
[(ID_SD,ID_SC)]
INPUT OUTPUT PINS 26 I/O Although these some pins have
multiple functionsthey can be
considered as I/O pins.
PWM Hardware PWM available on GPIO12, GPIO13, These 4 channels can provide PWM
GPIO18, GPIO19 (Pulse Width Modulation) outputs.
*Software PWM available on all pins
EXTERNAL All I/O In the board all I/O pins can be used
INTERRUPTS as Interrupts.
Raspberry Pi 3 Model B Features
CPU: Raspberry Pi 3 uses Broadcom BCM2837 SOC 64-bit quad-core ARM Cortex A53
(ARMv8 CPU) with 512KB shared L2 cache.
Memory: Provided with 1 GB of RAM
Wi-Fi Support: 802.11n Wireless LAN
Bluetooth: Supports Bluetooth 4.1 Bluetooth Low Energy (BLE)
USB Ports: 4-USB ports which allow attaching four different USB devices like
keyboard, mouse, etc.
Ethernet Port: Standard Ethernet port to quickly setup and access internet. This can be
very useful when we want to setup raspberry pi for the first time without a monitor.
GPIO Pins: Raspberry Pi 3 supports 40 GPIO Pins General Purpose Input Output. These
digital input/output pins can be used to drive LED, Switches, and Sensors etc.
Full HDMI Port: Support HDMI port (High-Definition Multimedia Interface) which can
be used to quickly connect raspberry pi to HDMI Monitor. With HDMI Cable and
Monitor we can add Screen to Raspberry Pi.
Micro SD card slot: The Micro SD Card will hold the operating system which will boot
while we power on Raspberry Pi 3. In next tutorial, we will learn how to setup and
prepare SD card with Raspbian OS.
Audio/Video: Combined 3.5mm audio jack and composite video
Display interface (DSI): enable us to interface Display Module
Camera interface (CSI): enable us to interface Camera Module
Graphics Support: VideoCore IV 3D graphics core for advance graphics capabilities .
Raspberry pi 3 installation steps
A word about NOOBS. It's worth noting that the method described here isn't
your only option for installing Raspbian
Step 1: Download Raspbian
Step 2: Unzip the file.
Step 3: Write the disc image to your microSD card.
Step 4: Put the microSD card in your Pi and boot up.
Differences between the models
Raspberry Pi Raspberry Pi Model B+ Model A+ Model A CMDK
3 Model B 2 Model B
Processor Broadcom Broadcom Broadcom Broadcom Broadcom Broadcom
Chipset BCM2837 BCM2836 BCM2835 BCM2835 BCM2835 BCM2835
64Bit ARMv7 32bit ARMv7 32bit ARMv6 32bit ARMv6 32bit ARMv6 32bit ARMv6
Quad Core Quad Core SoC full HD SoC full HD SoC full HD SoC full HD
Processor Processor multimedia multimedia multimedia multimedia
powered Single powered applications applications applications applications
Board Single Board processor processor processor processor
Computer Computer
running at running at
1250MHz 900MHz
GPU Videocore IV Videocore IV Videocore IV Videocore IV Videocore IV Videocore IV
Processor QUAD Core QUAD Core Single Core Single Core Single Core Single Core
Speed @1250 MHz @1250 MHz @700 MHz @700 MHz @700 MHz @700 MHz
RAM 1GB SDRAM 1GB SDRAM 512 MB 256 MB 256 MB 512 MB
@ 400 MHz @ 400 MHz SDRAM @ SDRAM @ SDRAM @ SDRAM @
400 MHz 400 MHz 400 MHz 400 MHz
Raspberry Raspberry Model B+ Model A+ Model A CMDK
Pi 3 Model B Pi 2 Model B
Storage MicroSD MicroSD MicroSD MicroSD SDCard 4GB eMMC
USB 2.0 4x USB Ports 4x USB Ports 4x USB Ports 1x USB Port 1x USB Port 1x USB Port
Power Draw / 40 pin 40 pin 40 pin 40 pin 26 pin 120 pin
voltage
GPIO Yes Yes Yes No No No
Ethernet Port Yes Yes Yes No No No
Wi-Fi Built in No No No No No
Bluetooth LE Built in No No No No No
Led on /off
import time
import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(11, GPIO.OUT) ## Setup GPIO Pin 11 to OUT
while True:
GPIO.output(11,True) ## Turn on Led
time.sleep(1) ## Wait for one second
GPIO.output(11,False) ## Turn off Led
time.sleep(1) ## Wait for one second
Push Button
Push button with led
import RPi.GPIO as IO #we are calling header file which helps us to use GPIO’s of PI
import time # we are calling for time to provide delays in program
IO.setwarnings(False) #do not show any warnings
IO.setmode (IO.BCM) #we are programming the GPIO by BCM pin numbers. (PIN39 as
‘GPIO19’)
IO.setup(19,IO.OUT) # initialize GPIO19 as an output.
IO.setup(26,IO.IN) #initialize GPIO26 as input
while 1: #execute loop forever
if(IO.input(26) == False): #if GPIO26 goes low execute the below statements
IO.output(19,True) # turn the LED on (making the voltage level HIGH)
time.sleep(0.11) #sleep for 100m second
IO.output(19,False) # turn the LED off (making GPIO19 low)
time.sleep(1) #sleep for 100m second
Interface IR sensor
import RPi.GPIO as GPIO
import time
sensor = 16
led= 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor,GPIO.IN)
GPIO.setup(led,GPIO.OUT)
while True:
if GPIO.input(sensor):
GPIO.output(led,True)
print "Object Detected”
else:
GPIO.output(led,False)
Interface
import RPi.GPIO as GPIO
import time
sensor = 39
led= 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor,GPIO.IN)
GPIO.setup(led,GPIO.OUT)
while True:
if GPIO.input(sensor):
GPIO.output(led,True)
print "Object Detected”
else:
GPIO.output(led,False)
Mcp3008 ic
Interface mq6 sensor
import RPi.GPIO as GPIO
import time # change these as desired - they're thepins connected from the# SPI
#port on the ADC to the Cobbler
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8
mq6_dpin = 20
mq6_apin = 0
#port init
def init():
GPIO.setwarnings(False)
GPIO.cleanup() #clean up at the end of your script
GPIO.setmode(GPIO.BCM) #to specify whilch pin numbering system #
set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
GPIO.setup(mq2_dpin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
#read SPI data from MCP3008(or MCP3204) chip,8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
GPIO.output(cspin, True)
adcout >>= 1 # first bit is 'null' so drop it
return adcout
#main ioop
def main():
init()
print("please wait...")
time.sleep(20)
while True:
COlevel=readadc(mq2_apin, SPICLK, SPIMOSI, SPIMISO, SPICS)
if GPIO.input(mq2_dpin):
print("Gas not leak")
time.sleep(0.5)
else:
print("Gas leakage")
print("Current Gas AD vaule = " +str
("%.2f"%((COlevel/1024.)*3.3))+" V")
time.sleep(0.5)
main()
GPIO.cleanup()