How to Connect Temperature Sensor with Raspberry Pi?

temperature sensor

Users can utilize Raspberry Pi, a powerful and low-cost computer, for various projects such as home automation, robotics, and Internet of Things (IoT). One of the most popular uses of Raspberry Pi is in environmental monitoring systems that require temperature sensing.


Temperature sensing is one of the most common applications of Raspberry Pi. By connecting a temperature sensor with Raspberry Pi, you can monitor the temperature of your home, office, or any other environment.

In this article, we will discuss how to connect it with Raspberry Pi and read temperature data using Python. We will also cover the different types of temperature sensors available and how to choose the right sensor for your project.

What is a Temperature Sensor?

it is a device that measures temperature and converts it into a readable format. There are various types of temperature sensors available, such as thermocouples, resistance temperature detectors (RTDs), thermistors, and integrated circuit (IC) sensors.

Types of Temperature Sensors

Thermocouples are the most commonly used temperature sensors in industrial applications due to their high accuracy and wide temperature range. RTDs are used in applications that require high accuracy and stability. Thermistors are the most affordable temperature sensors and are commonly used in consumer electronics.

Choosing a Temperature Sensor for Raspberry Pi

When choosing it for Raspberry Pi, you need to consider the accuracy, temperature range, and cost. You also need to ensure that the sensor is compatible with Raspberry Pi.

The most commonly used temperature sensors for Raspberry Pi are the DS18B20 and DHT11 sensors. The DS18B20 sensor is a digital temperature sensor that has a wide temperature range and high accuracy. The DHT11 sensor is a digital temperature and humidity sensor that is affordable and easy to use.

Components Required

  • Raspberry Pi (any model)
  • Temperature sensor (DS18B20 or DHT11)
  • Breadboard
  • Jumper wires

Step-by-Step Instructions

In this section, we will discuss how to connect it with Raspberry Pi and read temperature data using Python.

Connecting the Temperature Sensor

  1. Connect the VCC pin of the sensor to the 3.3V pin of Raspberry Pi.
  2. Connect the GND pin of the sensor to the GND pin of Raspberry Pi.
  3. Connect the data pin of the sensor to GPIO4 (pin 7) of Raspberry Pi.

Installing Required Libraries

Open the Terminal on Raspberry Pi.

Type the following command to update the Raspberry Pi:

sudo apt-get update
sudo apt-get install python3-w1thermsensor python3-pip

Writing Python Code to Read Temperature Data

from w1thermsensor import W1ThermSensor

sensor = W1ThermSensor()

temperature = sensor.get_temperature()

print("Temperature:", temperature)

Save the file by pressing CTRL + X, then Y, and then ENTER.

Run the file using the following command:


This will display the temperature in Celsius.

Congratulations! You have successfully connected it with Raspberry Pi and read temperature data using Python.


Temperature sensing is a common application of Raspberry Pi and can be used in various projects, including environmental monitoring systems, home automation, and IoT.

Follow Us on

Read More

Thank You

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *