wireless drone — Esp32 Devkit C V4 simulation

An interactive Esp32 Devkit C V4 circuit simulation you can run free in your browser on Velxio, by tareq-neoaz.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// ===== JOYSTICK PINS =====
#define THROTTLE_PIN 35
#define YAW_PIN      34
#define PITCH_PIN    33
#define ROLL_PIN     32

// ===== ARM TOUCH =====
#define ARM_TOUCH 26

// ===== UART TO DRONE =====
#define UART_TX 17
#define UART_RX 16

// ===== OLED =====
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// ===== STATE =====
bool armed = false;
unsigned long lastSend = 0;

void setup() {
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, UART_RX, UART_TX);

  pinMode(ARM_TOUCH, INPUT);

  Wire.begin(21, 22);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Remote Ready");
  display.display();
  delay(1000);
}

int readAxis(int pin, bool centered) {
  int raw = analogRead(pin);
  if (centered) {
    int val = map(raw, 0, 4095, -255, 255);
    if (abs(val) < 20) val = 0;
    return val;
  } else {
    return map(raw, 0, 4095, 0, 255);
  }
}

void loop() {
  int throttle = readAxis(THROTTLE_PIN, false);
  int yaw      = readAxis(YAW_PIN, true);
  int pitch    = readAxis(PITCH_PIN, true);
  int roll     = readAxis(ROLL_PIN, true);

  static bool lastArm = false;
  bool armTouch = digitalRead(ARM_TOUCH) == HIGH;
  if (armTouch && !lastArm) {
    armed = !armed;
  }
  lastArm = armTouch;

  if (millis() - lastSend > 20) {
    Serial2.print(throttle); Serial2.print(",");
    Serial2.print(yaw);      Serial2.print(",");
    Serial2.print(pitch);    Serial2.print(",");
    Serial2.print(roll);     Serial2.print(",");
    Serial2.println(armed ? 1 : 0);
    lastSend = millis();
  }

  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("ARM: "); display.println(armed ? "YES" : "NO");

  display.setCursor(0, 16);
  display.print("TH: "); display.println(throttle);

  display.setCursor(0, 32);
  display.print("Y:"); display.print(yaw);
  display.print(" P:"); display.println(pitch);

  display.setCursor(0, 48);
  display.print("R:"); display.println(roll);
  display.display();

  delay(20);
}

More projects by tareq-neoaz