Experiment 10: DS18B20 Temperature Sensor Experiment
From Diustou Wiki
Contents
Arduino
Experimental Phenomenon
- Print the detected temperature value via serial port.
Circuit Connection
Installing Libraries
- Installing DallasTemperature
- Installing OneWire
Reference Program
#include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup(void) { Serial.begin(9600); Serial.println("Dallas Temperature IC Control Library Demo"); sensors.begin(); } void loop(void) { Serial.print(" Requesting temperatures..."); sensors.requestTemperatures(); Serial.println("DONE"); Serial.print("Temperature for Device 1 is: "); Serial.print(sensors.getTempCByIndex(0)); delay(500); }
Raspberry Pi
Circuit Connection
Program Execution
Python
- Install the gpiozero library
- You can install the library using the following commands:
sudo apt update sudo apt install python3-gpiozero
- For other systems on Raspberry Pi, you can install the library using the following command:
sudo pip3 install gpiozero
- Run the following command to view the GPIO pin definitions on the Raspberry Pi:
pinout
- Related Configuration
- Enable the 1-Wire bus
sudo raspi-config Select Interfaces oOptions -> 1-Wire -> Would you like the one-wire interface to be enabled? Select Yes -> “The one-wire interface is enabled” Select Yes -> Select finish
- Restart the Raspberry Pi system
sudo reboot
- Install device drivers and verify if the device is working
sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices/ ls
- Check the current temperature
cd 28-0000004145bb ls
cat w1_slave
- Download the Raspberry Pi reference example, unzip the file, and copy it to the user directory.
cd raspberrypi/10/python_gpiozero python sensor.py
- You will see the Raspberry Pi correctly running the DS18B20 program, continuously printing the detected temperature values. To exit, press ctrl+C.
- For more commands, please refer to the gpiozero documentation