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

ESP32 C3 Controlled Robot with VL6180 Time of Flight Sensor

View and Edit Project In Cirkit DesignerImage of ESP32 C3 Controlled Robot with VL6180 Time of Flight Sensor

Circuit Documentation

Summary

This circuit is designed to control a pair of DC gearmotors using a DRV8833 motor driver, with an ESP32-C3 microcontroller as the central processing unit. The circuit also includes an Adafruit VL6180 Time of Flight (ToF) sensor for distance measurement. The ESP32-C3 microcontroller is responsible for driving the motors through the DRV8833 and for interfacing with the ToF sensor to read distance values.

Component List

Xiao ESP32 C3

Adafruit VL6180 Time of Flight Sensor

DRV8833 Motor Driver

Gearmotor DC Wheels Right

Gearmotor DC Wheels Left

Wiring Details

Xiao ESP32 C3

Adafruit VL6180 Time of Flight Sensor

DRV8833 Motor Driver

Gearmotor DC Wheels Right

Gearmotor DC Wheels Left

Documented Code

#include <Wire.h>
#include <Adafruit_VL6180X.h>
#include <Adafruit_MotorShield.h>

// Define motor driver pins
#define MOTOR_A1_PIN 4  // Forward pin for motor A (Left Motor)
#define MOTOR_A2_PIN 3  // Backward pin for motor A
#define MOTOR_B1_PIN 1  // Forward pin for motor B (Right Motor)
#define MOTOR_B2_PIN 0  // Backward pin for motor B

// Initialize the distance sensor
Adafruit_VL6180X vl = Adafruit_VL6180X();

void setup() {
  // Initialize serial communication
  Serial.begin(115200);
  Serial.println("Starting Test");

  // Set motor pins as outputs
  pinMode(MOTOR_A1_PIN, OUTPUT);
  pinMode(MOTOR_A2_PIN, OUTPUT);
  pinMode(MOTOR_B1_PIN, OUTPUT);
  pinMode(MOTOR_B2_PIN, OUTPUT);

  // Initialize the VL6180 sensor
  if (!vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("VL6180X sensor initialized");
}

void loop() {
  // Read distance
  uint8_t range = vl.readRange();
  Serial.print("Distance: ");
  Serial.print(range);
  Serial.println(" mm");

  // Drive motors forward
  digitalWrite(MOTOR_A1_PIN, HIGH);
  digitalWrite(MOTOR_A2_PIN, LOW);
  digitalWrite(MOTOR_B1_PIN, HIGH);
  digitalWrite(MOTOR_B2_PIN, LOW);
  delay(1000);

  // Stop motors
  digitalWrite(MOTOR_A1_PIN, LOW);
  digitalWrite(MOTOR_A2_PIN, LOW);
  digitalWrite(MOTOR_B1_PIN, LOW);
  digitalWrite(MOTOR_B2_PIN, LOW);
  delay(1000);

  // Drive motors backward
  digitalWrite(MOTOR_A1_PIN, LOW);
  digitalWrite(MOTOR_A2_PIN, HIGH);
  digitalWrite(MOTOR_B1_PIN, LOW);
  digitalWrite(MOTOR_B2_PIN, HIGH);
  delay(1000);

  // Stop motors
  digitalWrite(MOTOR_A1_PIN, LOW);
  digitalWrite(MOTOR_A2_PIN, LOW);
  digitalWrite(MOTOR_B1_PIN, LOW);
  digitalWrite(MOTOR_B2_PIN, LOW);
  delay(1000);
}

This code is designed to run on the Xiao ESP32 C3 microcontroller. It initializes the Adafruit VL6180X ToF sensor and controls the DRV8833 motor driver to move the gearmotors attached to the wheels of a mobile platform. The code includes functionality to drive the motors forward and backward, as well as to read the distance from the ToF sensor and output it to the serial monitor.