MPU6050 on an ESP8266-01 via MQTT

Update 3/16/17: I added code at the end of the post for dual MPU6050s
Update 2/2/18: Added images of possible wiring examples for ESP-01 and NodeMCU

Another milestone for me. I’m on a fucking roll! I found an Arduino sketch that takes an MPU6050 on an ESP8266-01  and spits it out data via MQTT. I have been looking for this and trying to accomplish this for months. My end goal is a clothes washer and dryer monitor for the family. There are sketches out there for laundry monitors but they don’t have what I need, mainly multiple users…I digress. So I found the script and it works, but some of it was it Italian which made it confusing for me and it did clumped all the sensor data into just two categories; accelerometer or gyroscope. I need more detailed information so I hacked it up a bit and it works!!

I commented out some stuff that was not needed (it all works for me YMMV), and I split all of the sensor data up into their own MQTT topic. So you get topics for AcX, AcY, AcZ and GyX, GyY, GyZ. This allows me to just use the sensor that works for the washer or dryer. Made it really easy once I plotted the data to a line graph.

Code below:
Originally taken from here

Arduino IDE code for the ESP8266, same as above but modified for dual (2) MPU6050s. It will spit out the MPU sensor data to MQTT topics: AcX, AcY, AcZ, GyX, GyY, GyZ. And the same for MPU #2. AcX2, AcY2, etc…

Code snaked from


Example wiring of a single MPU and ESP-01 (image borrowed from myrobotlab)

Esp8266 Mpu6050

Example possible wiring of a single MPU and NodeMCU. Keep in mind that you may use different pins for the SDA/SCL on a NodeMCU because well, it has more pins. Just specify which ones you are using in the code before you flash (see code line #87 or #127 “Wire.begin(0,2)” ). (Image borrowed from FilipeFlop)

nodemcu mpu6050

Discover more from Its_All.Lost

Subscribe to get the latest posts sent to your email.


  1. Ivanny Shintia
    July 8, 2017 / 8:52 am

    Hi, my name is Ivanny. There are several questions I want to ask you, I also make my final project using esp8266 and mpu6050 accel gyro. I have no idea about mqtt server that you mention above.
    1. how can I make my mqtt server?
    2. how’s the final result? is that a table or graph?
    thank you so much if you want to help me about this….

    • July 15, 2017 / 8:00 pm

      I run my mqtt server off a raspberry pi and another on an old PC running Ubuntu. It doesn’t take much to run one. I use mosquitto, but there are others.

      I use the mqtt server along with Node-Red. It takes the incoming messages from my sensors and then I can do what I want with the data. So I can graph it or use a table or send SMS messages… etc. Sky is the limit really.

  2. Lior G.
    February 2, 2018 / 1:33 am

    Hey m8 well done!

    Quick question, can u provide how u connected the MPU? which wire goes where?

    • February 2, 2018 / 1:52 pm

      It all depends on which ESP module you are using. The ESP only has two pins available so you must use those (0 &2). If you look at the first example at line #87, #127 in the dual MPU example it shows “Wire.begin(0,2);”. That specifies which pins are to be used for SDA/SCL. If you are using a NodeMCU you have many choices, just specify what they are on that line.

      I updated the post with some example images, hope that helps!

  3. Michael
    February 27, 2018 / 2:27 am

    Hi, thank you for this…Can i get my esp-01 to mpu6050 connected to my arduino for power source?
    how do i do it?

    • February 27, 2018 / 1:24 pm

      The 3.3v rail on the Arduino can only put out about 200mA of current. The ESP-01 can draw up to 435mA of current – so be careful. If you are using an Arduino Uno you can use the 5v rail, you will need to step down the voltage to 3.3v for the ESP though. Double check your MPU6050, some use 3.3v and some (like the GY-521) prefer 5v.

  4. Paul
    March 26, 2018 / 5:35 pm

    This also looks like what I am looking for as well. As I am pretty new at this stuff I would like to ask a question or two. I am using Arduino-1,8.5 on windows;trying to flash a nodemcu. Which libraries do I need to import/load to get this to work? I try to compile the sketch, and it always stops saying it’s missing some such library which I thought I had. I know I need pubsub. What else do I need? Sorry if this is rather basic but as I said I am rather new at this.


    • March 26, 2018 / 8:13 pm

      Hey there, lines 1-6 say which libraries we’re including (the others are commented out, unused).

      include Wire.hinclude PubSubClient.hinclude ESP8266WiFi.h

      If you include those and it still fails try to read the output message carefully, as it should tell you why it failed and point you in the direction needed.

  5. Sylvano
    January 9, 2020 / 9:17 am

    Thanks for this sample code

    I cannot see any call of the setup_wifi(); in your voide setup() loop. Am I wrong ?

    I will give a try to your code later today to familiarise myself with the MQTT protocol on ESP8266.

    • January 22, 2020 / 4:19 pm

      I don’t recall, but I don’t see it either. It has been a while since I have used this code but I remember it working as posted.

  6. Abdelnasir Ali Ahmed
    April 1, 2023 / 4:55 pm

    Hello, My name is Nasir. This is very close of my project. I want to make motion capture system by using (ESP01s) ESP8266-01 and ADXL345 (GY-85) for animation purpose. So I will take out put in the next step to 3dsmax (real runtime) to link with 3d character.
    My questions are:
    1. Can I use this code structure and replace MPU6050 and add GY-85 (ADXL345) ?
    2. Can I send data to Java Application through TCP/IP by adding java socket to the main code?
    3. any other solutions.
    * I use arduino IDE and CP2102 for programming.
    * 3V3 (UART) >> 3V3 (ESP8266-01)
    Ground (UART) >> Ground (ESP8266-01)
    TXD (UART) >> RX (ESP8266-01)
    RXD (UART) >> TXD (ESP8266-01)
    EN (ESP8266) >> 3V3 (ESP8266-01)
    GPIO0 (ESP8266) >> GND (ESP8266-01)

    1- Blink Test:
    in Arduino IDE:

    Board: Generic ESP8266 Module.
    Flash Mode: DIO
    Flash Frequency: 40 MHZ
    Reset Method: dtr aka(Nodemcu)
    Upload speed: 115200
    Flash size: 1MB

    // The Code: (under construction)

    /* Define ESP01 (ESP8266) and Wifi. */


    /* Set these to your desired credentials. */

    const char ssid = “******”;
    const char password = “********”;
    ESP8266WebServer server(80);

    #define DEBUG true
    #define Serial if(DEBUG)Serial

    #define DEVICE (0x53) // Device address as specified in data sheet
    float last_value = 0;
    float prelast_value = 0;

    // Reading data from IMU (GY-85) Adxl345
    // 5th step (to work with i2c let use the official Arduino library

    // 6th step (Now let us define the register in the returned access,
    #define SENSOR 0x53
    #define Power_Register 0x2D //Power Control Register
    #define X_Axis_Register_DATAX0 0x32 //X-Axis Data 0
    #define X_Axis_Register_DATAX1 0x33 //X-Axis Data 1
    #define Y_Axis_Register_DATAX0 0x34 //Y-Axis Data 0
    #define Y_Axis_Register_DATAX1 0x35 //Y-Axis Data 1
    #define Z_Axis_Register_DATAX0 0x36 //Z-Axis Data 0
    #define Z_Axis_Register_DATAX1 0x37 //Z-Axis Data 1

    //also add the variable to work with x axes for, two for returned data and one for compose in float numbers the variables to work with
    int DataReturned_x0, DataReturned_x1, DataModified_x_out;
    float x;
    int DataReturned_y0, DataReturned_y1, DataModified_y_out;
    float y;
    int DataReturned_z0, DataReturned_z1, DataModified_z_out;
    float z;

    void setup() {
    Wire.begin (9600);
    Serial.begin (9600);
    Delay (100);


    // Q1- which device (address) are you interest in?

    // 2- Which register do you want to talk to?

    // 3- What do you want the register to transmit?


    void loop() {

    // 4- What do you want to ask about the specific register?

    // 5- Now ask and wait for the data looping in a while
    Wire.requestFrom(SENSOR, 6);
    If (Wire.available() <= 6);
    DataReturned_x0 =;
    DataReturned_x1 =;
    DataReturned_y0 =;
    DataReturned_y1 =;
    DataReturned_z0 =;
    DataReturned_z1 =;
    // 6- Now how do you want the configuration of the three axes numbers ?
    DataReturned_x1 = DataReturned_x1 << 8;
    DataModified_x_out = DataReturned_x0 + DataReturned_x1;
    X = DataModified_x_out / 256.0;
    DataReturned_y1 = DataReturned_y1 << 8;
    DataModified_y_out = DataReturned_y0 + DataReturned_y1;
    Y = DataModified_y_out / 256.0;
    DataReturned_z1 = DataReturned_z1 << 8;
    DataModified_z_out = DataReturned_z0 + DataReturned_z1;
    X = DataModified_z_out / 256.0;

    Serial.print("x = ");
    Serial.print("y = ");
    Serial.print("z = ");

    // The last part print data to serial, I want to send it continuously to java .
    // Transmitting data to laptop (Java) through (esp01s) Wifi module.
    // Java socket !!


Leave a Reply