Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Adafruit MPU6050 and VL6180X Sensor Interface with Servo Control

View and Edit Project In Cirkit DesignerImage of Adafruit MPU6050 and VL6180X Sensor Interface with Servo Control

Circuit Documentation

Summary of the Circuit

This circuit integrates a set of sensors and a microcontroller to perform various sensing and control tasks. The primary components include the Adafruit MPU6050, a motion-tracking device with a 6-axis gyroscope and accelerometer; the Adafruit QT Py microcontroller, which serves as the central processing unit for the circuit; the Adafruit VL6180X Time of Flight Distance Sensor, which measures distance using laser light; and a Servomotor SG90, a small and lightweight servo for motion control. The circuit is designed to read sensor data from the MPU6050 and VL6180X and output it through the QT Py's serial interface. Additionally, the QT Py controls the Servomotor SG90 based on the program logic.

Component List

Adafruit MPU6050

Adafruit QT Py

Adafruit VL6180X Time of Flight Distance Sensor

Servomotor SG90

Wiring Details

Adafruit MPU6050

Adafruit QT Py

Adafruit VL6180X Time of Flight Distance Sensor

Servomotor SG90

Documented Code

#include <Wire.h>
#include <Adafruit_VL6180X.h>        // For VL6180X ToF sensor
#include <Adafruit_MPU6050.h>        // For Adafruit MPU6050 library
#include <Adafruit_Sensor.h>         // For sensor data handling

// VL6180X Time-of-Flight sensor setup
Adafruit_VL6180X vl = Adafruit_VL6180X();

// MPU6050 setup
Adafruit_MPU6050 mpu;

void setup() {
  // Initialize serial communication for debugging (serial monitor output)
  Serial.begin(115200);
  Serial.println("Starting setup...");

  // Initialize I2C communication (shared by all devices via Qwiic)
  Wire.begin();

  // Initialize MPU6050 sensor
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1);
  }
  Serial.println("MPU6050 Initialized");
  mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);

  // Initialize VL6180X ToF sensor
  if (!vl.begin()) {
    Serial.println("Failed to find VL6180X chip");
    while (1);
  }
  Serial.println("VL6180X ToF Sensor Initialized");

  Serial.println("Setup complete. System ready.");
}

void loop() {
  // 1. Get MPU6050 sensor readings
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  // Output MPU6050 accelerometer and gyroscope readings to the serial monitor
  Serial.print("MPU6050 AccX: ");
  Serial.print(a.acceleration.x);
  Serial.print(" | AccY: ");
  Serial.print(a.acceleration.y);
  Serial.print(" | AccZ: ");
  Serial.print(a.acceleration.z);
  Serial.print(" | GyroX: ");
  Serial.print(g.gyro.x);
  Serial.print(" | GyroY: ");
  Serial.print(g.gyro.y);
  Serial.print(" | GyroZ: ");
  Serial.println(g.gyro.z);

  // 2. Read distance from VL6180X ToF sensor
  uint8_t range = vl.readRange();
  uint8_t status = vl.readRangeStatus();

  // Output VL6180X readings to the serial monitor
  if (status == VL6180X_ERROR_NONE) {
    Serial.print("VL6180X ToF Distance: ");
    Serial.print(range);
    Serial.println(" mm");
  } else {
    Serial.println("VL6180X ToF Error");
  }

  // Small delay before the next loop
  delay(1000);  // Update rate
}

This code is designed to run on the Adafruit QT Py microcontroller. It initializes and reads data from both the MPU6050 motion sensor and the VL6180X Time of Flight sensor, outputting the readings to the serial monitor. The code also includes error checking to ensure that the sensors are properly initialized before attempting to read data.