📅  最后修改于: 2023-12-03 15:19:42.658000             🧑  作者: Mango
Raspberry Pi is a popular single-board computer that allows you to run Linux-based operating systems and perform various tasks, including monitoring temperature. In this guide, we will explore how to measure temperature using Raspberry Pi and discuss different ways to achieve it.
To follow along with this guide, you will need:
Some Raspberry Pi models have a built-in temperature sensor that can be accessed using the /sys
file system. Here are the steps to read the temperature:
Open the command-line terminal on your Raspberry Pi or establish an SSH connection.
Enter the following command to check the CPU temperature:
cat /sys/class/thermal/thermal_zone0/temp
The output is the temperature in millidegrees Celsius (°C). To convert it to degrees Celsius, divide it by 1000:
awk '{printf("%.1f\n", $1/1000)}' /sys/class/thermal/thermal_zone0/temp
By executing these commands, you can retrieve the current CPU temperature of your Raspberry Pi.
If your Raspberry Pi model does not have a built-in temperature sensor or you want more accurate results, you can connect an external temperature sensor. Some popular options include the DS18B20 digital temperature sensor. Follow these steps:
Connect the DS18B20 sensor to the Raspberry Pi using the GPIO pins.
Enable the 1-Wire interface by adding the following line to the /boot/config.txt
file:
dtoverlay=w1-gpio
Reboot the Raspberry Pi using the sudo reboot
command.
After rebooting, load the necessary kernel modules with the command:
sudo modprobe w1-gpio && sudo modprobe w1-therm
Navigate to the /sys/bus/w1/devices
directory:
cd /sys/bus/w1/devices
Locate the folder starting with "28-" (the unique sensor ID) using the ls
command:
ls
Change into the respective folder:
cd 28-xxxx (replace xxxx with your sensor's ID)
Read the temperature using the cat
command:
cat w1_slave
This method allows you to interface with various temperature sensors and obtain accurate temperature readings.
Besides monitoring the temperature manually, you can use programming languages like Python to continuously monitor the CPU temperature and implement customized actions. Here's a simple Python script to get you started:
import os
def get_cpu_temperature():
res = os.popen('vcgencmd measure_temp').readline()
temperature = float(res.replace("temp=","").replace("'C\n",""))
return temperature
while True:
print("CPU Temperature: {0} °C".format(get_cpu_temperature()))
Save the above code in a Python file (e.g., temperature_monitor.py
) on your Raspberry Pi. Execute it using the python temperature_monitor.py
command to continuously monitor the CPU temperature.
In this guide, we explored different methods to measure temperature using a Raspberry Pi. Whether you utilize the built-in temperature sensor, connect an external sensor, or monitor the CPU temperature, Raspberry Pi provides versatile options for temperature monitoring. Choose the method that suits your needs and start exploring the world of temperature sensing with Raspberry Pi!
Note: Make sure to provide adequate cooling measures to prevent overheating when monitoring temperature for extended periods.