Unable to get temperature readings from AHT10 Sensor, because it is protected?

Hi, I am having a problem with my AHT10 sensor wired into my arduino uno.

#include <Adafruit_AHTX0.h> // Temperature Sensor
#define aht_i2c_Address 0x3c
Adafruit_AHTX0 aht; // Temperature Sensor

float tempVal;

After initialising the sensor, the following code gives a strange error.

tempVal = aht._temperature;
   float _temperature, ///< Last reading's temperature (C)
         ^~~~~~~~~~~~

exit status 1

Compilation error: 'float Adafruit_AHTX0::_temperature' is protected within this context

Have you tried this code:

#include <Adafruit_AHTX0.h>

Adafruit_AHTX0 aht;

void setup() {
  Serial.begin(115200);
  Serial.println("Adafruit AHT10/AHT20 demo!");

  if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
  Serial.println("AHT10 or AHT20 found");
}

void loop() {
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
  Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");

  delay(500);
}

Taken from Adafruit. Close to what you have.

Ron

I tried this but it seems to be causing the void setup() to repeat itself over and over again. (I can tell because all my void setup() does is initialise a display, and its being initialised over and over again)

Screenshot 2024-06-01 at 10.46.16 AM

I don't have an AHT10 so I couldn't verify the code I posted. Sorry about that :frowning: . Hopefully someone else will come along familiar with the AHT.

Ron

Your Arduino is resetting. Your wires are not making good connections.

Also, remove the underscore from your code. The underscore is used for macros (advanced programming). Just use everyday words.

I just tried the sketch that @Ron_Blain provided with an Uno R3 and an AHT20. It worked as expected.

Monitor port settings:
baudrate=115200
Connected to /dev/ttyUSB0! Press CTRL-C to exit.
Adafruit AHT10/AHT20 demo!
AHT10 or AHT20 found
Temperature: 24.14 degrees C
Humidity: 37.73% rH
Temperature: 24.14 degrees C
Humidity: 37.73% rH
Temperature: 24.14 degrees C
Humidity: 37.73% rH

I'd try your sketch, @SunnyFlops, but you've yet to show it.

You are trying to read an internal class variable which is protected. You need to call some member function to return the value.

1 Like

The wires are very tightly inserted I doubt that's an issue.

There's only Serial.writeln("initialising") in void setup()
There's only the code they provided in void loop()

but it loops through setup.

I will try

This is a reset. Like I said...

You have a wiring problem.

You are wrong.

Well, I've been able to make an AHT20 work. You haven't. And you still haven't shown your complete sketch that you claim fails. I'm not a mind reader, and I'm done waiting. Good luck, and good-bye.

Did you try Ron's code exactly? I ask because Ron's code does not have "Initializing"

Im pretty sure that printing Serial.println isnt going to cause problems...

Again, did you try @Ron_Blain 's code from post #2 ?

You mentioned you tried some code and the display initialized. Ron's code does not have a display.

No, you are wrong. Your wiring is bad. Your code is bad. Your listening is bad.

Follow directions.

It's like deja vu, all over again.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.