SlideShare a Scribd company logo
1 of 6
Download to read offline
PYTHON CODE FOR SMART OBSTACLE DETECTION
USING RASBERRYPI
CODE TO CALCULATE THE DISTANCE FROM THE OBSTACLE USING ULTRASONIC
SENSOR
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
# Define GPIO pins for the ultrasonic sensor
TRIG = 11
ECHO = 13
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
# Distance measurement function
def distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
pulse_start = time.time()
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
pulse_end = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
# Speed of sound is 343m/s
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance
try:
while True:
dist = distance()
print("Distance: ", dist, "cm")
if dist < 30:
print("Obstacle detected!")
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
This code sets up the GPIO pins for the ultrasonic sensor, defines a function
to measure the distance, and then enters an infinite loop that continually
measures the distance and prints it to the console. If the distance is less than
30cm, the code prints "Obstacle detected!". The code also includes a try-
except block to handle keyboard interrupts and cleanup the GPIO pins when
the code is stopped.
CODE TO CALCULATE THE PITHOLE FROM THE OBSTACLE USING ULTRASONIC
SENSOR
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
# Define GPIO pins for the ultrasonic sensor
TRIG = 11
ECHO = 13
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
# Distance measurement function
def distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
pulse_start = time.time()
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
pulse_end = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
# Speed of sound is 343m/s
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance
try:
while True:
dist = distance()
print("Distance: ", dist, "cm")
if dist > 100:
print("Pit-hole detected!")
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
This code sets up the GPIO pins for the ultrasonic sensor, defines a function
to measure the distance, and then enters an infinite loop that continually
measures the distance and prints it to the console. If the distance is greater
than 100cm, the code prints "Pit-hole detected!". The code also includes a
try-except block to handle keyboard interrupts and cleanup the GPIO pins
when the code is stopped.
CODE TO CALCULATE THE PITHOLE FROM THE OBSTACLE USING ULTRASONIC
SENSOR
mport os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
# Define the location of the sensor
temp_sensor = '/sys/bus/w1/devices/28-0311970c3eff/w1_slave'
# Temperature measurement function
def read_temp_raw():
f = open(temp_sensor, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
try:
while True:
temp_c, temp_f = read_temp()
print("Temperature: ", temp_c, "C /", temp_f, "F")
time.sleep(1)
except KeyboardInterrupt:
print("Stopped by user")
This code sets up the Raspberry Pi to read from the DS18B20 temperature
sensor, defines a function to read the temperature, and then enters an infinite
loop that continually reads the temperature and prints it to the console. The
temperature is commanded through voice assistant to the user. The code also
includes a try-except block to handle keyboard interrupts and print a
message when the code is stopped.

More Related Content

Similar to PYTHON CODE FOR SMART OBSTACLE DETECTION USING RASBERRYPI.pdf

AOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docxAOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docx
KomlikaTaru
 
Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013
Tom Paulus
 
In this project you implement a program such that it simulates the p.pdf
In this project you implement a program such that it simulates the p.pdfIn this project you implement a program such that it simulates the p.pdf
In this project you implement a program such that it simulates the p.pdf
fathimafancy
 
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Irfan Qadoos
 

Similar to PYTHON CODE FOR SMART OBSTACLE DETECTION USING RASBERRYPI.pdf (20)

Arduino projects &amp; tutorials
Arduino projects &amp; tutorialsArduino projects &amp; tutorials
Arduino projects &amp; tutorials
 
Mitsubachi Arduino code
Mitsubachi Arduino codeMitsubachi Arduino code
Mitsubachi Arduino code
 
Raspberry Pi Sensors Tutorial
Raspberry Pi Sensors TutorialRaspberry Pi Sensors Tutorial
Raspberry Pi Sensors Tutorial
 
Arduino
ArduinoArduino
Arduino
 
Arduino
ArduinoArduino
Arduino
 
AOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docxAOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docx
 
Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013
 
The IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programmingThe IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programming
 
In this project you implement a program such that it simulates the p.pdf
In this project you implement a program such that it simulates the p.pdfIn this project you implement a program such that it simulates the p.pdf
In this project you implement a program such that it simulates the p.pdf
 
Session3
Session3Session3
Session3
 
Embedded system course projects - Arduino Course
Embedded system course projects - Arduino CourseEmbedded system course projects - Arduino Course
Embedded system course projects - Arduino Course
 
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
 
DomCode 2015 - Abusing phones to make the internet of things
DomCode 2015 - Abusing phones to make the internet of thingsDomCode 2015 - Abusing phones to make the internet of things
DomCode 2015 - Abusing phones to make the internet of things
 
Getting Started with Raspberry Pi - USC 2013
Getting Started with Raspberry Pi - USC 2013Getting Started with Raspberry Pi - USC 2013
Getting Started with Raspberry Pi - USC 2013
 
Easy GPS Tracker using Arduino and Python
Easy GPS Tracker using Arduino and PythonEasy GPS Tracker using Arduino and Python
Easy GPS Tracker using Arduino and Python
 
Ultrsonic sensor_Arduino_DRKG.pptx
Ultrsonic sensor_Arduino_DRKG.pptxUltrsonic sensor_Arduino_DRKG.pptx
Ultrsonic sensor_Arduino_DRKG.pptx
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
 
Multi Sensory Communication 2/2
Multi Sensory Communication 2/2Multi Sensory Communication 2/2
Multi Sensory Communication 2/2
 
Arduino tutorial #4
Arduino tutorial #4Arduino tutorial #4
Arduino tutorial #4
 

Recently uploaded

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 

PYTHON CODE FOR SMART OBSTACLE DETECTION USING RASBERRYPI.pdf

  • 1. PYTHON CODE FOR SMART OBSTACLE DETECTION USING RASBERRYPI CODE TO CALCULATE THE DISTANCE FROM THE OBSTACLE USING ULTRASONIC SENSOR import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) # Define GPIO pins for the ultrasonic sensor TRIG = 11 ECHO = 13 GPIO.setup(TRIG, GPIO.OUT) GPIO.setup(ECHO, GPIO.IN) # Distance measurement function def distance(): GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False) pulse_start = time.time() while GPIO.input(ECHO) == 0: pulse_start = time.time() pulse_end = time.time() while GPIO.input(ECHO) == 1: pulse_end = time.time()
  • 2. pulse_duration = pulse_end - pulse_start # Speed of sound is 343m/s distance = pulse_duration * 17150 distance = round(distance, 2) return distance try: while True: dist = distance() print("Distance: ", dist, "cm") if dist < 30: print("Obstacle detected!") time.sleep(0.5) except KeyboardInterrupt: GPIO.cleanup() This code sets up the GPIO pins for the ultrasonic sensor, defines a function to measure the distance, and then enters an infinite loop that continually measures the distance and prints it to the console. If the distance is less than 30cm, the code prints "Obstacle detected!". The code also includes a try- except block to handle keyboard interrupts and cleanup the GPIO pins when the code is stopped.
  • 3. CODE TO CALCULATE THE PITHOLE FROM THE OBSTACLE USING ULTRASONIC SENSOR import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) # Define GPIO pins for the ultrasonic sensor TRIG = 11 ECHO = 13 GPIO.setup(TRIG, GPIO.OUT) GPIO.setup(ECHO, GPIO.IN) # Distance measurement function def distance(): GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False) pulse_start = time.time() while GPIO.input(ECHO) == 0: pulse_start = time.time() pulse_end = time.time() while GPIO.input(ECHO) == 1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start # Speed of sound is 343m/s distance = pulse_duration * 17150
  • 4. distance = round(distance, 2) return distance try: while True: dist = distance() print("Distance: ", dist, "cm") if dist > 100: print("Pit-hole detected!") time.sleep(0.5) except KeyboardInterrupt: GPIO.cleanup() This code sets up the GPIO pins for the ultrasonic sensor, defines a function to measure the distance, and then enters an infinite loop that continually measures the distance and prints it to the console. If the distance is greater than 100cm, the code prints "Pit-hole detected!". The code also includes a try-except block to handle keyboard interrupts and cleanup the GPIO pins when the code is stopped.
  • 5. CODE TO CALCULATE THE PITHOLE FROM THE OBSTACLE USING ULTRASONIC SENSOR mport os import glob import time os.system('modprobe w1-gpio') os.system('modprobe w1-therm') # Define the location of the sensor temp_sensor = '/sys/bus/w1/devices/28-0311970c3eff/w1_slave' # Temperature measurement function def read_temp_raw(): f = open(temp_sensor, 'r') lines = f.readlines() f.close() return lines def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0 temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c, temp_f
  • 6. try: while True: temp_c, temp_f = read_temp() print("Temperature: ", temp_c, "C /", temp_f, "F") time.sleep(1) except KeyboardInterrupt: print("Stopped by user") This code sets up the Raspberry Pi to read from the DS18B20 temperature sensor, defines a function to read the temperature, and then enters an infinite loop that continually reads the temperature and prints it to the console. The temperature is commanded through voice assistant to the user. The code also includes a try-except block to handle keyboard interrupts and print a message when the code is stopped.