It's easy to use the PCT2075 temperature sensor with CircuitPython and the Adafruit CircuitPython PCT2075 module.  This module allows you to easily write Python code that reads the temperature and set an alert to go off when a given temperature is reached or exceeded.

You can use this sensor with any CircuitPython microcontroller board or with a Linux single board computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First you'll need to wire up the sensor for a basic I2C connection. The STEMMA QT connectors give you flexibility with how you choose to make the connections, but the required connections are fairly simple:

  • Board 3V to sensor VCC (red wire)
  • Board GND to sensor GND (black wire)
  • Board SCL to sensor SCL (yellow wire)
  • Board SDA to sensor SDA (blue wire)
Note: This breakout includes pullup resistors on the I2C lines, no external pullups are required.

Python Computer Wiring

Since there are dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi 3V to sensor VCC (red wire)
  • Pi GND to sensor GND (black wire)
  • Pi SCL to sensor SCL (yellow wire)
  • Pi SDA to sensor SDA (blue wire)

CircuitPython Installation of PCT2075 Library

You'll need to install the Adafruit CircuitPython PCT2075 library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_pct2075.mpy
  • adafruit_bus_device
  • adafruit_register

Before continuing make sure your board's lib folder or root filesystem has the adafruit_pct2075.mpy, adafruit_bus_device, and adafruit_register files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of PCT2075 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-pct2075

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the sensor we'll initialize it and read the temperature measurements from the board's Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

import time
import board
import adafruit_pct2075
i2c = board.I2C()

pct = adafruit_pct2075.PCT2075(i2c)

Now you're ready to read values from the sensor using the temperature property:

Temperature Alert Example

This next example will show you how to use the temperature alerting functions to let you know when the measured temperature gets higher than a configurable point.

First, we will have to wire up an LED and current limiting resistor that the sensor will use to alert us about the temperature. This requires just a few more connections than the basic temperature functionality. If you were using the STEMMA QT connector without a breadboard, you'll need to add one so that you have a place to plug in the LED and resistor. 

Wire up the sensor according to the diagram for your type of platform below:

CircuitPython Wiring

  • Connect sensor VCC (red wire) to Feather 3V.
  • Connect sensor GND (black wire) to Feather GND
  • Connect sensor SCL (yellow wire) to Feather SCL
  • Connect sensor SDA (blue wire) to Feather SDA
  • Connect sensor INT to Resistor leg 1
  • Connect LED Cathode to Resistor leg 2
  • LED Anode to Feather 5V

Python Computer Wiring

  • Connect sensor VCC (red wire) to Feather 3V.
  • Connect sensor GND (black wire) to Feather GND
  • Connect sensor SCL (yellow wire) to Feather SCL
  • Connect sensor SDA (blue wire) to Feather SDA
  • Connect sensor INT to Resistor leg 1
  • Connect LED Cathode to Resistor leg 2
  • LED Anode to Feather 5V

Temperature Alert Settings

High temperature threshold is the temperature that the sensor compares to the measured temperature each time it makes a measurement. If the current temperature is greater than or equal to the high temperature threshold, the sensor registers a temperature fault. What happens with that alert depends on the rest of the settings.

Temperature hysteresis is the temperature that defines the bottom of the "active" range of temperature where the sensor sill considers the temperature fault as being active. This temperature must be less than the high temperature threshold.

The fault count is how many consecutive temperature faults are required to raise an alert on the INT pin. In the example code the fault count is set to 4 meaning that four measurements by the sensor in a row must be equal to or greater than the high temperature threshold for the sensor to raise an alert.

Handy Interstitial Diagram

This diagram from the datasheet shows how the different parameters work together. High temperature threshold (here as Tots and temperature hysteresis (Thys) together define a temperature range that specify when the INT pin (OS) responds to, depending on the alert mode.

adafruit_products_temp_alert_ds_diagram.png
From the datasheet: https://www.nxp.com/docs/en/data-sheet/PCT2075.pdf

Alert Modes

The Alert mode dictates how the INT pin will be used to communicate with the user about the state of the temperature monitoring. In Comparator mode like in the example, the sensor acts similar to a thermostat controlling an air conditioning unit. When the sensor raises a high temperature alert according to the high temperature threshold and fault count, the INT pin is connected to ground, completing the LED circuit and lighting the LED. Once the temperature falls below the temperature hysteresis, the sensor puts the INT pin in a high impedance state, effectively disconnecting the LED from ground.

With the sensor in Comparator mode, the sensor can control a cooling mechanism such as a fan by having the INT pin activate a transistor or relay that controls the current flowing to the fan's motor. This way the decision of when to activate the fan is controlled directly by the PCT2075, allowing the microcontroller to focus on other things.

In Interrupt mode the sensor activates the INT pin twice: once when the alert is first raised, and a second time when the temperature falls below the temperature hysteresis. In interrupt mode the INT pin is deactivated by reading from the sensor.

Testing the Code

As in the previous example we start by making the required imports, setting up an I2C bus and creating the pct2075.PCT2075 class instance.

import time
import board
import adafruit_pct2075

i2c = board.I2C()
pct = adafruit_pct2075.PCT2075(i2c)

Next we can set and verify the temperature threshold and hysteresis

pct.high_temperature_threshold = 35.5
print("High temperature threshold: %.2f"%pct.high_temperature_threshold)

pct.temperature_hysteresis = 30.0
print("Temperature hysteresis: %.2f"%pct.temperature_hysteresis)

Finally we set the number of consecutive temperature faults required to raise an alert.

pct.faults_to_alert = adafruit_pct2075.FaultCount.FAULT_4
if pct.faults_to_alert is adafruit_pct2075.FaultCount.FAULT_4:
    print("Faults to alert: 4")

Finally we start taking temperature measurements in a loop. While it runs find a safe method of gently warming the sensor such as a hair dryer or chilled with warm breath and a longer than average attention span (definitely not fire). Once it has been warmed to or above 35.5 C (90.5 F) for 4 measurement cycles the LED will light up

while True:
    print("Temperature: %.2f C"%pct.temperature)
    time.sleep(0.5)

Is it hot in here?

That's all it takes! The PCT2075 is a versatile temperature sensor that you'll find all sorts of uses for.

Don't wonder how hot it is, ask the PCT2075!

Temperature Example Code

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_pct2075

i2c = board.I2C()  # uses board.SCL and board.SDA
pct = adafruit_pct2075.PCT2075(i2c)

pct.high_temperature_threshold = 35.5
pct.temperature_hysteresis = 30.0
pct.high_temp_active_high = False
print("High temp alert active high? %s" % pct.high_temp_active_high)

# Attach an LED with the Cathode to the INT pin and Anode to 3.3V with a current limiting resistor

while True:
    print("Temperature: %.2f C" % pct.temperature)
    time.sleep(0.5)

Temperature Alert Example

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_pct2075

i2c = board.I2C()  # uses board.SCL and board.SDA
pct = adafruit_pct2075.PCT2075(i2c)

while True:
    print("Temperature: %.2f C" % pct.temperature)
    time.sleep(0.5)

This guide was first published on Sep 25, 2019. It was last updated on Sep 25, 2019.

This page (Python & CircuitPython) was last updated on Oct 15, 2021.

Text editor powered by tinymce.