Raspberry pi
Syntax:-
What is Raspberry pi
Types of this
How to install
light blink
traffic light project
IR sensor connect to pi
LDR sensor connect to pi
Camera connect to pi
Raspberry pi -
A Raspberry Pi is a small computer that can run a full operating system (usually Linux). It has more power and features than a microcontroller, like USB ports, HDMI output, Wi-Fi, and the ability to run complex software.
There are various types of Raspberry pi model
- Raspberry Pi 4 Model B
- Raspberry Pi 3 Model B+
- Raspberry Pi 3 Model B
- Raspberry Pi Zero 2 W
- Raspberry Pi Zero W
- Raspberry Pi 400
- Raspberry Pi Pico
How to install Raspberry pi 4
- insert SD card in card reader and connect to computer and click on choose storage.
- After process remove SD card and put in Raspberry pi
- And connect Raspberry pi to monitor/computer/laptop by HDMI cable and c-type cable, Now you can use Raspberry pi.
- insert SD card in card reader and connect to computer and click on choose storage.
- After process remove SD card and put in Raspberry pi
- And connect Raspberry pi to monitor/computer/laptop by HDMI cable and c-type cable, Now you can use Raspberry pi.
Light blink code for pi
from gpiozero import LED from time import sleep
led = LED(17) # Connect LED to GPIO 17
while True: led.on() sleep(1) led.off() sleep(1)
//gpiozero is a Python library that makes it very easy to use the GPIO (General Purpose Input Output) pins on a Raspberry Pi.
Traffic light project:-
import RPi.GPIO as GPIOimport time
# Set GPIO modeGPIO.setmode(GPIO.BCM) #Broadcom SoC (chip) numbering
# Assign GPIO pinsRED = 17YELLOW = 27GREEN = 22
# Set up pins as outputGPIO.setup(RED, GPIO.OUT)GPIO.setup(YELLOW, GPIO.OUT)GPIO.setup(GREEN, GPIO.OUT)
def traffic_light_cycle(): while True: # Green light on GPIO.output(GREEN, True) GPIO.output(YELLOW, False) GPIO.output(RED, False) time.sleep(5) # Yellow light on GPIO.output(GREEN, False) GPIO.output(YELLOW, True) time.sleep(2) # Red light on GPIO.output(YELLOW, False) GPIO.output(RED, True) time.sleep(5)
try: traffic_light_cycle()except KeyboardInterrupt: print("Traffic light simulation stopped by user.")finally: GPIO.cleanup()
IR sensor connect to Raspberry pi
import RPi.GPIO as GPIOimport time
# Pin definitionsIR_SENSOR = 17 # GPIO pin number
# SetupGPIO.setmode(GPIO.BCM)GPIO.setup(IR_SENSOR, GPIO.IN)
print("IR Sensor Test (press Ctrl+C to exit)") #optional
try: while True: if GPIO.input(IR_SENSOR) == 0: print("Object detected!") else: print("No object.") time.sleep(0.5)except KeyboardInterrupt: print("Program stopped.")finally: GPIO.cleanup() Motor connect to Raspberry pi :-
import RPi.GPIO as GPIOimport time
# Pin definitionsIN1 = 17IN2 = 18IN3 = 22IN4 = 23
# SetupGPIO.setmode(GPIO.BCM)GPIO.setup(IN1, GPIO.OUT)GPIO.setup(IN2, GPIO.OUT)GPIO.setup(IN3, GPIO.OUT)GPIO.setup(IN4, GPIO.OUT)
def stop(): GPIO.output(IN1, False) GPIO.output(IN2, False) GPIO.output(IN3, False) GPIO.output(IN4, False)
def forward(): GPIO.output(IN1, True) GPIO.output(IN2, False) GPIO.output(IN3, True) GPIO.output(IN4, False)
def backward(): GPIO.output(IN1, False) GPIO.output(IN2, True) GPIO.output(IN3, False) GPIO.output(IN4, True)
def left(): GPIO.output(IN1, False) GPIO.output(IN2, True) GPIO.output(IN3, True) GPIO.output(IN4, False)
def right(): GPIO.output(IN1, True) GPIO.output(IN2, False) GPIO.output(IN3, False) GPIO.output(IN4, True)
try: print("Moving Forward") forward() time.sleep(3) stop() time.sleep(60)
print("Moving Backward") backward() time.sleep(3) stop() time.sleep(60)
print("Turning Left") left() time.sleep(1.5) stop() time.sleep(60)
print("Turning Right") right() time.sleep(1.5) stop() time.sleep(60)
except KeyboardInterrupt: print("Stopped by user.")
finally: stop() GPIO.cleanup()
Ultrasonc Code in pi
import RPi.GPIO as GPIOimport time
# Pin DefinitionsTRIG = 23ECHO = 24
GPIO.setmode(GPIO.BCM)GPIO.setup(TRIG, GPIO.OUT)GPIO.setup(ECHO, GPIO.IN)
def measure_distance(): # Send 10us pulse to trigger GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False)
start_time = time.time() stop_time = time.time()
# Wait for Echo pin to go HIGH while GPIO.input(ECHO) == 0: start_time = time.time()
# Wait for Echo pin to go LOW while GPIO.input(ECHO) == 1: stop_time = time.time()
# Calculate pulse duration elapsed = stop_time - start_time
# Calculate distance (speed of sound = 34300 cm/s) distance = (elapsed * 34300) / 2
return distance
try: while True: dist = measure_distance() print(f"Distance: {dist:.2f} cm") time.sleep(1)
except KeyboardInterrupt: print("Measurement stopped by user.")
finally: GPIO.cleanup()
Servo connect to pi
import RPi.GPIO as GPIOimport time
servo_pin = 18GPIO.setmode(GPIO.BCM)GPIO.setup(servo_pin, GPIO.OUT)
pwm = GPIO.PWM(servo_pin, 50) # 50Hz frequencypwm.start(0)
def set_angle(angle): duty = 2 + (angle / 18) # Convert angle to duty cycle pwm.ChangeDutyCycle(duty) time.sleep(0.5) pwm.ChangeDutyCycle(0) # Stop sending signal to avoid jitter
try: set_angle(45) # Move servo to 45 degrees time.sleep(2) # Wait for 2 seconds to hold position
finally: pwm.stop() GPIO.cleanup()
from gpiozero import LED
from time import sleep
led = LED(17) # Connect LED to GPIO 17
while True:
led.on()
sleep(1)
led.off()
sleep(1)
//gpiozero is a Python library that makes it very easy to use the GPIO (General Purpose Input Output) pins on a Raspberry Pi.
Traffic light project:-
import RPi.GPIO as GPIO
import time
# Set GPIO mode
GPIO.setmode(GPIO.BCM) #Broadcom SoC (chip) numbering
# Assign GPIO pins
RED = 17
YELLOW = 27
GREEN = 22
# Set up pins as output
GPIO.setup(RED, GPIO.OUT)
GPIO.setup(YELLOW, GPIO.OUT)
GPIO.setup(GREEN, GPIO.OUT)
def traffic_light_cycle():
while True:
# Green light on
GPIO.output(GREEN, True)
GPIO.output(YELLOW, False)
GPIO.output(RED, False)
time.sleep(5)
# Yellow light on
GPIO.output(GREEN, False)
GPIO.output(YELLOW, True)
time.sleep(2)
# Red light on
GPIO.output(YELLOW, False)
GPIO.output(RED, True)
time.sleep(5)
try:
traffic_light_cycle()
except KeyboardInterrupt:
print("Traffic light simulation stopped by user.")
finally:
GPIO.cleanup()
IR sensor connect to Raspberry pi
import RPi.GPIO as GPIO
import time
# Pin definitions
IR_SENSOR = 17 # GPIO pin number
# Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(IR_SENSOR, GPIO.IN)
print("IR Sensor Test (press Ctrl+C to exit)") #optional
try:
while True:
if GPIO.input(IR_SENSOR) == 0:
print("Object detected!")
else:
print("No object.")
time.sleep(0.5)
except KeyboardInterrupt:
print("Program stopped.")
finally:
GPIO.cleanup()
Motor connect to Raspberry pi :-
import RPi.GPIO as GPIO
import time
# Pin definitions
IN1 = 17
IN2 = 18
IN3 = 22
IN4 = 23
# Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)
def stop():
GPIO.output(IN1, False)
GPIO.output(IN2, False)
GPIO.output(IN3, False)
GPIO.output(IN4, False)
def forward():
GPIO.output(IN1, True)
GPIO.output(IN2, False)
GPIO.output(IN3, True)
GPIO.output(IN4, False)
def backward():
GPIO.output(IN1, False)
GPIO.output(IN2, True)
GPIO.output(IN3, False)
GPIO.output(IN4, True)
def left():
GPIO.output(IN1, False)
GPIO.output(IN2, True)
GPIO.output(IN3, True)
GPIO.output(IN4, False)
def right():
GPIO.output(IN1, True)
GPIO.output(IN2, False)
GPIO.output(IN3, False)
GPIO.output(IN4, True)
try:
print("Moving Forward")
forward()
time.sleep(3)
stop()
time.sleep(60)
print("Moving Backward")
backward()
time.sleep(3)
stop()
time.sleep(60)
print("Turning Left")
left()
time.sleep(1.5)
stop()
time.sleep(60)
print("Turning Right")
right()
time.sleep(1.5)
stop()
time.sleep(60)
except KeyboardInterrupt:
print("Stopped by user.")
finally:
stop()
GPIO.cleanup()
Ultrasonc Code in pi
import RPi.GPIO as GPIO
import time
# Pin Definitions
TRIG = 23
ECHO = 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
def measure_distance():
# Send 10us pulse to trigger
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
start_time = time.time()
stop_time = time.time()
# Wait for Echo pin to go HIGH
while GPIO.input(ECHO) == 0:
start_time = time.time()
# Wait for Echo pin to go LOW
while GPIO.input(ECHO) == 1:
stop_time = time.time()
# Calculate pulse duration
elapsed = stop_time - start_time
# Calculate distance (speed of sound = 34300 cm/s)
distance = (elapsed * 34300) / 2
return distance
try:
while True:
dist = measure_distance()
print(f"Distance: {dist:.2f} cm")
time.sleep(1)
except KeyboardInterrupt:
print("Measurement stopped by user.")
finally:
GPIO.cleanup()
Servo connect to pi
import RPi.GPIO as GPIO
import time
servo_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin, GPIO.OUT)
pwm = GPIO.PWM(servo_pin, 50) # 50Hz frequency
pwm.start(0)
def set_angle(angle):
duty = 2 + (angle / 18) # Convert angle to duty cycle
pwm.ChangeDutyCycle(duty)
time.sleep(0.5)
pwm.ChangeDutyCycle(0) # Stop sending signal to avoid jitter
try:
set_angle(45) # Move servo to 45 degrees
time.sleep(2) # Wait for 2 seconds to hold position
finally:
pwm.stop()
GPIO.cleanup()
Comments
Post a Comment