2

Previously I had pined the temp/hum sensor HDC1080 with an Arduino using this sketch and everything worked fine. Now I want to run it over an ESP-12F, I uploaded it but it has issues at the time of establishing I2C connection. Basically, I'm not sure if at the end of setup() the I2C connection has actually begun, because I get this at the serial monitor:

serial_monitor

As far as I see, the code skips everything related to Wire library. I blame it on the SDA/SCL pins, but actually none of the pins are working for me. I found that default I2C GPIO's are 2 and 14, I tried with them and got no result; later I tried with 12/16, as the commented Wire.begin line says, but it was the same. I even read some possible solutions in this post but I not even have the mentioned libraries.

Could you give me any suggestion, please? This is the sketch I'm using:

#include <Wire.h>

#define HDC 0x40 //B1000000 MSB
#define cociente 65536 //2^16

uint16_t humrawvalue;  //Raw data value, MS byte = valmsb, LS byte = vallsb
uint16_t temrawvalue;  //Raw data value, MS byte = valmsb, LS byte = vallsb
uint8_t humvalmsb, humvallsb;  //Raw 8-bit byte of humidity
uint8_t temvalmsb, temvallsb;  //Raw 8-bit byte of temperature
int incomingByte = 0; //Action input
float humidity, temperature;

void setup() {
  delay(100);
  Serial.begin(9600);
  delay(100);
  Serial.println("Hello");
  //Wire.begin(12,16);
  Wire.begin();
  Wire.setClockStretchLimit(1500);
}

void loop() {
  if (Serial.available() > 0) {    //User presses 1 to start sensor reading
    // read the incoming byte:
    incomingByte = Serial.read();
    yield();
    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }

  Serial.println("---g-");  //Just guideline

  if (incomingByte == 49){
    Serial.println("IIIII");  //Just guideline
    Wire.beginTransmission(HDC);
    Wire.write(0x00);
    Wire.endTransmission(true);
    delay(200);

    Wire.requestFrom(HDC,4,true); //It asks for 4 bytes, 2 bytes per register
    if(Wire.available()){
      Serial.println("     ");
      Serial.println("Measuring: ");
      temvalmsb = Wire.read();
      temvallsb = Wire.read();
      humvalmsb = Wire.read();
      humvallsb = Wire.read();

      Serial.println("====TEMPERATURE====");
      Serial.print("MSB: "); Serial.println(temvalmsb, BIN);
      Serial.print("LSB: "); Serial.println(temvallsb, BIN);
      temrawvalue = temvalmsb <<8| temvallsb; //Locates each byte in its proper place
      Serial.print("Temp: "); Serial.println(temrawvalue, BIN);
      temperature = ((float)temrawvalue/(float)cociente)*165.0-40.0;
      Serial.print("Temperatura [C]: "); Serial.println(temperature);

      Serial.println("====HUMIDITY====");
      Serial.print("MSB: "); Serial.println(humvalmsb, BIN);
      Serial.print("LSB: "); Serial.println(humvallsb, BIN);
      humrawvalue = humvalmsb <<8| humvallsb;
      Serial.print("Hum: "); Serial.println(humrawvalue, BIN);
      humidity = ((float)humrawvalue/(float)cociente)*100.0;
      Serial.print("Humidity [%]: "); Serial.println(humidity);

      Serial.println("     ");
      delay(3000);
    }
  }
  Serial.println("++++"); //Just guideline
  delay(3000);
}

Regards!

4
  • 1
    2 and 14? i thought 4+5 were the hardware i2c pins... if you don't specify, that what's used
    – dandavis
    Jun 8, 2017 at 6:23
  • @dandavis that's the main problem, each datasheet shows different information about I2C. I just tried with 4,5 and testing the signal on an scope but there's no difference, I just get a DC signal
    – Julio
    Jun 8, 2017 at 22:37
  • Is the device a 5V device? Oct 21, 2017 at 10:35
  • Update: it never worked. ESP-12F was still very unstable, what made it impossible to connect this specific sensor. With serial protocol for other devices the micro did it fine, but struggled when using I2C
    – Julio
    Aug 9, 2020 at 23:53

2 Answers 2

5

Unlike most Arduinos, the ESP8266 doesn't have a hardware TWI, so I²C is bit-banged in software. This means that you can use any GPIO pins. By default, Wire.begin() initializes pins 4 (SDA) and 5 (SCL), but you can specify other pins using Wire.begin(int sda, int scl).

Documentation

(Note: the datasheet specifies GPIO2 as SDA and GPIO14 as SCL, but that doesn't matter, AFAIK, it's just a convention.)

1
0

Are you referring the I2C pins on the Arduino? You could just skip the Arduino and wire it direct to the ESP8266 and in your code define which pins you are using for I2C or amend the library accordingly. If you go down this route, make sure you use the correct I2C scanner for the ESP8266. Which can be found on this Instructable, otherwise your I2C won't be detected.

http://www.instructables.com/id/ESP8266-I2C-PORT-and-Address-Scanner/

If you do stick with the Arduino, make sure the library is using the right address, again checking using the Arduino I2C scanner sketch to find which I2C address it is using.

https://playground.arduino.cc/Main/I2cScanner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.