Skip to content

I2C error #698

Closed
Closed
@petrd

Description

@petrd

Hello,
I tried to connect ESP-12E I2C humidity and temperature sensor AM2321. I2C communication is not working. I tried it on Modemcu 0.9 (ESP-12) and on Modemcu 1.0 (ESP-12E). The same code works with the Arduino Due. I reduced the pull-up resistors from 10k to 2k2. Also it is not working. I tried HTU21D, it is similar to the humidity and temperature sensor. Also not working.
I do not know what I'm doing wrong, but it looks like it is not working I2C?

Configuration:
SDA = GPIO4
SCL = GPIO5
Pull-up resistors 2x10K or 2x2k2
Version: 1.6.5-1044-g170995a

Code:


#include <Wire.h>

#define I2C_ADDR_AM2321 (0xB8 >> 1)
#define PARAM_AM2321_READ 0x03
#define REG_AM2321_HUMIDITY_MSB 0x00
#define REG_AM2321_HUMIDITY_LSB 0x01
#define REG_AM2321_TEMPERATURE_MSB 0x02
#define REG_AM2321_TEMPERATURE_LSB 0x03
#define REG_AM2321_DEVICE_ID_BIT_24_31 0x0B

void setup() {
  Serial.begin(9600);
  //Wire.pins(4,5); // SDA, SCL  
  //Wire.setClock(100000);
  Wire.begin(4, 5);
}

void loop() {

        //
        // Wakeup
        //
        Wire.beginTransmission(I2C_ADDR_AM2321);
        Wire.endTransmission();

        //
        // Read Command
        //
        Wire.beginTransmission(I2C_ADDR_AM2321);
        Wire.write(PARAM_AM2321_READ);
        Wire.write(REG_AM2321_HUMIDITY_MSB);
        Wire.write(4);
        Wire.endTransmission();

        //
        // Waiting
        //
        //delayMicroseconds(1600); //>1.5ms
        delay(2);
        //
        // Read
        //
        Wire.requestFrom(I2C_ADDR_AM2321, 8); // COMMAND + REGCOUNT + DATA + CRCLSB + CRCMSB
        //int i = 0;
        uint8_t buf[6];
        for (int i = 0; i < 6; ++i)
            buf[i] = Wire.read();

        unsigned short crc = 0;
        crc  = Wire.read();     //CRC LSB
        crc |= Wire.read() << 8;//CRC MSB

       // if (crc == crc16(buf, i))
       //     return true;
       // return false;

        unsigned int humidity;
        int temperature;
        humidity     = buf[2] << 8;
        humidity    += buf[3];
        temperature  = buf[4] << 8;
        temperature += buf[5];

        Serial.print("Temperature = ");
        Serial.print(temperature);
        Serial.print("\t Humidity = ");
        Serial.println(humidity);
        delay(3000);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions