The MPU6050 gives a NodeMCU a sense of tilt and motion — for gesture controls, a self-balancing robot or a vibration logger that sends alerts over Wi-Fi.
The GY-521 board is I2C, so it shares the same two wires as an OLED: SCL to D1, SDA to D2, power from 3V3. The Adafruit library handles the register set-up; the sketch sets sensible ranges and prints all seven values twice a second.
Wiring
| NodeMCU ESP8266 | MPU6050 (GY-521) | Wire | Notes |
|---|---|---|---|
| 3V3 | VCC | red | The GY-521 regulator passes 3.3 V through with a small drop; its pull-ups then sit at about 3.3 V. |
| GND | GND | black | |
| D1 | SCL | yellow | GPIO5 — default SCL. |
| D2 | SDA | blue | GPIO4 — default SDA. |
3.3–5 V (on-board 3.3 V regulator) part, 3.3 V board. The GY-521 has a 3.3 V regulator and pulls SDA/SCL up to 3.3 V, so it works on both 3.3 V boards and a 5 V Uno without a level shifter.
Board quirks
- I2C defaults on the ESP8266 are SDA = D2 (GPIO4), SCL = D1 (GPIO5). The core does I2C in software; 100 kHz (the default) and 400 kHz both work with the MPU6050.
- If you use the INT pin for motion-detect wake-up, wire it to D5, D6 or D7 — not D0. GPIO16 has no interrupt support, so
attachInterrupt()on D0 never fires. - AD0 open (or to GND) gives address 0x68; AD0 to 3V3 gives 0x69. The XDA/XCL pins are for a second sensor on the MPU’s own aux bus — leave them unconnected.
- The temperature reading is the chip’s die temperature, a few degrees above the room. It is useful only for compensating the gyro, not as a thermometer.
Code
// MPU-6050 (GY-521) accelerometer + gyro on a NodeMCU (ESP8266).
// VCC → 3V3, GND → GND, SCL → D1 (GPIO5), SDA → D2 (GPIO4). AD0 unconnected = address 0x68.
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
void setup() {
Serial.begin(115200);
Serial.println();
Wire.begin(); // SDA = D2, SCL = D1 on the ESP8266 core
if (!mpu.begin(0x68, &Wire)) {
Serial.println("MPU6050 not found - check SDA/SCL, VCC and AD0");
while (true) delay(100); // delay() keeps the watchdog fed
}
mpu.setAccelerometerRange(MPU6050_RANGE_4_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("MPU6050 found at 0x68");
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.printf("Accel X %.2f Y %.2f Z %.2f m/s2 | Gyro X %.2f Y %.2f Z %.2f rad/s | %.1f C\n",
a.acceleration.x, a.acceleration.y, a.acceleration.z,
g.gyro.x, g.gyro.y, g.gyro.z, temp.temperature);
delay(500);
}// MPU-6050 (GY-521) accelerometer + gyro on a NodeMCU (ESP8266).
// VCC → 3V3, GND → GND, SCL → D1 (GPIO5), SDA → D2 (GPIO4). AD0 unconnected = address 0x68.
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
void setup() {
Serial.begin(115200);
Serial.println();
Wire.begin(); // SDA = D2, SCL = D1 on the ESP8266 core
if (!mpu.begin(0x68, &Wire)) {
Serial.println("MPU6050 not found - check SDA/SCL, VCC and AD0");
while (true) delay(100); // delay() keeps the watchdog fed
}
mpu.setAccelerometerRange(MPU6050_RANGE_4_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("MPU6050 found at 0x68");
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.printf("Accel X %.2f Y %.2f Z %.2f m/s2 | Gyro X %.2f Y %.2f Z %.2f rad/s | %.1f C\n",
a.acceleration.x, a.acceleration.y, a.acceleration.z,
g.gyro.x, g.gyro.y, g.gyro.z, temp.temperature);
delay(500);
}Library: Adafruit MPU6050 (Adafruit (also install Adafruit Unified Sensor and Adafruit BusIO)) — compiled with Adafruit MPU6050 2.2.9.
Expected serial output
MPU6050 found at 0x68
Accel X 0.31 Y -0.12 Z 9.86 m/s2 | Gyro X -0.02 Y 0.01 Z 0.00 rad/s | 29.4 C
Accel X 4.87 Y -0.40 Z 8.51 m/s2 | Gyro X 0.63 Y -0.11 Z 0.05 rad/s | 29.4 CMPU6050 found at 0x68 Accel X 0.31 Y -0.12 Z 9.86 m/s2 | Gyro X -0.02 Y 0.01 Z 0.00 rad/s | 29.4 C Accel X 4.87 Y -0.40 Z 8.51 m/s2 | Gyro X 0.63 Y -0.11 Z 0.05 rad/s | 29.4 C
Common errors
“MPU6050 not found”
SDA/SCL swapped, AD0 floating on a module without a pull-down, or a clone chip with a different WHO_AM_I. Full fix.
Values freeze after a while
Loose breadboard contacts on SDA/SCL make the I2C bus hang. Solder the header and keep the wires short.