Test DS18B20 sensor

  1. sudo modprobe w1-gpio
  2. sudo modprobe w1-therm
  3. cd /sys/bus/w1/devices
  4. ls
  5. cd 28-xxxx (change this to match what serial number pops up)
  6. cat w1_slave

The interface is a little unreliable, but fortunately it tells us if there is a valid temperature to read. It’s like a file, so all we have to do is read

The response will either have YES or NO at the end of the first line. If it is yes, then the temperature will be at the end of the second line, in 1/000 degrees C. So, in the example above, the temperature is actually read as 20.687 and then 26.125 degrees C.

If you have more than one Sensor connected, you’ll see multiple 28-xxx files. Each one will have the unique serial number so you may want to plug one in at a time, look at what file is created, and label the sensor!

Test DS18B20 sensor

UltraSonic Sensors

The Sensor is powered by a +5v rail from pin 2 of the Pi. The trigger pin comes from pin 16 (Gpio23) and this tell the sensor to start measuring. The echo pin in normally low until the sensor sends an output then it goes +5v for the time the sensor took to measure the distance. This is the reason for the resistors as the GPIO can only handle a +3.3v.

Diagram

The Code

#!/usr/bin/python
# Import required Python libraries
import time
import RPi.GPIO as GPIO
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
#
# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO = 24
#
print "Ultrasonic Measurement"
#
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
#
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
#
# Allow module to settle
time.sleep(0.5)
#
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
#
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
#
# Calculate pulse length
elapsed = stop-start
#
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34000
#
# That was the distance there and back so halve the value
distance = distance / 2
#
print "Distance : %.1f" % distance
#
# Reset GPIO settings
GPIO.cleanup()

Ultrasonic Rocket launcher

This is the code and diagram to fire the big trak rockets using the ultra sonic sensor on the Pi. The aim is to have the the rocket pod attached to Big Trak with the sensor at the front.The code is simple it will ask for a distance i normally say 80 (8cm).It will then print out the measurements every second until there is one below 80 then it`s fires the rockets , takes a picture using the call function and switches the LED on. This code is a work in progress so if you see any way to improve it I would like to hear.

Diagram in PDF 

#!/usr/bin/python
#
#
# Author : Julian and Kyle Milligan
# Date : 09/01/2013
# Import required Python libraries
import time
import RPi.GPIO as GPIO
from subprocess import call
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO = 24
GPIO_FIRE = 4
#
print "Ultrasonic Measurement"
#
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
GPIO.setup(GPIO_FIRE,GPIO.OUT) # Fire
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
# GPIO.output(GPIO_FIRE, False)
#
# Allow module to settle
time.sleep(0.5)
#
# set distance to trigger
setdistance = input('Please enter a value to trigger the camera:')#on screen prompt to fire
while True:
time.sleep(0.1)
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
s = start
quit =0
#
while quit ==0:
quit = GPIO.input(GPIO_ECHO)
start = time.time()
if start - s > 0.1:
quit = 1
#
if start - s < 0.1:
while GPIO.input(GPIO_ECHO) == 1:
stop = time.time()
#
elapsed = stop-start
#
distance = elapsed * 34300
distance = distance / 2
#
print "Distance : %.1f" % distance
#
if distance < setdistance:
call (["raspistill -o image.jpg"], shell=True) # take a picture with Pi Camera
print "Fire"
GPIO.output(GPIO_FIRE,True) ## Turn on GPIO pin 7 fire the rockets
#
# Reset GPIO settings
GPIO.cleanup(


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top