import RPi.GPIO as GPIO
import time
# Set the GPIO mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin numbers
led_pin = 18 # The pin connected to the LED
button_pin = 23 # The pin connected to the button
# Set up the LED pin as an output and the button pin as an input
GPIO.setup(led_pin, GPIO.OUT)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while True:
# Check if the button is pressed
if GPIO.input(button_pin) == GPIO.LOW:
# Turn on the LED
GPIO.output(led_pin, GPIO.HIGH)
else:
# Turn off the LED
GPIO.output(led_pin, GPIO.LOW)
# Small delay to debounce the button press
time.sleep(0.1)
except KeyboardInterrupt:
# Clean up the GPIO on CTRL+C exit
GPIO.cleanup()
-------------------------
import RPi.GPIO as GPIO
import time
# Set the GPIO mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin numbers
red_led_pin = 20 # The pin connected to the red LED
blue_led_pin = 21 # The pin connected to the blue LED
button_pin = 23 # The pin connected to the button
motion_sensor_pin = 24 # The pin connected to the motion sensor
# Set up the LED pins as outputs and the button and motion sensor pins as inputs
GPIO.setup(red_led_pin, GPIO.OUT)
GPIO.setup(blue_led_pin, GPIO.OUT)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(motion_sensor_pin, GPIO.IN)
try:
while True:
# Check if the button is pressed
if GPIO.input(button_pin) == GPIO.LOW:
# Turn on the red LED
GPIO.output(red_led_pin, GPIO.HIGH)
else:
# Turn off the red LED
GPIO.output(red_led_pin, GPIO.LOW)
# Check if motion is detected
if GPIO.input(motion_sensor_pin):
# Turn on the blue LED
GPIO.output(blue_led_pin, GPIO.HIGH)
else:
# Turn off the blue LED
GPIO.output(blue_led_pin, GPIO.LOW)
# Small delay to debounce the button press and allow sensor to reset
time.sleep(0.1)
except KeyboardInterrupt:
# Clean up the GPIO on CTRL+C exit
GPIO.cleanup()
------------------------------
import RPi.GPIO as GPIO
import time
# Set the GPIO mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin numbers
red_led_pin = 20 # The pin connected to the red LED
blue_led_pin = 21 # The pin connected to the blue LED
green_led_pin = 22 # The pin connected to the green LED
button_pin = 23 # The pin connected to the button
motion_sensor_pin = 24 # The pin connected to the motion sensor
sound_sensor_pin = 25 # The pin connected to the sound sensor
# Set up the LED pins as outputs and the button, motion, and sound sensor pins as inputs
GPIO.setup(red_led_pin, GPIO.OUT)
GPIO.setup(blue_led_pin, GPIO.OUT)
GPIO.setup(green_led_pin, GPIO.OUT)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(motion_sensor_pin, GPIO.IN)
GPIO.setup(sound_sensor_pin, GPIO.IN)
try:
while True:
# Check if the button is pressed
if GPIO.input(button_pin) == GPIO.LOW:
# Turn on the red LED
GPIO.output(red_led_pin, GPIO.HIGH)
else:
# Turn off the red LED
GPIO.output(red_led_pin, GPIO.LOW)
# Check if motion is detected
if GPIO.input(motion_sensor_pin):
# Turn on the blue LED
GPIO.output(blue_led_pin, GPIO.HIGH)
else:
# Turn off the blue LED
GPIO.output(blue_led_pin, GPIO.LOW)
# Check if sound is detected
if GPIO.input(sound_sensor_pin):
# Turn on the green LED
GPIO.output(green_led_pin, GPIO.HIGH)
else:
# Turn off the green LED
GPIO.output(green_led_pin, GPIO.LOW)
# Small delay to debounce the button press and allow sensors to reset
time.sleep(0.1)
except KeyboardInterrupt:
# Clean up the GPIO on CTRL+C exit
GPIO.cleanup()
------------------------
import RPi.GPIO as GPIO
import time
# Set the GPIO mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin numbers
red_led_pin = 20 # The pin connected to the red LED
orange_led_pin = 27 # The pin connected to the orange LED
blue_led_pin = 21 # The pin connected to the blue LED
green_led_pin = 22 # The pin connected to the green LED
yellow_led_pin = 17 # The pin connected to the yellow LED
button_pin = 23 # The pin connected to the button
motion_sensor_pin = 24 # The pin connected to the motion sensor
sound_sensor_pin = 25 # The pin connected to the sound sensor
# Set up the LED pins as outputs and the button, motion, and sound sensor pins as inputs
GPIO.setup(red_led_pin, GPIO.OUT)
GPIO.setup(orange_led_pin, GPIO.OUT)
GPIO.setup(blue_led_pin, GPIO.OUT)
GPIO.setup(green_led_pin, GPIO.OUT)
GPIO.setup(yellow_led_pin, GPIO.OUT)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(motion_sensor_pin, GPIO.IN)
GPIO.setup(sound_sensor_pin, GPIO.IN)
try:
while True:
# Check if the button is pressed
if GPIO.input(button_pin) == GPIO.LOW:
# Turn on the red LED
GPIO.output(red_led_pin, GPIO.HIGH)
time.sleep(0.5) # Wait for 0.5 seconds
# Turn on the orange LED
GPIO.output(orange_led_pin, GPIO.HIGH)
time.sleep(0.5) # Wait for 0.5 seconds
# Turn off the red and orange LEDs
GPIO.output(red_led_pin, GPIO.LOW)
GPIO.output(orange_led_pin, GPIO.LOW)
# Check if motion is detected
if GPIO.input(motion_sensor_pin):
# Turn on the blue LED
GPIO.output(blue_led_pin, GPIO.HIGH)
time.sleep(0.5) # Wait for 0.5 seconds
# Turn off the blue LED
GPIO.output(blue_led_pin, GPIO.LOW)
# Check if sound is detected
if GPIO.input(sound_sensor_pin):
# Turn on the green LED
GPIO.output(green_led_pin, GPIO.HIGH)
time.sleep(0.5) # Wait for 0.5 seconds
# Turn on the yellow LED
GPIO.output(yellow_led_pin, GPIO.HIGH)
time.sleep(0.5) # Wait for 0.5 seconds
# Turn off the green and yellow LEDs
GPIO.output(green_led_pin, GPIO.LOW)
GPIO.output(yellow_led_pin, GPIO.LOW)
# Small delay to debounce the button press and allow sensors to reset
time.sleep(0.1)
except KeyboardInterrupt:
# Clean up the GPIO on CTRL+C exit
GPIO.cleanup()
-------------------
import RPi.GPIO as GPIO
import time
# Set the GPIO mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin numbers
red_led_pin = 20 # The pin connected to the red LED
orange_led_pin = 27 # The pin connected to the orange LED
blue_led_pin = 21 # The pin connected to the blue LED
pink_led_pin = 5 # The pin connected to the pink LED
white_led_pin = 6 # The pin connected to the white LED
green_led_pin = 22 # The pin connected to the green LED
yellow_led_pin = 17 # The pin connected to the yellow LED
button_pin = 23 # The pin connected to the button
motion_sensor_pin = 24 # The pin connected to the motion sensor
sound_sensor_pin = 25 # The pin connected to the sound sensor
# Set up the LED pins as outputs and the button, motion, and sound sensor pins as inputs
GPIO.setup(red_led_pin, GPIO.OUT)
GPIO.setup(orange_led_pin, GPIO.OUT)
GPIO.setup(blue_led_pin, GPIO.OUT)
GPIO.setup(pink_led_pin, GPIO.OUT)
GPIO.setup(white_led_pin, GPIO.OUT)
GPIO.setup(green_led_pin, GPIO.OUT)
GPIO.setup(yellow_led_pin, GPIO.OUT)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(motion_sensor_pin, GPIO.IN)
GPIO.setup(sound_sensor_pin, GPIO.IN)
def blink_led(pin, blink_times=3, blink_duration=0.2):
"""Function to blink an LED"""
for _ in range(blink_times):
GPIO.output(pin, GPIO.HIGH)
time.sleep(blink_duration)
GPIO.output(pin, GPIO.LOW)
time.sleep(blink_duration)
try:
while True:
# Check if the button is pressed
if GPIO.input(button_pin) == GPIO.LOW:
# Turn on the red LED, then the orange LED
GPIO.output(red_led_pin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(orange_led_pin, GPIO.HIGH)
time.sleep(0.5)
# Turn off the red and orange LEDs
GPIO.output(red_led_pin, GPIO.LOW)
GPIO.output(orange_led_pin, GPIO.LOW)
# Check if motion is detected
if GPIO.input(motion_sensor_pin):
# Turn on the blue LED, then the pink LED, and blink the white LED
GPIO.output(blue_led_pin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pink_led_pin, GPIO.HIGH)
time.sleep(0.5)
blink_led(white_led_pin)
# Turn off the blue and pink LEDs
GPIO.output(blue_led_pin, GPIO.LOW)
GPIO.output(pink_led_pin, GPIO.LOW)
# Check if sound is detected
if GPIO.input(sound_sensor_pin):
# Turn on the green LED, then the yellow LED
GPIO.output(green_led_pin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(yellow_led_pin, GPIO.HIGH)
time.sleep(0.5)
# Turn off the green and yellow LEDs
GPIO.output(green_led_pin, GPIO.LOW)
GPIO.output(yellow_led_pin, GPIO.LOW)
# Small delay to debounce the button press and allow sensors to reset
time.sleep(0.1)
except KeyboardInterrupt:
# Clean up the GPIO on CTRL+C exit
GPIO.cleanup()
--------------------------------------
import RPi.GPIO as GPIO
import time
# Set the GPIO mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin numbers
trigger_pin = 18 # The pin used to trigger the ultrasonic sensor
echo_pin = 24 # The pin used to receive the signal from the ultrasonic sensor
red_led_pin = 20 # The pin connected to the red LED
blue_led_pin = 21 # The pin connected to the blue LED
# Set up the LED pins as outputs and the ultrasonic pins
GPIO.setup(red_led_pin, GPIO.OUT)
GPIO.setup(blue_led_pin, GPIO.OUT)
GPIO.setup(trigger_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)
def get_distance():
# Send a 10us pulse to trigger the sensor
GPIO.output(trigger_pin, True)
time.sleep(0.00001)
GPIO.output(trigger_pin, False)
start_time = time.time()
stop_time = time.time()
# Save the start time
while GPIO.input(echo_pin) == 0:
start_time = time.time()
# Save the arrival time
while GPIO.input(echo_pin) == 1:
stop_time = time.time()
# Calculate the time difference and then the distance
time_elapsed = stop_time - start_time
distance = (time_elapsed * 34300) / 2 # Speed of sound wave divided by 2 (go and back)
return distance
try:
while True:
dist = get_distance()
print(f"Measured Distance = {dist:.1f} cm")
# Check the distance for the red LED
if dist == 100:
GPIO.output(red_led_pin, GPIO.HIGH)
else:
GPIO.output(red_led_pin, GPIO.LOW)
# Check the distance for the blue LED
if dist == 4:
GPIO.output(blue_led_pin, GPIO.HIGH)
else:
GPIO.output(blue_led_pin, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
print("Measurement stopped by user")
GPIO.cleanup()
----------------------------
import RPi.GPIO as GPIO
import time
# Set the GPIO mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin numbers for the ultrasonic sensor
trigger_pin = 18
echo_pin = 24
# Define the GPIO pin numbers for the motors
motor1_forward = 23
motor1_backward = 22
motor2_forward = 27
motor2_backward = 17
# Set up the ultrasonic sensor pins
GPIO.setup(trigger_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)
# Set up the motor pins
GPIO.setup(motor1_forward, GPIO.OUT)
GPIO.setup(motor1_backward, GPIO.OUT)
GPIO.setup(motor2_forward, GPIO.OUT)
GPIO.setup(motor2_backward, GPIO.OUT)
def get_distance():
# Send a 10us pulse to trigger the sensor
GPIO.output(trigger_pin, True)
time.sleep(0.00001)
GPIO.output(trigger_pin, False)
start_time = time.time()
stop_time = time.time()
# Save the start time
while GPIO.input(echo_pin) == 0:
start_time = time.time()
# Save the arrival time
while GPIO.input(echo_pin) == 1:
stop_time = time.time()
# Calculate the time difference and then the distance
time_elapsed = stop_time - start_time
distance = (time_elapsed * 34300) / 2 # Speed of sound wave divided by 2 (go and back)
return distance
def drive_forward():
GPIO.output(motor1_forward, GPIO.HIGH)
GPIO.output(motor2_forward, GPIO.HIGH)
def stop():
GPIO.output(motor1_forward, GPIO.LOW)
GPIO.output(motor2_forward, GPIO.LOW)
GPIO.output(motor1_backward, GPIO.LOW)
GPIO.output(motor2_backward, GPIO.LOW)
def turn_back():
# Stop the car first
stop()
# Reverse the car for 2 seconds
GPIO.output(motor1_backward, GPIO.HIGH)
GPIO.output(motor2_backward, GPIO.HIGH)
time.sleep(2)
# Stop the car after turning back
stop()
try:
while True:
distance = get_distance()
print(f"Distance: {distance} cm")
if distance > 5:
drive_forward()
else:
turn_back()
time.sleep(0.1)
except KeyboardInterrupt:
print("Program stopped by user")
GPIO.cleanup()
---------------

No comments:
Post a Comment