Skip to main content

Controlling Servo Motor With MPU6050 Gyro Sensor Using ESP32 Beginners Projects

 


Controlling Servo With MPU6050 Gyro Sensor Using ESP32 - Servo Control Using ESP32

MPU6050 measures accelerometer + gyroscope. Code calculates pitch from accelerometer (X tilt). The pitch (range -90° to +90°) is mapped to servo angles (0°–180°). The servo on GPIO 13 moves according to tilt.

📌 Wiring:-


MPU6050ESP32

VCC → 3.3V

GND → GND

SDA → GPIO 21 (default I²C SDA on ESP32)

SCL → GPIO 22 (default I²C SCL on ESP32)


Servo → ESP32

Signal (orange/yellow) → GPIO 13

VCC (red) → 5V (use external supply if possible, not directly from ESP32)

GND (black/brown) → GND (common ground with ESP32 & MPU6050)

⚠️ Important: Servos draw a lot of current → use an external 5V supply for the servo, not just the ESP32’s 5V pin.


Controlling servo with mpu6050 Using Esp32



Full Code:- 

#include <Wire.h>

#include <Adafruit_MPU6050.h>

#include <Adafruit_Sensor.h>

#include <ESP32Servo.h>

Adafruit_MPU6050 mpu;

Servo myServo;

int servoPin = 13;  // PWM pin for servo

void setup() {

  Serial.begin(115200);

  // Initialize MPU6050

  if (!mpu.begin()) {

    Serial.println("Failed to find MPU6050 chip");

    while (1) {

      delay(10);

    }

  }

  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);

  mpu.setGyroRange(MPU6050_RANGE_500_DEG);

  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);

  delay(100);

  // Attach servo

  myServo.attach(servoPin);

}

void loop() {

  sensors_event_t a, g, temp;

  mpu.getEvent(&a, &g, &temp);

  // Calculate pitch angle from accelerometer

  float pitch = atan2(a.acceleration.y, a.acceleration.z) * 180 / PI;

  // Map pitch (-90 to 90) → servo (0 to 180)

  int servoAngle = map(pitch, -90, 90, 0, 180);

  // Constrain to avoid errors

  servoAngle = constrain(servoAngle, 0, 180);

  myServo.write(servoAngle);

  Serial.print("Pitch: ");

  Serial.print(pitch);

  Serial.print("°  →  Servo: ");

  Serial.println(servoAngle);

  delay(20); // Small delay for smooth motion

}


👉Project All Parts Amazon Link:- Click Here👈


Comments

Popular posts from this blog

Clap On Off Switch Circuit Using Arduino

  Arduino Clap LED program #define MicAO 8 int ledPin = 13; int clap = 0; long detection_range_start = 0; long detection_range = 0; boolean status_lights = false; void setup() {   pinMode(MicAO, INPUT);   pinMode(ledPin,OUTPUT);   } void loop() { int status_MicAO = digitalRead(MicAO); if (status_MicAO == 0) { if (clap == 0) { detection_range_start = detection_range = millis(); clap++; } else if (clap > 0 && millis()-detection_range >= 50) { detection_range = millis(); clap++; } } if (millis()-detection_range_start >= 400) { if (clap == 2) { if (!status_lights) { status_lights = true; digitalWrite(ledPin, HIGH); } else if (status_lights) { status_lights = false; digitalWrite(ledPin, LOW); } } clap = 0; } }

Bluetooth Robot Car Using Arduino Project Step By Step

Bluetooth Controlled Robot Car Using Arduino Bluetooth Controlled Robot Car Using Arduino Project using LM298n motor deriver and hc-05 bluetooth module step by step tutorial. Code Download :- Click Here Arduino bluetooth controller app Arduino bluetooth controller App

How To Make Radar With Arduino UNO Using Ultrasonic Sensor

  Creating a radar system with an Arduino UNO and an ultrasonic sensor involves a few straight forward steps. First, you'll need to connect the ultrasonic sensor's trigger pin to one of the Arduino’s digital output pins and the echo pin to a digital input pin. Then, write a simple Arduino sketch to send a pulse from the trigger pin and measure the duration of the pulse received on the echo pin. This duration, proportional to the distance of an object from the sensor, can be calculated using the speed of sound. By continuously taking readings and mapping them to distances, you can plot these measurements to visualize objects in your environment, effectively creating a basic radar system. To display the results, you could use a serial monitor or even integrate an LCD display for real-time feedback. Here is the code below ,- //radar.ino #include <Servo.h>  const int trigPin = 9; const int echoPin = 8; long duration; int distance; Servo myServo;  void setup() {   p...