Difference between revisions of "Raspberry Pi and Sensors"

From csn
Jump to navigation Jump to search
(Created page with "In this activity we are going to integrate a range of sensors. <pre> #!/usr/bin/python3 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) TRIG = 23 ECHO = 24...")
 
Line 2: Line 2:
  
  
 +
 +
First, we will use the HC-SR04 Ultrasonic Module Distance Measuring Sensor to measure distance.
  
  

Revision as of 10:22, 18 July 2020

In this activity we are going to integrate a range of sensors.


First, we will use the HC-SR04 Ultrasonic Module Distance Measuring Sensor to measure distance.



#!/usr/bin/python3

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

TRIG = 23
ECHO = 24

print("Distance Measurement In Progress")

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)

print("Waiting For Sensor To Settle")

time.sleep(2)

GPIO.output(TRIG, True)
time.sleep(0.00001)

GPIO.output(TRIG, False)

while GPIO.input(ECHO)==0:

  pulse_start = time.time()

while GPIO.input(ECHO)==1:

  pulse_end = time.time()      

pulse_duration = pulse_end - pulse_start

distance = pulse_duration * 17150

distance = round(distance, 2)

print("Distance:",distance,"cm")

GPIO.cleanup()