esp8266 / Arduino

ESP8266 core for Arduino
GNU Lesser General Public License v2.1
15.97k stars 13.34k forks source link

I2C error #698

Closed petrd closed 8 years ago

petrd commented 9 years ago

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 commented 9 years ago

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

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 https://github.com/esp8266/Arduino/issues/698.

petrd commented 9 years ago

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

petrd commented 9 years ago

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

igrr commented 9 years ago

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 https://github.com/esp8266/Arduino/issues/698#issuecomment-131577357.

petrd commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

igrr commented 9 years ago

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 https://github.com/esp8266/Arduino/issues/698#issuecomment-131952114.

petrd commented 9 years ago

OK, Thank you very much.