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

User avatar
By Hopperpop
#22338 I got my ESP8266-01 in the mail this week and I was excited to do some things with it. After some struggeling I got it working and I could run some example programs. Now I want to connect a MMA8451 accelerometer to it and read it out. But I'm having problems with the I2C communication.

I ran an I2C scanner that I found here:
The scanner found the sensor and gaves the expected adress (0x1C).
If the scanner works, can I be sure that the I2C connection works?

After that I made a small program to see if I could read something from the sensor (I use the arduino IDE).
Code: Select all#include <Wire.h>

void setup()
   Wire.begin(0, 2);

void loop()
    Wire.write(0x0D); //WHO_AM_I register
    int error=Wire.endTransmission(false); // MMA8451 + friends uses repeated start!!
    Serial.print("Transmissionerror: ");
    Wire.requestFrom(0x1C, 1);
    if (! Wire.available()) {
      Serial.println("Nothing came back");
    else {


This is what I get in the serial monitor:
    Transmissionerror: 0
    Nothing came back
    Transmissionerror: 4
    Nothing came back
    Transmissionerror: 4

If I put the same program on a arduino nano and use Wire.begin(), it works perfect and I get the value from the register (26). So there is nothing wrong with the program or sensor. The only place where I think the problem can be is in the special Wire library or in the hardware connection.
I added two 10k pull-up resistor to GPIO 0 and 2 and connected them to SDA and SCL. Here is the schematic of the sensor board:

Can somebody help me figuring out what the problem is? I tried a lot of different things (changing clock speed, removing the pull-ups, using a lvl shifter to go back to 5v,...) But nothing seems to work.
User avatar
By Hopperpop
#23162 After a lot of digging in the libaries code, I found the problem. The I²C libary has a bug with repeated start. The SCL isn't put to low after an ACK, so the sensor doesn't release the ACK. And if it then sends a new start command, it thinks that the line is busy.

The code was already fixed in the newer versions, but I used the board manager to install the ESP8266 to the arduino IDE and that have the bug in it. So I manualy installed the newest "core_esp8266_si2c.c" libary and now everything works fine.
User avatar
By eriksl
#23424 There is hardly ever a situation where you need a repeated start condition in practice.

You didn't mention you were using Arduino. I'd almost say, go and file a complaint with the Arduino guys.
User avatar
By mpbr
#90342 Hi, I'm new to NodeMCU 8266 and MMA845x sensor. I've tested the i2c script also and it gives the 0x1c address. Then, I've tried your script but changing the sda and scl pin (sda:D2 ,scl:D1) but it doesn't work. I also get "transmissionerror: 4, Nothing came back" output. This is my pinout connection:
Sensor 3.3V - NodeMCU 3V3
Sensor GND - NodeMCU GND
Sensor SDA - NodeMCU D2 (GPIO 4)
Sensor SCL - NodeMCU D1 (GPIO 5)
I've tried lots of changes but I'm struggling with it. I downloaded Arduino IDE and NodeMCU 1.0 board inside last year so I supposed it has the new i2c core lib.
I know this was 5 years ago but I'm still hopeful. Thanks in advance.