Testing the DS18B20 Temperature Sensor on Raspberry Pi
Testing the DS18B20 Temperature Sensor on Raspberry Pi
- sudo modprobe w1-gpio
- sudo modprobe w1-therm
- cd /sys/bus/w1/devices
- ls
- cd 28-xxxx (replace with the serial number that appears)
- cat w1_slave
The 1‑Wire interface can be a bit flaky, but it reliably reports whether a temperature value is available. Because the sensor exposes its data as a simple text file, all you need to do is read the file.
The first line ends with either YES or NO. If it’s YES, the second line contains the temperature in thousandths of a degree Celsius. In the example above, the sensor reports 20.687 °C and then 26.125 °C.
When multiple sensors are connected, each will appear as a separate 28-xxxx directory. Plug one in at a time, note the created directory, and label the sensor for future reference.
Working with Ultrasonic Sensors
The HC‑SR04 ultrasonic sensor is powered from the Pi’s +5 V rail (pin 2). The trigger pin (GPIO 23) initiates a measurement, while the echo pin returns a high pulse whose width corresponds to the round‑trip distance. Because GPIO pins can only tolerate 3.3 V, a resistor divider protects the echo line.
Example 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‑Powered Rocket Launcher
The following script uses the ultrasonic sensor to trigger a rocket launch when an object is closer than a user‑defined distance. It also captures an image and lights an LED as a visual indicator.
#!/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)
# Allow module to settle
time.sleep(0.5)
# Set distance to trigger
setdistance = input('Please enter a value to trigger the camera:')
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()
Streaming a Webcam from the Raspberry Pi
To stream video from a USB webcam, ensure your Pi is connected to a network. Attach a recent webcam and follow the official guide for setting up MJPEG streaming. For a deeper dive, see the DS18B20 sensor test documentation.
Manufacturing process
- DS18B20 Temperature Sensor – Precise 1‑Wire Digital Thermometer for Industrial & Consumer Use
- How to Connect a DS1820 One‑Wire Temperature Sensor to a Raspberry Pi Using GPIO
- Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
- Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
- How to Connect a DS18B20 One‑Wire Digital Thermometer to a Raspberry Pi – A Step‑by‑Step Guide
- Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
- How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi
- Digital Light Sensor – Power an LED with Ambient Light Using Windows 10 IoT Core
- Assessing Sensor Accuracy: External vs Body Temperature Comparison
- Turck TB3-CP80: Precision Sensor Test Box for Inductive, Magnetic, Capacitive, and Photoelectric Sensors