AHT20 always returning just the status code never sensor readings.

The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and developing countries.

Post Reply
User avatar
/RaspberryPi
Corporate
Posts: 2449
Joined: Wed Jun 05, 2019 1:29 am

AHT20 always returning just the status code never sensor readings.

Post by /RaspberryPi »


So basically whenever I tried to read it with wiringPiI2CRead it always just returns the status code (either 24 or 151). I have tried just using read() but it didn't work at all. I also tried setting the read and write mode manually as per the spec (I'm not sure if wiringPi does this). With or without setting the r/w bit it didn't work. Also it is being detected, I checked with i2cdetect What could cause this? thanks in advance!
bool IsBusy(){ unsigned char buf; wiringPiI2CWrite(fd, 0x71); read(fd, &buf, 1); printf("DEBUG: current status code is %i, checking if busy.\n", buf); return ((buf & BUSY_MASK) == BUSY_MASK); } void Calibrate(){ wiringPiI2CWrite(fd, 0xbe); wiringPiI2CWrite(fd, 0x08); wiringPiI2CWrite(fd, 0x00); } bool IsCalibrated(){ unsigned char buf; wiringPiI2CWrite(fd, 0x71); read(fd, &buf, 1); printf("DEBUG: current status code is %i, checking if calibrated.\n", buf); return ((buf & CALIBRATED_MASK) == CALIBRATED_MASK); } void Measure(){ if(!IsBusy()){ wiringPiI2CWrite(fd, 0xac); wiringPiI2CWrite(fd, 0x33); wiringPiI2CWrite(fd, 0x00); } usleep(1000*75); } wiringPiSetup(); // move somewhere esle fd = wiringPiI2CSetup(DEVICE_ID); if(fd == -1){ printf("ERROR: Failed to communicate with AHT20(temp. and humidity sensor).\n"); return; } usleep(1000*40); //whatever the spec say unsigned char attempts = 1; while(!IsCalibrated()){ printf("Attempting to calibrate AHT20, attempt number %i (if it overflows it's fucked(maybe))\n", attempts); Calibrate(); attempts++; } printf("INFO: Successfuly established feed with AHT20 (that doesn't mean it works though lol). fd: %i\n", fd); //Inside a function so don't mind the fact it's not in main while (IsBusy()) { usleep(80); } Measure(); while (IsBusy()) { usleep(80); } char buf[6]; read(fd, buf, 6); for(int i = 0; i < 6; i++){ printf("%i\n", buf); } submitted by /u/LotosProgramer
[link] [comments]

Source: https://www.reddit.com/r/raspberry_pi/c ... ode_never/
/RaspberryPi
Post Reply

Return to “Raspberry Pi Forum”