Introduction to Raspberry pi
WHAT I S RAS P B ER RY P I
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
RASPBERRY PI BOARD
RASPBERRY PI V/S ARDUINO
Raspberry Pi Arduino
Raspberry Pi is a Single Board Arduino is a Microcontroller based
Computer or SBC development board
It is based on Broadcom SoC, an ARM It is based on Atmel Microcontrollers.
Cortex A Series Microprocessor Arduino UNO uses ATmega328P
A Debian based Linux Distribution Microcontroller
called Raspberry Pi OS is needed to As it is a Microcontroller, there is no
boot the Raspberry Pi need for an operating system
Raspberry Pi SBC can perform multiple Arduino is usually used for running a
tasks simultaneously due to its single task (or a very small no. of
powerful processor and Linux based simple tasks) repeatedly, over and
OS over again
Both the hardware and firmware of Arduino is developed as open-source
Raspberry Pi are closed-source. it is hardware and software from the
not available for general use beginning. You can easily get complete
information on Arduino’s hardware
and software
SET UP YOUR RASPBERRY
PI
SD card (Minimum size 8 Gb, recommended 16 Gb)
HDMI to HDMI / HDMI to VGA Convertor
Keyboard and mouse (USB 2.0)
Ethernet network cable (optional)
Power adapter (Micro USB)
Audio jack 3.5 mm (optional)
BOOTING OF RASPBERRY PI
Download Raspberry Pi Imager from
https://www.raspberrypi.com/software/
Click to
Download
Click on choose OS
Select OS
Choose
Storage
Select
Storage
Click on Write
Click on Write button and wait for complate after that sd
card will ready for boot-up
Put sd card in raspberry pi and power up
it will take some time to first boot.
PROGRAMMING IN RASPBERRY
PI
Python (Primary programming language)
Java
C/C++
HTML5
JavaScript
Scratch (Mathematical and computational
concepts)
JQuery (JavaScript library)
Perl
Erlang
APPLICATION OF RASPBERRY PI
Iridis-Pi
Supercomputer
Developed by Prof Simon Cox
of the University of
Southampton
Calculating Pi was the first test
System has 64 processors and
1TB of memory
Each Raspberry Pi has 16 Gb
of SD card
Jasper
Developed by Charlie
Marsh and Shubhro
Saha
Open-source platform
Control anything with
your voice
Always listening
Robots and Drones
PYTHON PROGRAMMING SETUP
Firstly we have to install python Library
Now open the terminal of raspberry OS then,
1. "sudo apt-get install python3-gpiozero"
enter this text and press enter button
After the installation of first library we can install
second library which is written below
2. "sudo apt-get install python3-rpi.gpio"
enter this text and press enter button
Check install library
After the completion of installation check the library successfully installed or not
for this we need to put these command written below
"python3 -m pip show gpiozero“
After put this command this interface will show
"python3 -m pip show RPi.GPIO“
After put this command this interface will show
ACTIVITY 1
FLASHING LED
Component Require
1. Led light
2. Jumper cable
3. Raspberry pi 3b+
4. Breadboard
Connections
- Connect led Annode to GPIO pin 17 of Raspberry PI
- Connect led Cathod to GPIO pin 06 of Raspberry PI
Open notepad and create a new file.
Copy and Paste the following code to the text file and
save it as ‘led_flashing.py’ and select the all files and
save it.
the path of the file is /home/Adminisrator and paste it
to the folder directly.
CONNECTIONS
Hear is the code of python programming
from gpiozero import LED
from time import sleep
led = LED(17) # Create an LED object on pin 17
try:
while True:
led.on() # Turn the LED on
sleep(0.5) # Wait for 0.5 seconds
led.off() # Turn the LED off
sleep(0.5) # Wait for 0.5 seconds
except KeyboardInterrupt:
# Handle the keyboard interrupt (Ctrl+C)
print("Program interrupted and stopped.")
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
open terminal and write the command'python filename.py' and press ent
See the output of led flashing
ACTIVITY 2
BREATHING LED
Component Require
1. Led light
2. Jumper cable
3. Raspberry pi 3b+
4. Breadboard
Connection
Connect led Annode to GPIO pin 18 of Raspberry PI
Connect led Cathod to GPIO pin 06 of Raspberry PI
Open notepad and create a new file
Copy and Paste the following code to the text file and save it as
led_breathing.py and select the all files and save it.
the path of the file is /home/Adminisrator and paste it to the
folder directly.
CONNECTIONS
Hear is the code of python programming
import RPi.GPIO as GPIO
import time
# Constants
LED_PIN = 18 # GPIO pin connected to the LED
FREQUENCY = 500 # PWM frequency in Hz
PWM_MAX_DUTY = 100 # Maximum PWM duty cycle percentage (0-100)
# Initialize GPIO
GPIO.setmode(GPIO.BCM) # Use Broadcom pin numbering
GPIO.setup(LED_PIN, GPIO.OUT) # Set the LED pin as an output
# Initialize PWM
pwm_led = GPIO.PWM(LED_PIN, FREQUENCY) # Create PWM instance with the
specified frequency
pwm_led.start(0) # Start PWM with a duty cycle of 0%
try:
while True:
# Fade in
for duty_cycle in range(0, PWM_MAX_DUTY + 1, 1): # Increment duty cycle
pwm_led.ChangeDutyCycle(duty_cycle) # Update PWM duty cycle
time.sleep(0.01) # Delay
# Fade out
for duty_cycle in range(PWM_MAX_DUTY, -1, -1): # Decrement duty cycle
pwm_led.ChangeDutyCycle(duty_cycle) # Update PWM duty cycle
time.sleep(0.01) # Delay
except KeyboardInterrupt:
pass # Allow graceful exit with Ctrl+C
finally:
pwm_led.stop() # Stop PWM
GPIO.cleanup() # Clean up GPIO settings
print("Program stopped and GPIO cleaned up.")
open terminal and write the command'python filename.py' and
press enter
See the output of led breathing.
ACTIVITY 3
TRAFFIC SIGNAL USING LED (RED, YELLOW, GREEN)
Component Require
1. Led light - 1 Red, 1 Yellow, 1Green
2. Jumper cable
3. Raspberry pi 3b+
4. Breadboard
Connection
Connect Red led Annode to GPIO pin 16 of Raspberry PI
Connect Yellow led Annode to GPIO pin 17 of Raspberry PI
Connect Green led Annode to GPIO pin 23 of Raspberry PI
Connect all led Cathod as common to GPIO pin 06 of
Raspberry PI
Open notepad and create a new file
Copy and Paste the following code to the text file and save it as
traffic.py and select the all files and save it.
the path of the file is /home/Adminisrator and paste it to the
folder directly.
Connections
Hear is the code of python programming
import RPi.GPIO as GPIO
import time
# Constants for GPIO pins
LED_RED_PIN = 16
LED_YELLOW_PIN = 17
LED_GREEN_PIN = 23
# Initialize GPIO
GPIO.setmode(GPIO.BCM) # Use Broadcom pin numbering
GPIO.setup(LED_RED_PIN, GPIO.OUT) # Set red LED pin as an output
GPIO.setup(LED_YELLOW_PIN, GPIO.OUT) # Set yellow LED pin as an output
GPIO.setup(LED_GREEN_PIN, GPIO.OUT) # Set green LED pin as an output
try:
while True:
# Red LED on for 5 seconds
GPIO.output(LED_RED_PIN, GPIO.HIGH)
time.sleep(5)
GPIO.output(LED_RED_PIN, GPIO.LOW)
# Yellow LED blinking 6 times
for _ in range(6):
GPIO.output(LED_YELLOW_PIN, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(LED_YELLOW_PIN, GPIO.LOW)
time.sleep(0.5)
# Green LED on for 5 seconds
GPIO.output(LED_GREEN_PIN, GPIO.HIGH)
time.sleep(5)
GPIO.output(LED_GREEN_PIN, GPIO.LOW)
except KeyboardInterrupt:
pass # Allow graceful exit with Ctrl+C
finally:
GPIO.cleanup() # Clean up GPIO settings
print("Program stopped and GPIO cleaned up.")
open terminal and write the command'python
filename.py' and press enter
See the output of led as traffic signal.
ACTIVITY 4
Active Buzzer
Component Require
1. Led light
2. Jumper cable
3. Raspberry pi 3b+
4. Breadboard
5. Buzzer
Connection
Connect Buzzer +ve terminal to GPIO pin 16 of Raspberry PI
Connect Buzzer -ve terminal to GPIO pin 6 of Raspberry PI
Connect led Annode to Buzzer +ve terminal
Connect led Cathod to Buzzer -ve terminal
Open notepad and create a new file
Copy and Paste the following code to the text file and save it
as active_buzzer.py and select the all files and save it.
the path of the file is /home/Adminisrator and paste it to the
folder directly.
Hear is the code of python programming
import RPi.GPIO as GPIO
import time
# Set up GPIO using BCM numbering
GPIO.setmode(GPIO.BCM)
# Define the buzzer pin
BUZZER_PIN = 16
# Set up the buzzer pin as an output
GPIO.setup(BUZZER_PIN, GPIO.OUT)
try:
while True:
GPIO.output(BUZZER_PIN, GPIO.HIGH) # Turn buzzer on
time.sleep(0.5) # Sleep for 0.5 seconds
GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn buzzer off
time.sleep(0.5) # Sleep for 0.5 seconds
except KeyboardInterrupt:
# Clean up GPIO settings on exit
GPIO.cleanup()
Open terminal and write the command'python filename.py' and
press enter
see the output of led as blinking and buzzer beeping
ACTIVITY 5
RGB LED
Component Require
1. RGB Led light
2. Jumper cable
3. Raspberry pi 3b+
4. Breadboard
Connection
Condition 1
If RGB have Comman Cathode and 3 Anode
*Connect cathode to pin no 6 of raspberry pi (as ground)
*Connect 3 Anode to pin no 26, 17, 16 of raspberry pi
Respectively
Condition 2
If RGB have Comman Anode and 3 Cathode
*Connect Anode to pin no 2 of raspberry pi (as vcc)
*Connect 3 Cathode to pin no 26, 17, 16 of raspberry pi
Respectively
CONNECTIONS
Open notepad and create a new file
Copy and Paste the following code to the text file and save it
as RGB.py and select the all files and save it.
the path of the file is /home/Adminisrator and paste it to the
folder directly.
Hear is the code of python programming
"import RPi.GPIO as GPIO
from random import randint
import time
# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# GPIO pin assignments
pins = [26, 17, 16]
freq_num = 10
# Set up PWM for each pin
pwm_instances = {}
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
pwm_instances[pin] = GPIO.PWM(pin, freq_num)
pwm_instances[pin].start(0) # Start with duty cycle at 0
# Function to set the color of the RGB LED
def setColor(r, g, b):
# Convert 16-bit values to 8-bit (0-100) for PWM duty cycle
duty_r = (65535 - r) / 655.35 # scaling 16-bit to 8-bit (0-100)
duty_g = (65535 - g) / 655.35
duty_b = (65535 - b) / 655.35
pwm_instances[pins[0]].ChangeDutyCycle(duty_r)
pwm_instances[pins[1]].ChangeDutyCycle(duty_g)
pwm_instances[pins[2]].ChangeDutyCycle(duty_b)
try:
while True:
red = randint(0, 65535)
green = randint(0, 65535)
blue = randint(0, 65535)
setColor(red, green, blue)
time.sleep(0.2) # time.sleep() in seconds
except KeyboardInterrupt:
# Clean up on exit
print("Exiting...")
finally:
# Turn off PWM and cleanup
for pwm in pwm_instances.values():
pwm.stop()
GPIO.cleanup()
Open terminal and write the command'python
filename.py' and press enter
See the output of RGB led as blinking
ACTIVITY 6
IR SENSOR
Component Require
1. Led light
2. Jumper cable
3. Raspberry pi 3b+
4. Breadboard
5. LDR Sensor module
6.Resistor 220 ohm
Connection
Connect Sensor +ve terminal to GPIO pin 2 of Raspberry PI
Connect Sensor -ve terminal to GPIO pin 6 of Raspberry PI
Connect Sensor o/p terminal to GPIO pin 23 of Raspberry PI
Connect led Annode to GPIO pin 26 of Raspberry PI
Connect led Cathod to -ve terminal
Open notepad and create a new file
Copy and Paste the following code to the text file and save it as
IR.py and select the all files and save it.
the path of the file is /home/Administrator and paste it to the
folder directly.
Hear is the code of python programming
import RPi.GPIO as GPIO
import time
# Setup
IR_SENSOR_PIN = 23 # GPIO pin connected to the IR sensor output
LED_PIN = 26 # GPIO pin connected to the LED
GPIO.setmode(GPIO.BCM) # Use Broadcom SOC numbering
GPIO.setup(IR_SENSOR_PIN, GPIO.IN) # Set IR sensor pin as input
GPIO.setup(LED_PIN, GPIO.OUT) # Set LED pin as output
try:
while True:
if GPIO.input(IR_SENSOR_PIN):
GPIO.output(LED_PIN, GPIO.LOW)
else:
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(0.1) # Small delay to avoid excessive CPU usage
except KeyboardInterrupt:
print("Program terminated by user")
finally:
GPIO.cleanup() # Clean up GPIO settings before exiting