Rich's Wordpress

又一个WordPress站点

Temperature Sensor

DS18B20 is a commonly used digital temperature sensor, its output is a digital signal, small size, strong anti-interference ability, and high precision.

It can measures temperatures from -55°C to +125°C (-67°F to +257°F) with an accuracy of ±0.5°C

GND: Ground

DQ: Data input and output

VDD: Digital power supply

The Raspberry PI is connected to the DS18B20 sensor through pin 17. The Raspberry PI can use the w1thermsensor library module, with which I can easily get data from temperature sensor data by programming in Python.

Download w1thermsensor Library

To install the library, type the following command in the terminal in Raspberry Pi

sudo apt-get install python3-w1thermsensor

Then type this command to open the configuration

sudo nano /boot/config.txt

Add a new line at the end of the open file that reads:

dtoverlay=w1-gpio-pullup,gpiopin=17

Then press control+x to leave and press Y to save. Finally, restart the Raspberry Pi. This step enables the one-wire interface that is needed.

Flowchart

Code

from w1thermsensor import W1ThermSensor

from luma.core.render import canvas
from luma.oled.device import ssd1306
from time import sleep

device = ssd1306(port=1, address=0x3C)

DS18B20 = W1ThermSensor()

while True:
	temperature = DS18B20.get_temperature()
	
	with canvas(device) as draw:
		draw.text((0,0), "Temperature test:", fill = "white")
		draw.text((0,20), '%.2f'%temperature+' C', fill = "white")
	
	print('%.2f'%temperature)
	
	sleep(2)

Result

Temperature Sensor
Scroll to top