Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By alnitak
#11701 Guys,

Trying to talk to a htu21df i2c board at 0x40. Have flashed the NodeMCU 0.9.5 build 20150213 powered by Lua 5.1.4 and can talk to the machine. Used the great i2c discovery program (elsewhere on this forum) to discover the device at 0x40 and show me the gpio pins for sda and scl. I know tha the htu board is working because I can talk to it using an arduino.

Here's the software library that I wrote:

Code: Select allhtu21df= {
     dev_addr = 0x40,
     id = 0,
     init = function (self, sda, scl)
               self.id = 0
               i2c.setup(self.id, sda, scl, i2c.SLOW)
     end,
     read_reg = function(self, dev_addr, reg_addr)
          i2c.start(self.id)
          i2c.address(self.id, dev_addr ,i2c.TRANSMITTER)
          i2c.write(self.id,reg_addr)
          i2c.stop(self.id)
          i2c.start(self.id)
          i2c.address(self.id, dev_addr,i2c.RECEIVER)
          c=i2c.read(self.id,2)
          i2c.stop(self.id)
          return c
    end,
   

    readTemp = function(self)
          h, l = string.byte(self:read_reg(0x40, 0xE3), 1, 2)
          h1=bit.lshift(h,8)
          rawtemp = bit.band(bit.bor(h1,l),0xfffc)
          temp = ((rawtemp/65536)*175.72)-46.85
          return temp
    end,
    readHum = function(self)
          h,l = string.byte(self:read_reg(0x40,0xE5),1,2)
          h1=bit.lshift(h,8)
          rawhum = bit.band(bit.bor(h1,1),0xfffc)
          hum = ((rawhum/65536)*125.0)-6.0
          return hum
     end
     }


and here's the test code:

Code: Select allrequire('htu21df')                 --call for new created HTU21df Module Driver
          sda=3 --GPIO2               --  declare your I2C interface PIN's
          scl=4 --GPIO0
          htu21df:init(sda, scl)      -- initialize I2C 
          temp = htu21df:readTemp()   -- read temperature
          print(temp)                 -- print it
          hum = htu21df:readHum()
          print(hum)


The problem is that no matter what, i get both h and l bytes set at 255.

I checked the voltage using the lua command and got 3.366 V, so I don't think it is a power problem. The htu board has pullup resistors on it.

Any ideas?
User avatar
By alnitak
#11733 I think it is just the comments that are incorrect. I tried both ways with 0, and 2, so tried it with sda on 0 and then with sda on 2, doesn't work either way.
User avatar
By jankop
#11734
the htu board has pullup resistors on it.


My HTU21 board has pullup too, but they are not connected tu Vcc. The user must connect it.