Raspberry Pi-Lab Manual-2-46
Raspberry Pi-Lab Manual-2-46
2
Click on start and search Raspberry pi imager
• Select operating system as raspberry pi os (32Bit)
• Select SD CARD and click on WRITE
• Insert SD card in pi and update the raspberry pi.
• After write finished remove SD card.
• Change the following
• When you start your Raspberry Pi for the first time, the Welcome to
Raspberry Pi application will pop up and guide you through the initial setup.
Click Next to start the setup.
Enter a new password for your Raspberry Pi and click next. In this step, you need to plug a
keyboard into the Raspberry Pi to help you input the new password.
Connect to your WiFi network by selecting its name, entering the
password, and clicking next.
Click Next let the wizard check for updates to Raspbian and install them (this might
take a little while).
3
• From menu-->Preferences-->Raspberry Pi 4 B+ Configuration. Then Go To
Interfaces
• Enable Camera, SSH, VNC, SPI, I2C, Serial Port. Do not Enable Remaining Things.
• Connect to Wi-Fi. > Check the date and Time.
• Open the Terminal. > Execute following default commands.
• sudo apt-get update > sudo apt-get up grade
• Now you can write your programs at Thonny Python IDE. Menu→ Programming
• →Thonny Python IDE.
• File → New ….. You can write your Program here and save.
• Execute the program at terminal only by following command sudo python <program-
name>.py
PI PROGRAMMING:
Start Thonny by clicking on the Raspberry Pi icon followed by Programming >
Thonny Python IDE
4
Sensor Description
1. Soil Moisture Sensor
The dielectric constant of soil increases as the water content of the soil increases.
The sensor is designed to estimate soil volumetric water content based on the
dielectric constant of the soil. The dielectric constant can be thought of as the soil's
ability to transmit electricity. The dielectric constant of soil increases as the water
content of the soil increases. This response is due to the fact that the dielectric
constant of water is much larger than the other soil components, including air.
Thus, measurement of the dielectric constant gives a predictable estimation of
water content.
Soil moisture
Specification:
The light sensor converts this “light energy” whether visible or in the infrared parts of the
spectrum into an electrical signal output. From “Infrared” to “Visible” up to “Ultraviolet”
light spectrum. Sensitivity can be adjusted through potentiometer.
5
Light sensor
Specificatio
n:
• Using photosensitive resistance sensor sensitive type.
• Working Voltage: 3.3V-5V.
• Output form: Digital Switch shows (0 for Brightness and 1 for darkness).
• Fixed Bolt Hole, easy installation.
Specification:
• Operating Voltage: 3.5V to 5.5V.
• Operating current: 0.3mA (measuring) 60uA (standby) Output: Serial data.
• Temperature Range: 0°C to 50°C. Humidity Range: 20% to 90%
• Resolution: Temperature and Humidity both are 16-bit. Accuracy: ±1°C and ±1%
Ultrasonic Distance Sensor:
An ultrasonic sensor is to measures the distance of an object using ultrasonic sound
waves. An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses
that relay back information about an object’s proximity. High-frequency sound waves
reflect from boundaries to produce distinct echo patterns.
6
Ultrasonic sonic sensor
Specification:
• Working Voltage: DC 5V.
• Working Current: 15mA.
• Working Frequency: 40Hz.
• Max Range: 4m.
• Min Range: 2cm.
• Measuring Angle: 15 degree.
• Trigger Input Signal: 10µS TTL pulse.
• Echo Output Signal Input TTL level signal and the range in proportion.
RCWL-0516:
RCWL-0516 is a Doppler radar microwave motion sensor that can act as an alternative
to a PIR motion sensor. This get repository is an attempt to collect the rather scant
information on this board in one place.
Specification:
• Equipped with RCWL-9196 chip supporting 4-28V wide voltage input, high
level signal output when detecting the motion with active repeat trigger
7
• 360 degree no blind angle detection with 5-7m detecting distance, the
detection not affected by the surroundings, better performance than the IR
sensor
• Support night detecting function turn on /off, adjustable repeat trigger time and
detecting distance by add the SMD components to the corresponding pins
• Working Voltage: DC 4-28V, Board Size: 35.9 X 17.3mm/1.41 X 0.68inch
GAS Sensor
Gas Sensors (MQ2) are useful to detect the gas leakage (home and industry). It is
suitable for detecting the Smoke, CO, H2, LPG, CH4 and Alcohol. Due to fast response
time and its high sensitivity, measurement can be taken as soon as possible.
Potentiometer is used to adjust the sensitivity of the sensor.
Gas Sensor
Specification:
• Operating Voltage is +5V.
• Can be used to Measure or detect LPG, Alcohol, Propane, Hydrogen, CO
and even methane.
• Analog output voltage: 0V to 5V.
• Digital Output Voltage: 0V or 5V (TTL Logic).
• Preheat duration 20 seconds.
• Can be used as a Digital or analog sensor.
• The Sensitivity of Digital pin can be varied using the potentiometer
Analog to Digital converter:
The MCP3008 is a low cost 8-channel 10-bit analog to digital converter. The precision
of this ADC is similar to that of an Arduino Uno, and with 8 channels you can read
quite a few analog signals from the Pi. This chip is a great option if you just
need to read
simple analog signals, like from a temperature or light sensor.
8
Light Emitting Diode:
LEDs (Light Emitting Diodes) are semiconductor light sources that combine a P-type
semiconductor (larger hole concentration) with an N-type semiconductor (larger
electron concentration). Applying a sufficient forward voltage will cause the electrons
and holes to recombine at the P-N junction, releasing energy in the form of light.
9
Switches:
Switching happens reliably at specific and repeatable positions of the actuator, which
is not necessarily true of other mechanisms. They are very common due to their low
cost but high durability, greater than 1 million cycles and up to 10 million cycles for
heavy-duty models. This durability is a natural consequence of the design.
4x4 keypad:
10
TESTING of LDR SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Light Dependent Resistors (LDR) with Raspberry Pi.
Write code as per requirement and save it with .py extension example ldr.py on
desktop. And then Run program output will display on Shell as shown below
11
CODE
#CONNECT C1 TO C14 WITH 4 PIN FEMALE CONNECTOR AND RUN
PROGRAM
import RPi.GPIO as GPIO
import time
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(light,GPIO.IN)
def read_light():
while True:
12
def destroy(): #When program ending, the function is executed.
GPIO.cleanup()
read_light()
except KeyboardInterrupt:
if light_state == 0:
print("Light Detected")
elif light_state == 1:
time.sleep(.3)
13
TESTING of DHT11 SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Digital Humidity and Temperature (DHT) with Raspberry Pi.
Write code as per requirement and save it with .py extension example dht11.py on
desktop. And then Run program output will display on Shell as shown below
14
CODE
#Before executing this program intsall Adafruit_DHT11 Library using follwing
c7 import time
import Adafruit_DHT
while(1):
15
try:
print ("Temperature
Note if unable to find bcm try with beagle bone then go for
Go to file > USR > LOCAL > LIB > 3.9 > distpackages > Adafruit_DHT > Platform_detect.py >
Type sudo nano platform_detech.py and add bcm2711 like below > ctrl+x to save & exit.
16
TESTING of SOILMOISTURE SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Soil Moisture with Raspberry Pi.
Write code as per requirement and save it with .py extension example dht11.py on
desktop. And then Run program output will display on Shell as shown below
17
CODE
import RPi.GPIO as
relay = 12
GPIO.setup(soil,GPIO.IN)
GPIO.setup(relay,GPIO.OUT)
def read_soil():
while True:
soil_state = GPIO.input(soil)
if soil_state == 0:
18
print("soil Moisture Not
GPIO.output(relay,True)
elif soil_state == 1:
print("Soil Moisture
GPIO.output(relay,False)
time.sleep(.3)
GPIO.cleanup()
here try:
setup()
read_soil()
except
KeyboardInterrupt:
destroy()
19
TESTING of RCWL MOTION DETECHTOR SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Motion Detecting sensor (RCWL) with Raspberry Pi.
20
CODE
import RPi.GPIO as GPIO
import time
rcwl = 11 #Board
GPIO.setmode(GPIO.BOARD)
GPIO.setup(rcwl,GPIO.
while True:
21
rcwl_state = GPIO.input(rcwl) if rcwl_state == 0:
print("motion Detected")
time.sleep(.3)
GPIO.cleanup()
setup()
read_rcwl()
except
KeyboardInterrupt: destroy()
22
TESTING of ULTRASONIC SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Ultrasonic sensor with Raspberry Pi.
23
CODE
# Program to test Ultrasonic Distance
import RPi.GPIO as GPIO #Import GPIO library import time #Import time library
GPIO.setup(ECHO,GPIO.IN)
24
while True:
GPIO.output(TRIG, False) #Set TRIG as LOW print ("Waitng For Sensor To Settle")
TRIG as LOW
if distance > 2 and distance < 400: #Check whether the distance is
else:
25
TESTING of MQ2 GAS SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Gas Sensor with Raspberry Pi.
26
CODE
import RPi.GPIO as
relay = 12
GPIO.setup(gas,GPIO.IN)
GPIO.setup(relay,GPIO.OUT)
27
def
read_gas():
if gas_state == 0:
print("GAS Detected")
print("Relay on")
GPIO.output(relay,True)
elif gas_state == 1:
print("Relay off")
GPIO.output(relay,False)
time.sleep(.3)
GPIO.cleanup()
here try:
setup()
read_gas
()
except
KeyboardInterrupt:
destroy()
28
TESTING of RELAY SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Relay Sensor with Raspberry Pi.
29
CODES:
import time
import RPi.GPIO as gpio
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(36, gpio.OUT)
try:
while(1):
gpio.output(36,0)
print("Relay OFF")
time.sleep(1)
gpio.output(36,1)
print("Relay ON")
time.sleep(1)
#gpio.cleanup()
except KeyboardInterrupt:
gpio.cleanup()
exit
30
TESTING of BUZZER SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Buzzer Sensor with Raspberry Pi.
31
CODES:
import time
import RPi.GPIO as gpio
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(36, gpio.OUT)
try:
while(1):
gpio.output(36,0)
print("Buzzer OFF")
time.sleep(1)
gpio.output(36,1)
print("Buzzer ON")
time.sleep(1)
#gpio.cleanup()
except KeyboardInterrupt:
gpio.cleanup()
exit
32
TESTING of IR SENSOR WITH RASPBERRY
PI CIRCUIT DIAGRAM
AIM: To Interface IR Sensor with Raspberry Pi.
33
CODES:
import RPi.GPIO as GPIO
import time
sensor1 = 11
sensor2 = 13
#sensor3 = 13
#sensor4 = 15
buzzer = 40
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor1,GPIO.IN)
GPIO.setup(sensor2,GPIO.IN)
#GPIO.setup(sensor3,GPIO.IN)
#GPIO.setup(sensor4,GPIO.IN)
GPIO.setup(buzzer,GPIO.OUT)
GPIO.output(buzzer,False)
print "IR Sensor Ready .... "
print " "
try:
while True:
if GPIO.input(sensor1):
GPIO.output(buzzer,True)
print "Object Detected-1"
while GPIO.input(sensor1):
time.sleep(0.3)
else: GPIO.output(buzzer,False)
if GPIO.input(sensor2):
GPIO.output(buzzer,True)
print "Object Detected-2"
while GPIO.input(sensor2):
time.sleep(0.3)
else: GPIO.output(buzzer,False)
except KeyboardInterrupt:
GPIO.cleanup()
34
TESTING of LCD SENSOR WITH
RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface LCD Sensor with Raspberry Pi.
35
CODES:.
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
def main():
# Main program block
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7
# Initialise display
lcd_init()
while True:
36
# Send some text
lcd_string("1234567890123456",LCD_LINE_1)
lcd_string("abcdefghijklmnop",LCD_LINE_2)
def lcd_init():
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)
GPIO.output(LCD_RS, mode) # RS
# High bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)
37
# Toggle 'Enable' pin
lcd_toggle_enable()
# Low bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)
def lcd_toggle_enable():
# Toggle enable
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)
def lcd_string(message,line):
# Send string to display
lcd_byte(line, LCD_CMD)
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
if name == ' main ':
try:
main()
except KeyboardInterrupt:
pass
finally:
lcd_byte(0x01, LCD_CMD)
lcd_string("Goodbye!",LCD_LINE_1)
38
GPIO.cleanup()
39
CODES:
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(led1,gpio.OUT,initial=0)
gpio.setup(led2,gpio.OUT,initial=0)
gpio.setup(led3,gpio.OUT,initial=0)
gpio.setup(led4,gpio.OUT,initial=0)
gpio.setup(switch1,gpio.IN)
gpio.setup(switch2,gpio.IN)
gpio.setup(switch3,gpio.IN)
gpio.setup(switch4,gpio.IN)
def glow_led(event):
if event == switch1 :
gpio.output(led1, True)
time.sleep(0.2)
gpio.output(led1, False)
40
elif event == switch2 :
gpio.output(led2, True)
time.sleep(0.2)
gpio.output(led2, False)
try:
while(True):
#to avoid 100% CPU usage
time.sleep(1)
except KeyboardInterrupt:
#cleanup GPIO settings before exiting
gpio.cleanup()
41
TESTING of KEYPAD MATRIX SENSOR
WITH RASPBERRY PI CIRCUIT DIAGRAM
AIM: To Interface Keypad Matrix Sensor with Raspberry Pi.
42
Codes:
# Connect CN9 to CN5
#sudo apt-get install pad4pi
import RPi.GPIO as GPIO
import time
from pad4pi import rpi_gpio
KEYPAD = [
["1","2","3","A"],
["4","5","6","B"],
["7","8","9","C"],
["*","0","#","D"],
]
ROW_PINS =[21,20,16,12] # pin num 40,38,36,32
COL_PINS =[7,8,25,24] # pin num 26,24,22,18
factory = rpi_gpio.KeypadFactory()
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS,
col_pins=COL_PINS)
def printKey(key):
print(key)
time.sleep(0.2)
keypad.registerKeyPressHandler(printKey)
43
TESTING of ANALOG to DIDGTAL SENSOR WITH RASPBERRY
AIM: To Interface MCP3008 & Potentiometer with Raspberry Pi.
CIRCUIT DIAGRAM:
Connect as per picture below on PHYSITECH trainer C18 to POT OUT and C20 to C9
44
CODE:
import busio
import digitalio
import board
import time
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
while True:
value1 = AnalogIn(mcp, MCP.P0)
#print('Raw ADC Value: ', chan.value)
print('ADC Voltage: ' + str(value1.voltage) + 'V')
time.sleep(0.5)
if(value1.voltage<.5):
print('EMPTY')
led.value = False
led1.value = False
led2.value = False
led3.value = False
if(value1.voltage>0.5) & (value1.voltage<1):
print('LOW')
led.value = True
45
led1.value = False
led2.value = False
led3.value = False
if(value1.voltage>1) & (value1.voltage<2):
print('MEDIUM')
led.value = True
led1.value = True
led2.value = False
led3.value = False
if(value1.voltage>2) & (value1.voltage<3):
print('HIGH')
led.value = True
led1.value = True
led2.value = True
led3.value = False
if(value1.voltage>3):
print('FULL')
led.value = True
led1.value = True
led2.value = True
led3.value = True
Output will appear on shell vary potentiometer to observe variation of input signal.
46