AHT20 show temperature/humidity in the GUI gauges

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
AAW172
 
Posts: 2
Joined: Wed May 24, 2023 2:59 am

AHT20 show temperature/humidity in the GUI gauges

Post by AAW172 »

Hello,

Currently, I am working on a code to show the temperature and humidity in my Gui which will be imported to the main code. at first, I followed the tutorial on how to use the aht20: https://learn.adafruit.com/adafruit-aht ... cuitpython.
The example code is working well but my own code has a lot of errors.

Many thanks!

Code: Select all

import time
import board
import adafruit_ahtx0

class Temperature_Sensor:
    def __init__(self):

        i2c = board.I2C()
        self.sensor = adafruit_ahtx0.AHTx0(i2c)

    def run(self):
        while True:
            print("\nTemperature: %0.1f C" % self.read_temperature())
            print("Humidity: %0.1f %%" % self.read_humidity())

            # Code for temperature gauges...
            temperature_top = self.read_temperature()
            temperature_bottom = self.read_temperature()

            # Code for humidity gauges...
            humidity_top = self.read_humidity()
            humidity_bottom = self.read_humidity()

            time.sleep(2)

    def read_temperature(self):
        return self.sensor.temperature

    def read_humidity(self):
        return self.sensor.relative_humidity

# Creates an instance of the Temperature_Sensor class
sensor = Temperature_Sensor()

# Starts reading temperature and humidity
sensor.run()

[errors from this code]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/adafruit_bus_device/i2c_device.py", line 175, in __probe_for_device
    self.i2c.writeto(self.device_address, b"")
  File "/usr/local/lib/python3.9/dist-packages/busio.py", line 196, in writeto
    return self._i2c.writeto(address, buffer, stop=stop)
  File "/usr/local/lib/python3.9/dist-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 52, in writeto
    self._i2c_bus.write_bytes(address, buffer[start:end])
  File "/usr/local/lib/python3.9/dist-packages/Adafruit_PureIO/smbus.py", line 320, in write_bytes
    self._device.write(buf)
TimeoutError: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/adafruit_bus_device/i2c_device.py", line 181, in __probe_for_device
    self.i2c.readfrom_into(self.device_address, result)
  File "/usr/local/lib/python3.9/dist-packages/busio.py", line 186, in readfrom_into
    return self._i2c.readfrom_into(address, buffer, stop=stop)
  File "/usr/local/lib/python3.9/dist-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 59, in readfrom_into
    readin = self._i2c_bus.read_bytes(address, end - start)
  File "/usr/local/lib/python3.9/dist-packages/Adafruit_PureIO/smbus.py", line 187, in read_bytes
    return self._device.read(number)
TimeoutError: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/Fiberneering/Desktop/rpi files GUI/temperature_Sense_FIB.py", line 35, in <module>
    sensor = Temperature_Sensor()
  File "/home/Fiberneering/Desktop/rpi files GUI/temperature_Sense_FIB.py", line 10, in __init__
    self.sensor = adafruit_ahtx0.AHTx0(i2c)
  File "/usr/local/lib/python3.9/dist-packages/adafruit_ahtx0.py", line 93, in __init__
    self.i2c_device = I2CDevice(i2c_bus, address)
  File "/usr/local/lib/python3.9/dist-packages/adafruit_bus_device/i2c_device.py", line 62, in __init__
    self.__probe_for_device()
  File "/usr/local/lib/python3.9/dist-packages/adafruit_bus_device/i2c_device.py", line 184, in __probe_for_device
    raise ValueError("No I2C device at address: 0x%x" % self.device_address)
ValueError: No I2C device at address: 0x38
Attachments
This is how the AHT20 is connected to the raspberry pi. It's connected to the 3,3V Pin.
This is how the AHT20 is connected to the raspberry pi. It's connected to the 3,3V Pin.
IMG_Raspberry_Pi_connection_AHT20-1290x960.jpg (984.57 KiB) Viewed 76 times

User avatar
AAW172
 
Posts: 2
Joined: Wed May 24, 2023 2:59 am

Re: AHT20 show temperature/humidity in the GUI gauges

Post by AAW172 »

small update.

I fixed a lot of problems within the code.
but for some reason i can't get the temp_reading_top defined and show me some digits.

Code: Select all

        # Code for temperature gauges...
        temp_reading_top = temperature_Sens.read_temperature()
        temp_reading_bottom = temperature_Sens.read_temperature()

        # Code for humidity gauges...
        humid_reading_top = temperature_Sens.read_humidity()
        humid_reading_bottom = temperature_Sens.read_humidity()

        #code for temperature gauges
        self.meter1 = Meter(self, radius=350, start=0, end=80, border_width=0, fg="#242424", bg="#242424",
               text_color="white", start_angle=270, end_angle=-270,
               text_font="DS-Digital 12", scale_color="white", needle_color="red", text=" \N{DEGREE SIGN}C")
        self.meter1.set_mark(60, 80) # set red marking from 60 to 80
        self.meter1.grid(row=3, column=0, padx=20, pady=10, rowspan=5)
        self.meter1.set(temp_reading_top)

        self.label3 = customtkinter.CTkLabel(self, text="Temperatures")
        self.label3.grid(row=2, column=0, padx=20)
        
this code should show me the gauges through a import i made.
[code]
class Temperature_Sensor:
    def __init__(self):
        i2c = board.I2C()
        self.sensor = adafruit_ahtx0.AHTx0(i2c)
        self.temperature_top = 0
        self.temperature_bottom = 0
        self.humidity_top = 0
        self.humidity_bottom = 0

    def run(self):
        while True:
            print("\nTemperature: %0.1f C" % self.read_temperature())
            print("Humidity: %0.1f %%" % self.read_humidity())

            # Code for temperature gauges...
            temp_reading_top = self.read_temperature()
            
            # Code for humidity gauges...
            humid_reading_top = self.read_humidity()
            
            time.sleep(2)

    def read_temperature(self):
        return self.sensor.temperature

    def read_humidity(self):
        return self.sensor.relative_humidity

# Create an instance of the Temperature_Sensor class
sensor = Temperature_Sensor()

# Create a thread for temperature readings
thread = threading.Thread(target=sensor.run)
thread.daemon = True
thread.start()

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: AHT20 show temperature/humidity in the GUI gauges

Post by adafruit_support_mike »

I don't see any code in the object's run loop that sets the meter.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”