I have successfully attached a BMP085 board to a Raspberry Pi and am logging pressure and temperature to cosm.com. This was fairly easy as I simply followed the instructions.
Then, I added a second I2C device a Honeywell HIH6131 which is a humidity and temperature sensor.
Finally, I wrote code (well maybe hacked is a better description) but it is not working correctly and would like some help.
First let me explain the HIH6131 which resides at the I2C address of 0x27. From the data sheet it does not have registers rather you simply read and write values. So the only write command is a "measurement request" command which causes the device to initiate a measurement. The command is shown in figure 1.
From the data sheet it is "a Measurement Request command consists of the Slave address plus the WRITE bit (0). Once the sensor responds with an acknowledge (ACK), the Master generates a stop condition."
Next I can either read two bytes for a humidity reading or four bytes for a humidity and temperature reading. For this question I will focus on a two byte humidity reading which is shown in figure 2.
From the data sheet "the Master generates a START condition and sends the sensor Slave address followed by a read bit (shown in Figure 2). After the sensor generates an acknowledge (ACK), it will transmit up to four bytes of data – the first two bytes containing the compensated temperature output and the second two bytes containing the optional compensated temperature output.
Now my testing. I am able to confirm that both devices are present as a sudo i2cdetect -y 0 command reveals both the BMP085 at 0x77 and the HIH6131 at 0x27.
Second, I am convinced the wiring of my new HIH6131 device is working because if I remove the clock wire to the HIH6131 I get "Error accessing 0x27: Check your I2C address" which of course is the correct device.
The problem is that I read back the same byte every time. So a two byte read is two bytes of the same value and a four byte read is four. However, they will change in value if I breath hot air on the sensor".
So, I think that tells me that I am issuing the measurement request since the byte is changing and therefore my problem is in reading the data back.
Now I will describe the part of my code where it goes wrong.
The first thing I did is added two commands to Adafruit_I2C.py:
Code: Select all
def write0(self): "Puts only device address on bus with write bit set" try: self.bus.write_quick(self.address) if (self.debug): print("I2C: Cannot access device") except IOError, err: print "Error accessing 0x%02X: Check your I2C address" % self.address return -1 def read0(self): "reads I2C bus" try: result = self.bus.read_byte(self.address) if (self.debug): print "I2C: Device 0x%02X error" % self.address return result except IOError, err: print "Error accessing 0x%02X: Check your I2C address" % self.address return -1
Then I created a new file called Adafruit_HIH6131.py and used the same format as Adafruit_BMP085.py.
I will focus on the two byte humidity read command:
Code: Select all
def readRawHumidity(self): # byte 1 = humidity msb, byte 2 = humidity lsb "Reads the raw (uncompensated) humidity from the sensor" self.i2c.write0() time.sleep(0.500) # allow time for the conversion msb = self.i2c.read0() lsb = self.i2c.read0() raw = (msb << 8) + lsb return raw
Can anyone help me?