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

Quadcopter Flight Controller with GPS and Ultrasonic Sensor

View and Edit Project In Cirkit DesignerImage of Quadcopter Flight Controller with GPS and Ultrasonic Sensor

Circuit Documentation

Summary

The circuit in question is designed to control a quadcopter with four brushless motors, each connected to an individual Electronic Speed Controller (ESC). The circuit includes an Arduino Leonardo microcontroller for processing and control, an MPU-6050 sensor for motion tracking, a GPS NEO 6M module for positioning, an HC-SR04 ultrasonic sensor for distance measurement, and an OV7725 camera module for visual data capture. The system is powered by a lipo battery, and the ESCs provide power distribution to the other components.

Component List

Brushless Motor

Electronic Speed Controller (ESC)

MPU-6050

HC-SR04 Ultrasonic Sensor

OV7725 Camera Module

Arduino Leonardo (Rev3b)

GPS NEO 6M

Lipo Battery 2200mAh 30C

Wiring Details

Brushless Motors

Electronic Speed Controllers (ESCs)

MPU-6050

HC-SR04 Ultrasonic Sensor

OV7725 Camera Module

Arduino Leonardo (Rev3b)

GPS NEO 6M

Documented Code

#include <Wire.h>
#include <Servo.h>
#include <SoftwareSerial.h>

// Define pins for ESC signals
#define ESC1_PIN 5
#define ESC2_PIN 6
#define ESC3_PIN 9
#define ESC4_PIN 3

// Define pins for sensors
#define TRIG_PIN 10
#define ECHO_PIN 11

// GPS module pins
#define GPS_RX_PIN 2
#define GPS_TX_PIN 3

// Create Servo objects for ESCs
Servo esc1;
Servo esc2;
Servo esc3;
Servo esc4;

// MPU-6050 I2C address
const int MPU_ADDR = 0x68;

// Variables to store MPU-6050 data
int16_t ax, ay, az;
int16_t gx, gy, gz;

// SoftwareSerial for GPS
SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN);

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  gpsSerial.begin(9600);

  // Initialize ESCs
  esc1.attach(ESC1_PIN);
  esc2.attach(ESC2_PIN);
  esc3.attach(ESC3_PIN);
  esc4.attach(ESC4_PIN);

  // Set initial ESC speed to 0
  esc1.writeMicroseconds(1000);
  esc2.writeMicroseconds(1000);
  esc3.writeMicroseconds(1000);
  esc4.writeMicroseconds(1000);

  // Initialize MPU-6050
  Wire.begin();
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x6B); // PWR_MGMT_1 register
  Wire.write(0);    // Set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);

  // Initialize ultrasonic sensor
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // Read MPU-6050 data
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B); // Starting register for accelerometer data
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_ADDR, 14, true); // Request a total of 14 registers

  ax = Wire.read() << 8 | Wire.read();
  ay = Wire.read() << 8 | Wire.read();
  az = Wire.read() << 8 | Wire.read();
  gx = Wire.read() << 8 | Wire.read();
  gy = Wire.read() << 8 | Wire.read();
  gz = Wire.read() << 8 | Wire.read();

  // Print MPU-6050 data to serial monitor
  Serial.print("aX = "); Serial.print(ax);
  Serial.print(" | aY = "); Serial.print(ay);
  Serial.print(" | aZ = "); Serial.print(az);
  Serial.print(" | gX = "); Serial.print(gx);
  Serial.print(" | gY = "); Serial.print(gy);
  Serial.print(" | gZ = "); Serial.println(gz);

  // Read ultrasonic sensor data
  long duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;

  // Print ultrasonic sensor data to serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Control ESCs based on sensor data (example logic)
  int speed = map(distance, 0, 200, 1000, 2000); // Map distance to ESC speed range
  esc1.writeMicroseconds(speed);
  esc2.writeMicroseconds(speed);
  esc3.writeMicroseconds(speed);
  esc4.writeMicroseconds(speed);

  // Read GPS data
  while (gpsSerial.available()) {
    char c = gpsSerial.read();
    Serial.print(c); // Print GPS data to serial monitor
  }

  delay(100);
}

This code is responsible for initializing and controlling the various components of the quadcopter. It sets up communication with the MPU-6050 sensor and the GPS module, initializes the ESCs, reads sensor data, and adjusts the speed of the motors accordingly. The ultrasonic sensor is used to measure distance, which is then mapped to motor speed to control the altitude of the quadcopter. The GPS data is read and output to the serial monitor for tracking purposes.