AM2315 I2C Not working with BBB

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
joshs85
 
Posts: 29
Joined: Fri Sep 13, 2013 2:52 pm

AM2315 I2C Not working with BBB

Post by joshs85 »

I am having some trouble with the AM2315 Temp\Humidity sensor. I have it hooked up to my BeagleBone Black as described below with the addition of a 10k resistor from White to red and another one from yellow to red. When its all hooked up and I run i2cdetect, it loads up to 5B really fast and then it slowly (about 1 per second, in red below) loads each address after that until it gets to the end.. but never shows the AM2315 at 5C. I have tried multiple i2c buses and 4.7k resistors but get the same thing. Any ideas?

Red: 3v3
White: P9_21
Yellow: P9_20
Black: GND

i2cdetect -y -r 2
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

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

Re: AM2315 I2C Not working with BBB

Post by adafruit_support_mike »

If you're using the Adafruit_BBIO library, the default I2C pins are P9_19 for the clock signal, and P9_20 for data.

User avatar
joshs85
 
Posts: 29
Joined: Fri Sep 13, 2013 2:52 pm

Re: AM2315 I2C Not working with BBB

Post by joshs85 »

adafruit_support_mike wrote:If you're using the Adafruit_BBIO library, the default I2C pins are P9_19 for the clock signal, and P9_20 for data.
Yeah, I tried on that bus and i get the same thing.. I know its connected to the right bus because when i do an i2cdetect it slows down at the devices address only if its connected. When i read with the adafruit library (setting the correct bus), it tells me it couldn't find the device. Wire one up and try it.. I bet it won't work. I"m running debian 7.1 btw. I also checked my bus speed. Its set to 100 Khz

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

Re: AM2315 I2C Not working with BBB

Post by adafruit_support_mike »

You're right that the slowdown occurring just when the I2C scan gets to the device address looks too convenient to be a coincidence.

I don't think I have an AM2315 in my briefcase at the moment, but will try it as soon as I have all the pieces in hand.

Do you have any other I2C devices you can connect to the bus to confirm it? If the bus works for two devices but not a third, that's a clue to look at the device. If three devices all fail on the same but, it's a clue to look at the bus.

As another option, have to tried swapping the yellow and white wires? If the device wiring is backwards, your SDA and SCK signals will be connected to the wrong pins. It would be unusual, but we have seen manufacturing errors like that before. The good news is that reversing the connections on an I2C bus doesn't create any risk of damage to the parts. You'll just get unusual bus behavior, which is a general description for what's happening here.

User avatar
joshs85
 
Posts: 29
Joined: Fri Sep 13, 2013 2:52 pm

Re: AM2315 I2C Not working with BBB

Post by joshs85 »

The only other device I have is a 5v rtc. I don't want to damage the bbb though. I have tried swapping the yellow and white wire. When I do that.. and run i2cdetect, It returns all addresses immediately but it doesn't show anything on the bus and when i try to query the device, it just fails. I have two of these here and was able to get the same results with the second one. I just tried switching the resistors to 1k as well but get the same results.

User avatar
joshs85
 
Posts: 29
Joined: Fri Sep 13, 2013 2:52 pm

Re: AM2315 I2C Not working with BBB

Post by joshs85 »

I just realized that this device is 3.5V-5V compatible. I ordered a bi directional level shifter. I"m going to try that out at 5v.

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

Re: AM2315 I2C Not working with BBB

Post by adafruit_support_mike »

That's a good point, but I've also been looking through the datasheet: http://www.adafruit.com/datasheets/AM2315.pdf

The AM2315's humidity sensor can accumulate error through self-heating as current flows through it, so the device automatically shuts down to avoid the problem. That means it doesn't respond like a regular I2C device.

The wake-up process is described on page 14 of the datasheet: You begin by asserting a START condition and clocking out the device address, but the AM2315 will not respond with an ACK. You have to pause the CLK for a period of 800 microseconds to 3 milliseconds, then assert the STOP condition. That will wake the AM2315 up, and then it will respond like a normal I2C device.

`i2cdetect` doesn't know about device-specific quirks like that, so the results you saw probably reflect a glitch in the wake-up process.

User avatar
joshs85
 
Posts: 29
Joined: Fri Sep 13, 2013 2:52 pm

Re: AM2315 I2C Not working with BBB

Post by joshs85 »

How would I go about programming that in python for the bbb? I don't think you can write to it if the device doesn't exist.

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

Re: AM2315 I2C Not working with BBB

Post by adafruit_support_mike »

You'd probably need to control the SDA and SCK lines directly for that part of the transaction.

User avatar
joshs85
 
Posts: 29
Joined: Fri Sep 13, 2013 2:52 pm

Re: AM2315 I2C Not working with BBB

Post by joshs85 »

I finally got this working. I had to add a logic level converter on the line and power it with the 5v supply and then had to use this code below. The wakeup sequence is flakey and it seems to always fail on the first run.. and then works perfectly on the second so there's a loop in there that runs twice.

Code: Select all

import time
import smbus

class am2315():

    def __init__(self):
        self.I2CBUS = 1
        self.AM2315_I2CADDR = 0x5C
        self.AM2315_WAITTIME = .158
        self.MAXTRYS=2
        self.FUNCTION_CODE_READ = 0x03
        self.readBytes = [0x00, 0x04]
    def read(self):
        read_results = None
        i=0
        errcode=0
        hum = 999
        temp = 999
        while i <= self.MAXTRYS:
            bus = smbus.SMBus(self.I2CBUS)
            try:
                bus.write_i2c_block_data(self.AM2315_I2CADDR, 0x00, [0x00])
                bus.write_i2c_block_data(self.AM2315_I2CADDR, self.FUNCTION_CODE_READ, self.readBytes)
                time.sleep(self.AM2315_WAITTIME)
                read_results = bus.read_i2c_block_data(self.AM2315_I2CADDR, 0x00, 8)
                break
            except:
                i = i+1
                time.sleep(2.4)
        if i > self.MAXTRYS:
            errcode = 1
        else:
           s=bytearray(read_results)
           crc = 256*s[7]+s[6]
           t = bytearray([s[0],s[1],s[2],s[3],s[4],s[5]])
           c = self.crc16(t)
           if crc != c:
             errcode = 2
           else:
             hum = (256*s[2]+s[3])/10
             temp = (256*s[4]+s[5])/10
        return temp,hum,errcode
    def crc16(self, char):
         crc = 0xFFFF
         for l in char:
               crc = crc ^ l
               for i in range(1,9):
                   if (crc & 0x01):
                      crc = crc >> 1
                      crc = crc ^ 0xA001
                   else:
                      crc = crc >> 1
         return crc

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

Re: AM2315 I2C Not working with BBB

Post by adafruit_support_mike »

Your sleep time is much longer than it should be. `time.sleep()` takes its argument in seconds, so the value you've chosen is 158 milliseconds. The delay after the wakeup signal should be no longer than 3 milliseconds.

mmm_ny
 
Posts: 12
Joined: Mon Feb 24, 2014 6:35 pm

Re: AM2315 I2C Not working with BBB

Post by mmm_ny »

I am having the same problem. I have connected a tsl2561 to the BBB. I can i2cdetect it and run it fine. I have purchased the am2315 and the bi directional level shifter(bdls for short). I am powering the BBB with an external 5v (not usb) power plug. I have the ground from P9_1 connected to the ground on HV side of the bdls and P9_2 connected to the the ground on the am2315. I have 5v from P9_5 connected to the red of the am2315. I have P9_6 connect to the HV side of the bdls. I have the white line from the am2315 connected to the HV side of the bdls and the corresponding pin across the bdls (lv side) connected to P9_19. I have the yellow line from the am2315 connect to the HV side of the bdls and the corresponding pin across the bdls (lv side) connected to P9_20. I have 5v on the HV side of everything and 3.2v on the lv side for both SCL and SDA. When I try to run i2cdetect it runs very slow and does not detect the am2315 address. I have tried the above python code and get no output for humidity or temperature. Please help. Do I have everything hooked up properly? Why doesn't BBB detect the device? Is there other python code I should try?
Thanks!

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

Re: AM2315 I2C Not working with BBB

Post by adafruit_support_mike »

The AM2315 shuts down automatically every time it takes a reading. If current ran through it continuously, the chip would warm up slightly, changing the humdity near the sensor.

You have to send a read command to make the AM2315 wake up and take a reading, then send another read command within 2 seconds to get the actual value. The AM2315 won't respond to the first read command, so you have to trust the hardware to do what it should.

mmm_ny
 
Posts: 12
Joined: Mon Feb 24, 2014 6:35 pm

Re: AM2315 I2C Not working with BBB

Post by mmm_ny »

Thank you...that explains how the hardware works more clearly. I have changed the time.sleep() values in the above code (.158 to .003 and 2.4 to 1)to reflect the timing and I still can't get any output other than (999,999,1) which means it failed. Am I at a dead end?

I need to read temperature and humidity using the BBB. I've tried to get dht22 working but never could. I've now tried the i2c approach and it appears that isn't going to work. I do have your 1wire temperature sensor working on the BBB- but it lacks the humidity sensor. The analog output sensors in the market seem really expensive and most put out 0 to 10 or 0 to 5v - BBB would not be happy even with the voltage divider.

Any suggestions or change of direction to get temperature & humidity working on the BBB would greatly be appreciated. Thanks!

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

Re: AM2315 I2C Not working with BBB

Post by adafruit_support_mike »

The code about three posts up seems to have worked for josh85.

You can expect the first attempt at a reading to fail, because the AM2315 needs a wake-up signal. The remaining code seems to have worked though.

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

Return to “Beagle Bone & Adafruit Beagle Bone products”