Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2C error #698

Closed
petrd opened this issue Aug 16, 2015 · 12 comments
Closed

I2C error #698

petrd opened this issue Aug 16, 2015 · 12 comments

Comments

@petrd
Copy link

petrd commented Aug 16, 2015

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);
}
@igrr
Copy link
Member

igrr commented Aug 16, 2015

Which pins on Nodemcu board are you using? You know that silkscreen labels
do not correspond to GPIO number right?

On Sun, Aug 16, 2015, 18:40 petrd notifications@github.com wrote:

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);

}


Reply to this email directly or view it on GitHub
#698.

@petrd
Copy link
Author

petrd commented Aug 16, 2015

Hello,
I use D1 and D2, it should correspond GPIO5 and GPIO4.

@petrd
Copy link
Author

petrd commented Aug 16, 2015

I tried also GPIO12 and GPIO14, the result is the same.

@igrr
Copy link
Member

igrr commented Aug 16, 2015

Ok, I don't have these sensors at hand, please attach oscilloscope traces
so that we can figure out what is the cause of this issue.

On Sun, Aug 16, 2015, 19:05 petrd notifications@github.com wrote:

Hello,
I use D1 and D2, it should correspond GPIO5 and GPIO4.


Reply to this email directly or view it on GitHub
#698 (comment).

@petrd
Copy link
Author

petrd commented Aug 17, 2015

Hello,
so for me does not work Wire.write ()

First shot of the Arduino UNO + AM2321:
1
1d

The following is a picture ESP-12E + AM2321:
2
2d

@igrr
Copy link
Member

igrr commented Aug 17, 2015

I can't zoom the traces enough to see the pulses, but from the log it looks like the difference is that ESP doesn't generate STOP condition after a NACK.

@igrr
Copy link
Member

igrr commented Aug 17, 2015

You can try to edit twi_writeTo function in core_esp8266_si2c.c. Make it call twi_write_stop on errors (i.e. before return 2 statement).

@petrd
Copy link
Author

petrd commented Aug 17, 2015

Sending an enlarged graph which corresponds to the following code:


        //
        // 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();

Arduino Uno:
Wire.beginTransmission(0xB8);
Wire.endTransmission();
Wire.beginTransmission(0xB8);
Wire.write(0x03);
Wire.write(0x00);
Wire.write(0x04);
Wire.endTransmission();

screenshot 2015-08-17 21 03 53

screenshot 2015-08-17 21 04 29

Arduino ESP-12E:
Wire.beginTransmission(0xB8);
Wire.endTransmission();
Wire.beginTransmission(0xB8);
Wire.write(0x03); // Error, Failure to send
Wire.write(0x00); // Error, Failure to send
Wire.write(0x04); // Error, Failure to send
Wire.endTransmission();

screenshot 2015-08-17 21 05 45

screenshot 2015-08-17 21 06 07

@igrr
Copy link
Member

igrr commented Aug 17, 2015

Okay, graphs show that ESP doesn't generate a STOP condition after the first NACK. Please try the edit suggested above — it might resolve the issue.

@petrd
Copy link
Author

petrd commented Aug 17, 2015

Hello,
I edited core_esp8266_si2c.c and AM2321 works.
Thank you very much for your help.

@igrr
Copy link
Member

igrr commented Aug 17, 2015

That's great! Could you please post the diff or the changed code for the
record? (just to be sure I won't mess up anything when doing this change).

On Mon, Aug 17, 2015, 23:29 petrd notifications@github.com wrote:

Hello,
I edited core_esp8266_si2c.c and AM2321 works.
Thank you very much for your help.


Reply to this email directly or view it on GitHub
#698 (comment).

@petrd
Copy link
Author

petrd commented Aug 17, 2015

OK, Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants