📜  raspberry pi temparature (1)

📅  最后修改于: 2023-12-03 15:19:42.658000             🧑  作者: Mango

Raspberry Pi Temperature

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.

Table of Contents
Prerequisites

To follow along with this guide, you will need:

  • A Raspberry Pi board (any model)
  • A microSD card with an operating system installed (e.g., Raspberry Pi OS)
  • Access to a command-line terminal or SSH connection to the Raspberry Pi
Method 1: Utilizing the Built-in Temperature Sensor

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:

  1. Open the command-line terminal on your Raspberry Pi or establish an SSH connection.

  2. Enter the following command to check the CPU temperature:

    cat /sys/class/thermal/thermal_zone0/temp
    
  3. 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.

Method 2: Using External Temperature Sensor

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:

  1. Connect the DS18B20 sensor to the Raspberry Pi using the GPIO pins.

  2. Enable the 1-Wire interface by adding the following line to the /boot/config.txt file:

    dtoverlay=w1-gpio
    
  3. Reboot the Raspberry Pi using the sudo reboot command.

  4. After rebooting, load the necessary kernel modules with the command:

    sudo modprobe w1-gpio && sudo modprobe w1-therm
    
  5. Navigate to the /sys/bus/w1/devices directory:

    cd /sys/bus/w1/devices
    
  6. Locate the folder starting with "28-" (the unique sensor ID) using the ls command:

    ls
    
  7. Change into the respective folder:

    cd 28-xxxx (replace xxxx with your sensor's ID)
    
  8. 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.

Method 3: Monitoring CPU Temperature

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.

Conclusion

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.