Untitled
unknown
plain_text
2 years ago
2.0 kB
46
Indexable
--------ultrasonic-------
import time
import RPi.GPIO as GPIO
p=0
while p<20:
GPIO.setmode(GPIO.BOARD)
PIN_TRIGGER = 7
PIN_ECHO = 11
GPIO.setup(PIN_TRIGGER, GPIO.OUT)
GPIO.setup(PIN_ECHO, GPIO.IN)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
GPIO.output(PIN_TRIGGER, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
while GPIO.input(PIN_ECHO)==0:
pulse_start_time = time.time()
while GPIO.input(PIN_ECHO)==1:
pulse_end_time = time.time()
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
print ("Distance:",distance,"cm")
p=p+1
time.sleep(2)
GPIO.cleanup()
-------dht11-----
import time
import board
import adafruit_dht
# Sensor data pin is connected to GPIO 4
# Uncomment for DHT11
sensor = adafruit_dht.DHT11(board.D4)
while True:
try:
# Print the values to the serial port
temperature_c = sensor.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = sensor.humidity
print("Temp={0:0.1f}ºC, Temp={1:0.1f}ºF, Humidity={2:0.1f}%".format(temperature_c, temperature_f, humidity))
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
sensor.exit()
raise error
time.sleep(3.0)
-----ir-----
import RPi.GPIO as GPIO
import time
sensor = 16
buzzer = 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor,GPIO.IN)
GPIO.setup(buzzer,GPIO.OUT)
GPIO.output(buzzer,False)
print("IR Sensor Ready.....")
print(" ")
try:
while True:
if GPIO.input(sensor):
GPIO.output(buzzer,True)
print ("Object Detected")
while GPIO.input(sensor):
time.sleep(0.2)
else:
GPIO.output(buzzer,False)
except KeyboardInterrupt:
GPIO.cleanup()
Editor is loading...
Leave a Comment