am2315 for raspberry pi in python
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- ydna
- Posts: 15
- Joined: Sun Jul 21, 2013 7:47 pm
Re: am2315 for raspberry pi in python
I hooked it up to my Pi and i2cdetect didn't show it was connected. What should I do?
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: am2315 for raspberry pi in python
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.
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.
- ydna
- Posts: 15
- Joined: Sun Jul 21, 2013 7:47 pm
Re: am2315 for raspberry pi in python
What are the pullup resitors for? I didn't use them for my arduino and it worked, perhaps the Pi needs them ?
-
- Posts: 13
- Joined: Wed Dec 25, 2013 4:38 pm
Re: am2315 for raspberry pi in python
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 ?
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 ?
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: am2315 for raspberry pi in python
Yours came with pullup resistors? Mine didn't come with any, but I tried it with pullups anyway - didn't help.ydna wrote:What are the pullup resitors for? I didn't use them for my arduino and it worked, perhaps the Pi needs them ?
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.
-
- Posts: 12151
- Joined: Thu Apr 06, 2006 4:21 pm
Re: am2315 for raspberry pi in python
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
-
- Posts: 13
- Joined: Wed Dec 25, 2013 4:38 pm
Re: am2315 for raspberry pi in python
I've got the same as here :
http://forums.adafruit.com/viewtopic.php?f=19&t=42649
http://forums.adafruit.com/viewtopic.php?f=19&t=42649
I got it detected now. Kind of strange.i.e: You have to issue i2cdetect twice to have it show up on the list of devices.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: am2315 for raspberry pi in python
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.
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.
-
- Posts: 14
- Joined: Sat Jan 18, 2014 5:58 pm
Re: am2315 for raspberry pi in python
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!
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!
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: am2315 for raspberry pi in python
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.
It also repeats steps 1, 2, and 3 before moving on to step 4.
-
- Posts: 14
- Joined: Sat Jan 18, 2014 5:58 pm
Re: am2315 for raspberry pi in python
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)
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
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: am2315 for raspberry pi in python
Use the code tags (at the top of the edit pane) for posting code.
You're not indexing your data correctly:
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
-
- Posts: 14
- Joined: Sat Jan 18, 2014 5:58 pm
Re: am2315 for raspberry pi in python
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>
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>
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: am2315 for raspberry pi in python
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?
-
- Posts: 14
- Joined: Sat Jan 18, 2014 5:58 pm
Re: am2315 for raspberry pi in python
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.
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.
Please be positive and constructive with your questions and comments.