Re: am2315 for raspberry pi in python

by ydna on Sun Feb 09, 2014 10:55 am

I hooked it up to my Pi and i2cdetect didn't show it was connected. What should I do?
ydna
 
Posts: 15
Joined: Sun Jul 21, 2013 6:47 pm

Re: am2315 for raspberry pi in python

by adafruit_support_rick on Sun Feb 09, 2014 11:40 am

Good question. Mine is doing the same thing - I just got it yesterday, and this is the first chance I've had to play with it.
My sensor is working on Arduino, but it's not showing up on the Pi. At this point, I don't know what the problem is.
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: am2315 for raspberry pi in python

by ydna on Sun Feb 09, 2014 1:35 pm

What are the pullup resitors for? I didn't use them for my arduino and it worked, perhaps the Pi needs them ? :?:
ydna
 
Posts: 15
Joined: Sun Jul 21, 2013 6:47 pm

Re: am2315 for raspberry pi in python

by Vince8134 on Sun Feb 09, 2014 1:47 pm

Mine doesn't show up either among my i2c devices.

I connected 3.3 and 5V to the HV and LV of the i2c level shifter.
The SDA and SCLof the Pi on the A1 and A2. SDA and SCL of the sensor on the B1 and B2.
GND of the Pi on the GND of the left (A side), GND of the sensor on the GND of the right (B side).

But i2cdetect is missing it, whereas I got all my other i2c devices detected :(.
What can I do ?
Vince8134
 
Posts: 8
Joined: Wed Dec 25, 2013 3:38 pm

Re: am2315 for raspberry pi in python

by adafruit_support_rick on Mon Feb 10, 2014 8:51 am

ydna wrote:What are the pullup resitors for? I didn't use them for my arduino and it worked, perhaps the Pi needs them ?

Yours came with pullup resistors? Mine didn't come with any, but I tried it with pullups anyway - didn't help.
The Pi already has weak pullups on SDA and SCL. The Arduino has no pullups on those lines and it works, so the am2315 must already have internal pullups.

I'm stumped.
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: am2315 for raspberry pi in python

by adafruit on Mon Feb 10, 2014 12:36 pm

The AM2315 is pretty weird, it doesn't use standard I2C command styles and it may not be true I2C. not sure it works with the Pi at all :(
User avatar
adafruit
 
Posts: 11508
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: am2315 for raspberry pi in python

by Vince8134 on Mon Feb 10, 2014 3:01 pm

I've got the same as here :
http://forums.adafruit.com/viewtopic.php?f=19&t=42649
i.e: You have to issue i2cdetect twice to have it show up on the list of devices.


I got it detected now. Kind of strange.
Vince8134
 
Posts: 8
Joined: Wed Dec 25, 2013 3:38 pm

Re: am2315 for raspberry pi in python

by adafruit_support_rick on Mon Feb 10, 2014 5:51 pm

Aha! Yes, that gets mine to show up as well.
The Arduino library essentially does the same thing - it issues all commands twice. I have my python code doing that, but it blows up on the first command because the am2315 doesn't respond to it.
I may have to hack the Adafruit_I2C library to do the double-bang thing, so that it doesn't throw an error.
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: am2315 for raspberry pi in python

by NerdWorld on Tue Feb 11, 2014 2:48 pm

Now that I can communicate with the AM2315, I'm having a little trouble understanding the control logic for the sensor. This is what I __think__ is needed, based on my decifering the arduino code. Could somebody please confirm this?

To read the humidity & temperature, you must:

1. Write 0x03 into register 00 (at SMB addddress 0x5c) to signify that a read is desired
2. Write 00 into register 01
3. Write 04 into register 02 (this tells the sensor to return 4 bytes of data)
4. Read register 00 (at SMB address 0x5c). If it does not match 0x03, then try again, since the data is not valid
5. Read register 01. It should be 04 to indicate 4 bytes returned. if not try again since the data is not valid
6. Read register 02 for Humidity high byte (unsigned integer)
7. Read register 03 for Humidity low byte
8. Read register 04 for Temperature high byte (signed interger)
9. Read register 05 for Temperature low byte
10. Divide temperature by 10 to get correct scale

Is this correct?

The docs for the AM2315 seem to show that register 0 & 1 are for humidity and 2 & 3 are for temperature. This seems to differ from what I think the arduino code is doing.


Could somebody confirm the logic for me? TIA!
NerdWorld
 
Posts: 14
Joined: Sat Jan 18, 2014 4:58 pm

Re: am2315 for raspberry pi in python

by adafruit_support_rick on Tue Feb 11, 2014 7:08 pm

The logic looks right. The Arduino code also does a divide by 10 on the humidity, I believe.
It also repeats steps 1, 2, and 3 before moving on to step 4.
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: am2315 for raspberry pi in python

by NerdWorld on Thu Feb 13, 2014 2:59 pm

My logic might be correct, but my code stinks <grin>.

Here's what I've tried to do to get data from the AM2315. I'm getting data, but it's not correct. Can you help me spot what I'm doing wrong?


(BTW, the forum is wrecking my indentation, so please assume standard Pyhton indents)
Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# AM2315.py
#
#
#
#  A test program to debug getting data from a AM2315 Temperature/Humidity sensor on the Raspberry Pi
#
#   

# Load needed python modules
import smbus   # Be able to read the SM Bus (i2c)  (apt-get install python-smbus)
import time   # Needed for sleep routine

BusNumber   = smbus.SMBus(1)   
BusAddress   = 0x5c   

TrialNumber = 0
MaxTrials    = 3

while TrialNumber <= MaxTrials:
   #
   #
   try:
      # Start the measurement process by sending 0x03 to the device at register zero & asking for 4 bytes back
      BusNumber.write_i2c_block_data(BusAddress, 0, [0x03, 0, 4])
      #
      #            
      # Allow for the measurement to be conducted by the sensor
      time.sleep(.15)
      #
      # Now read the data
      SensorData = BusNumber.read_i2c_block_data(BusAddress, 0, 8)
      #
      break
      #
      #
   except:
      #
      TrialNumber = TrialNumber + 1
      #
      #
      #
#
#

print SensorData

Humidity       = float(SensorData[3]*256 + SensorData[3])/10
TemperatureC   = float(SensorData[5]*256 + SensorData[4])/10
TemperatureF    = (TemperatureC * 1.8) + 32

print Humidity
print TemperatureF
NerdWorld
 
Posts: 14
Joined: Sat Jan 18, 2014 4:58 pm

Re: am2315 for raspberry pi in python

by adafruit_support_rick on Thu Feb 13, 2014 3:20 pm

Use the code tags (at the top of the edit pane) for posting code.

You're not indexing your data correctly:
Code: Select all
Humidity       = float(SensorData[2]*256 + SensorData[3])/10
TemperatureC   = float(SensorData[4]*256 + SensorData[5])/10
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: am2315 for raspberry pi in python

by NerdWorld on Thu Feb 13, 2014 5:05 pm

Doh! You're correct about my indexes, but the data I'm getting back still isn't correct.

Here's the data I ALWAYS get.

[0, 5, 128, 115, 48, 0, 0, 0]


The first character should be 0x03
The second character should be 4
The last 2 should be a checksum.

I'm doing somethin' wrong somewhere. <sigh>
NerdWorld
 
Posts: 14
Joined: Sat Jan 18, 2014 4:58 pm

Re: am2315 for raspberry pi in python

by adafruit_support_rick on Thu Feb 13, 2014 7:07 pm

You're going to do four read attempts. Is that what you get on every attempt, or just what you get on the fourth attempt?
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: am2315 for raspberry pi in python

by NerdWorld on Fri Feb 14, 2014 10:09 am

Every read attempt (that doesn't error out) yields the same 8 bytes.

I figure I am having one (or both) of these 2 problems;

1. I'm not initializing the AM2315 properly due to improper use of the i2c write command, so I'm gettng default data during the read operation
2. I'm not reading the registers properly due to improper use of the i2c read command, so I'm picking up some garbage data.
NerdWorld
 
Posts: 14
Joined: Sat Jan 18, 2014 4:58 pm