✅ COMPLETED IN LAB Day 9 (17 Nov) I2C 400kHz

Session 2: External Dual LEDs + MPU-6050 Tilt Spirit Level

Connected the MPU-6050 6-axis Gyroscope & Accelerometer over I2C (SDA 8, SCL 9). Controlled 2 external breadboard LEDs (Left GPIO 4 & Right GPIO 5) to create a visual electronic spirit level!

← Course Book
Connect Robo Logo Connect Robo Kit

⚡ 4-Step Visual System Flow

No Text Walls • 1-Glance Architecture
Step 1 📐
MPU-6050 6-Axis IMU
I2C Bus (Pins 8 & 9)

Reads gravity acceleration vector along X and Y axes.

Step 2 🧠
atan2() Tilt Math
Pitch & Roll Degrees

Calculates real-time horizontal level tilt in degrees.

Step 3 ⚡
Threshold Detection
|Tilt| > 15° Alert

Checks if tilt exceeds safe level balance angle.

Step 4 💡
Dual Warning LEDs
GPIO 4 & 5 Outputs

Left LED lights on left tilt; Right LED on right tilt!

1

🧰 Components Used in Session 2

ESP32-S3
ESP32-S3 Dev Board Brain • Dual USB-C
MPU-6050 IMU
MPU-6050 6-Axis IMU I2C: SDA 8 / SCL 9 (3.3V)
2x LEDs
2x Red LEDs (5mm) Left: GPIO 4 • Right: GPIO 5
Breadboard
Breadboard & 220Ω Resistors Current Limiting Protection
★

🧭 Live MPU-6050 Spirit Level Simulator (Touch to Tilt!)

Real-Time Math
Virtual MPU-6050 Spirit Level
Left (GPIO 4) MPU-6050 Right (GPIO 5)
0.0° (FLAT • Both LEDs OFF)
Drag to Tilt Board: 0°
⟵ Tilt Left (-45°) Flat (0°) Tilt Right (+45°) ⟶

When tilted > +10° to the right, the Right LED (GPIO 5) lights up in red. When tilted < -10° to the left, the Left LED (GPIO 4) turns on. Keep flat to turn both OFF!

3

🔌 Circuit Pinout (ESP32-S3 I2C + External LEDs)

MPU-6050 VCC
Red Jumper Wire to 3.3V Power Rail
3.3V Power
Common Ground (GND)
Black Jumper connecting MPU GND and LED Cathodes
GND
MPU-6050 SDA (I2C Data)
Yellow Jumper Wire from MPU SDA to ESP32
GPIO 8
MPU-6050 SCL (I2C Clock)
Green Jumper Wire from MPU SCL to ESP32
GPIO 9
Left Tilt LED Anode (+)
Blue Jumper Wire via 220Ω Current-Limit Resistor
GPIO 4
Right Tilt LED Anode (+)
Orange Jumper Wire via 220Ω Current-Limit Resistor
GPIO 5
4

⚡ Step-by-Step Verified Code (1-Click "Open in IDE")

step1_dual_led_blink.ino ESP32-S3 20 lines
/* Session 2 - Step 1: Dual External LED Blink (GPIO 4 & GPIO 5) */
#define LED_LEFT 4
#define LED_RIGHT 5

void setup() {
  pinMode(LED_LEFT, OUTPUT);
  pinMode(LED_RIGHT, OUTPUT);
}

void loop() {
  digitalWrite(LED_LEFT, HIGH);
  digitalWrite(LED_RIGHT, LOW);
  delay(500);
  digitalWrite(LED_LEFT, LOW);
  digitalWrite(LED_RIGHT, HIGH);
  delay(500);
}
Preview (8 of 20 lines) — Click ⚡ Open in IDE to auto-load code & board!
✅ Output: Left and Right external LEDs blink alternately every 500ms!
step2_mpu6050_spirit_level.ino ESP32-S3 45 lines
/* Session 2 - Final: MPU-6050 Digital Spirit Level */
#include <Wire.h>
#define LED_LEFT 4
#define LED_RIGHT 5
#define MPU_ADDR 0x68

void setup() {
  Serial.begin(115200);
  pinMode(LED_LEFT, OUTPUT);
  pinMode(LED_RIGHT, OUTPUT);
  Wire.begin(8, 9); // ESP32-S3 default I2C pins
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x6B);
  Wire.write(0); // Wake up MPU-6050
  Wire.endTransmission();
}

void loop() {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_ADDR, 4, true);
  int16_t ax = (Wire.read() << 8) | Wire.read();
  int16_t ay = (Wire.read() << 8) | Wire.read();

  float roll = (ay / 16384.0) * 45.0;
  Serial.printf("Roll Angle: %.1f deg\n", roll);

  if (roll < -10.0) {
    digitalWrite(LED_LEFT, HIGH);
    digitalWrite(LED_RIGHT, LOW);
  } else if (roll > 10.0) {
    digitalWrite(LED_LEFT, LOW);
    digitalWrite(LED_RIGHT, HIGH);
  } else {
    digitalWrite(LED_LEFT, LOW);
    digitalWrite(LED_RIGHT, LOW);
  }
  delay(50);
}
Preview (8 of 45 lines) — Click ⚡ Open in IDE to auto-load code & board!
✅ Output: Tilt left ➔ Left LED ON. Tilt right ➔ Right LED ON. Flat ➔ Both OFF!
← Session 1: Super-Kit & RGB LED Session 3: Buttons & Motion Game →