Terraformer
Posts: 11
Joined: Sat Jun 04, 2016 6:00 pm

AM2320 per I2C with RPi3

Mon Jun 20, 2016 8:04 pm

Hi all,

I bought an AM2320 which uses I2C by default:
https://akizukidenshi.com/download/ds/aosong/AM2320.pdf

However, it does not work out of the box. i2cdetect -y 1 did not show the sensor but only my BMP280. I checked all wirings a dozen times, they are correct. So I read many threads/articles but still can't get it to work. The "solutions" offered did not work with my RPi3. For example, one post suggested that the AM2320 is sleeping and one needs to run two i2cdetect -y 1 right after another--did not work either.

To test that the sensor is working, I ran an I2Cscanner with an Arduino Nano (AM2320 attached per I2C). The AM2320 was found on 0xB8 and 0xB9 (sic!). Note that I could not access the AM2320 using the Arduino.
After that I tested the "Single bus communication (ONE-WIRE)" (SCL <> GND, SDA <> data pin), and it worked.

Now that I know that the sensor is fine, I would like to use the I2C bus of the RPI3 to read the sensor data.
Does anyone has a working AM2320 via I2C configuration or knows a working solution?

Thanks & best,
TF

User avatar
joan
Posts: 14093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: AM2320 per I2C with RPi3

Mon Jun 20, 2016 8:22 pm

Why not just use one wire mode if that works? I don't think the AM2320 has a reputation for working reliably as an I2C device.

fivdi
Posts: 208
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: AM2320 per I2C with RPi3

Mon Jun 20, 2016 10:11 pm

As far as I remember i2cdetect -y 1 uses the SMBus quick command to detect devices on the I2C bus. Does i2cdetect -y -r 1, which uses the SMBus receive byte command to detect device work?

fivdi
Posts: 208
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: AM2320 per I2C with RPi3

Mon Jun 20, 2016 10:22 pm

There are projects on GitHub with code for accessing the AM2320 with I2C. I've never actually used any of them.

https://github.com/Gozem/am2320
https://github.com/dnivanthaka/am2320-raspi

Terraformer
Posts: 11
Joined: Sat Jun 04, 2016 6:00 pm

Re: AM2320 per I2C with RPi3

Tue Jun 21, 2016 6:06 pm

Hi all,

Thank you all for your ideas! I want to free GPIO and, therefore, I thought using I2C devices would be a good move :)

I've tried i2cdetect -y -r 1 but the AM2320 does not show (the BMP280 does).
And I downloaded both AM2320 libraries. The first does not work and I've no idea why:

Code: Select all

Traceback (most recent call last):
  File "./am2320.py", line 86, in <module>
    (t,h) = am2320.readSensor()
  File "./am2320.py", line 46, in readSensor
    posix.write(fd, b'\x03\x00\x04')
OSError: [Errno 5] Input/output error
Package: https://github.com/Gozem/am2320

In addition, I was not able to compile the second package (https://github.com/dnivanthaka/am2320-raspi) due to a missing "pca9685.h". I guess I've identified the missing file but this itself seems to depend on more libs I don't have ... so I'm lost.

Thanks & best,
TF

User avatar
joan
Posts: 14093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: AM2320 per I2C with RPi3

Tue Jun 21, 2016 6:52 pm

It's possible but unlikely that any I2C based solution will work unless i2cdetect can see the device.

i2cdetect not working suggests it is not wired correctly to the Pi.

Terraformer
Posts: 11
Joined: Sat Jun 04, 2016 6:00 pm

Re: AM2320 per I2C with RPi3

Tue Jun 21, 2016 7:32 pm

Hello joan,

Thanks for the feedback. That backs what I read elsewhere.
However, the problem persists when using an Arduino Nano but the device can be detected on 0xB8. How this is possible or what this means, I don't know. As far as I understand it, the problem is that AM2320 uses the address 0xB8 which is beyond the last 0x77 register of the Pi.

Best,
TF

User avatar
joan
Posts: 14093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: AM2320 per I2C with RPi3

Tue Jun 21, 2016 7:53 pm

The AM2320 I2C address is 0x5C (92 decimal).

The Arduino is (incorrectly) showing the address plus the R/W bit.

fivdi
Posts: 208
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: AM2320 per I2C with RPi3

Tue Jun 21, 2016 7:55 pm

0x5c << 1 == 0xb8

0xb8 is 0x5c left shifted one bit. Sometime documentation uses the address left shifted one bit. The least significant bit is then used to distinguish between read and write operations.

The "Input/output error" is the typical error seen when attempting to access a device on the I2C bus but the device can't be found.

Terraformer
Posts: 11
Joined: Sat Jun 04, 2016 6:00 pm

Re: AM2320 per I2C with RPi3

Fri Jun 24, 2016 3:06 pm

Thank you both for the clarification. That's in line with what I read about the issue elsewhere.
It's a pity that the Arduino "I2C scanner" finds something that is not there.

In general, I'm surprised how many outdated code can be found on GitHub and the like. Maybe a consequence of the zeitgeist ...

Best,
TF

frankb4
Posts: 1
Joined: Fri Jun 24, 2016 8:28 pm

Re: AM2320 per I2C with RPi3

Fri Jun 24, 2016 8:31 pm

This just works, both C and python:
Package: https://github.com/Gozem/am2320
i2cdetect does not show any address, as stated above.

User avatar
joan
Posts: 14093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: AM2320 per I2C with RPi3

Fri Jun 24, 2016 9:14 pm

frankb4 wrote:This just works, both C and python:
Package: https://github.com/Gozem/am2320
i2cdetect does not show any address, as stated above.
The OP tried that code and it failed with an I/O error. Presumably that was just a wiring problem.

dna_pi
Posts: 1
Joined: Tue Jul 03, 2012 5:05 pm
Location: Colombo
Contact: Website

Re: AM2320 per I2C with RPi3

Sat Jul 16, 2016 5:32 pm

Terraformer wrote:Hi all,

Thank you all for your ideas! I want to free GPIO and, therefore, I thought using I2C devices would be a good move :)

I've tried i2cdetect -y -r 1 but the AM2320 does not show (the BMP280 does).
And I downloaded both AM2320 libraries. The first does not work and I've no idea why:

Code: Select all

Traceback (most recent call last):
  File "./am2320.py", line 86, in <module>
    (t,h) = am2320.readSensor()
  File "./am2320.py", line 46, in readSensor
    posix.write(fd, b'\x03\x00\x04')
OSError: [Errno 5] Input/output error
Package: https://github.com/Gozem/am2320

In addition, I was not able to compile the second package (https://github.com/dnivanthaka/am2320-raspi) due to a missing "pca9685.h". I guess I've identified the missing file but this itself seems to depend on more libs I don't have ... so I'm lost.

Thanks & best,
TF
I wrote this https://github.com/dnivanthaka/am2320-raspi, This one does not seems to work properly. May be due to a timing problem.

Please use my kernel driver module. (https://github.com/dnivanthaka/linux-am2320-driver) This one is tested and it works, currently it shows the measurements as * 10, You have to read the values and divide the results by 10 :). I will improve the functionality as time goes on. Please do provide me with feedback.
Web: http://www.nivanthaka.com/
GitHub: https://github.com/dnivanthaka

Terraformer
Posts: 11
Joined: Sat Jun 04, 2016 6:00 pm

Re: AM2320 per I2C with RPi3

Mon Jul 18, 2016 12:11 pm

Thank you very much!

maurice1
Posts: 37
Joined: Tue Mar 05, 2013 8:55 am
Location: Dublin

Re: AM2320 per I2C with RPi3

Wed Mar 22, 2017 6:30 pm

Hi Terraferrma
I can replocate your error if I unplug the AM2320.
Otherwise github.com/Gozem/am2320/blob/master/am2320.py copied into a folder under home/pi worked straight away after making executable.
However when I run it
pi@raspberrypi:~ $ python /home/pi/AM2320/am2320.py
23.6 1.0
pi@raspberrypi:~ $ python /home/pi/AM2320/am2320.py
21.4 1.0
pi@raspberrypi:~ $ python /home/pi/AM2320/am2320.py
19.8 1.0

The temperature varies if I hold it, but no matter how much breathing I do into the sensor, I can't get the humidity to change from 1.0.
Does anyone know the range of this humidity sensor?
Can it detect rainy weather vs. dry weather?

maurice1
Posts: 37
Joined: Tue Mar 05, 2013 8:55 am
Location: Dublin

Re: AM2320 per I2C with RPi3

Fri Mar 24, 2017 7:04 pm

Figured it out the humidity part was faulty.
Plugged in new one and readings were too high but after a couple of minutes settled down and gave realistic readings

napierjohn
Posts: 1
Joined: Thu Feb 07, 2019 3:54 am

Re: AM2320 per I2C with RPi3

Thu Feb 07, 2019 6:35 am

I got the AM2320.py to work right off, too. I then made a function that takes the average of 'sr' number of samples where 'sr' is a variable you can alter. I also added a timestamp and the time in seconds it takes to complete the sampling and reporting.

I am surprised how fast it can sample. I decreased the sleep to 0.000001 at it gave results. I think for a reliable reading, though, one should build the list over a longer period of time rather than shorter.

Return to “Troubleshooting”