Micro quadcopter with remote control (ESP32-C3, SX1280)
X-frame drone and a handheld remote: fly it with the sticks over a 2.4 GHz LoRa link.
- ESP32-C3
- MPU-6050
- SX1280
- Coreless motors
- Joysticks
Two emulated chips and the flight physics: on most computers this runs slower than real time.
How it works
Two boards in one project. The quadcopter: an X-shaped board with a 1S LiPo, 3.3 V LDO, ESP32-C3 with a Zephyr flight controller (source firmware/esp32c3/quad_rc), MPU-6050 gyro/accelerometer, an SX1280 2.4 GHz receiver, four AO3400A low-side drivers and four coreless motors. The remote: ESP32-C3 (firmware/esp32c3/rc_remote), SX1280, two thumb joysticks (left: throttle and yaw, right: pitch and roll), an arm switch and a status LED. The remote sends the sticks 250 times a second (LoRa SF5, 812 kHz, packets with sequence number and CRC, every 8th slot answered with telemetry); the quad flies what the sticks say. Arm: switch on with the throttle at the bottom. Without a valid packet for 0.5 s the quad levels out and lowers its throttle to zero (failsafe). The packets go through the simulated air: carry the remote far away in the World and the link breaks; the remote LED blinks fast when the quad no longer answers. In the World view, drag the sticks of the remote with the mouse, or map a game controller. The quad is the flying body: its mass and inertia come from the board and its parts.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | BT1 −BT101 −C1 2C2 2C3 2C4 2C5 2C6 2C7 2C8 2C9 2C10 2C11 2C101 2C102 2C103 2C104 2C105 2C106 2C107 2C108 2J1 GNDJ101 GNDJS101 GNDJS102 GNDQ1 SQ2 SQ3 SQ4 SR7 2R8 2R9 2R10 2SW101 COMU1 GNDU2 GNDU2 AD0U2 CLKINU2 FSYNCU3 GNDU4 GNDU101 GNDU102 GNDU103 GNDY1 GNDY101 GND |
| 3V3 | C2 1C3 1C4 1C9 1R1 1R2 1R11 1R12 1U1 3V3U1 ENU2 VDDU2 VLOGICU3 OUTU4 VBATU4 VBAT_IO |
| VBAT | BT1 +C1 1D1 KD2 KD3 KD4 KM1 +M2 +M3 +M4 +U3 INU3 EN |
| CPOUT | C6 1U2 CPOUT |
| D1 | D1 AM1 −Q1 D |
| D2 | D2 AM2 −Q2 D |
| D3 | D3 AM3 −Q3 D |
| D4 | D4 AM4 −Q4 D |
| G1 | Q1 GR3 2R7 1 |
| G2 | Q2 GR4 2R8 1 |
| G3 | Q3 GR5 2R9 1 |
| G4 | Q4 GR6 2R10 1 |
| PWM1 | R3 1U1 GPIO4 |
| PWM2 | R4 1U1 GPIO5 |
| PWM3 | R5 1U1 GPIO6 |
| PWM4 | R6 1U1 GPIO7 |
| R_3V3 | C102 1C103 1C106 1JS101 VCCJS102 VCCR101 1R102 1R103 1U101 3V3U101 ENU102 VBATU102 VBAT_IOU103 OUT |
| R_ARM | R101 2SW101 1U101 GPIO4 |
| R_BUSY | U101 GPIO18U102 BUSY |
| R_DIO1 | U101 GPIO19U102 DIO1 |
| R_LED | D101 KU101 GPIO8 |
| R_LEDA | D101 AR102 2 |
| R_MISO | U101 GPIO5U102 MISO |
| R_MOSI | U101 GPIO7U102 MOSI |
| R_NSS | R103 2U101 GPIO10U102 NSS |
| R_PIT | JS102 VRYU101 GPIO2 |
| R_RF_ANT | C108 1J101 RFL102 2 |
| R_RF_DCC | L101 1U102 DCC_SW |
| R_RF_RFIO | C107 1L102 1U102 RFIO |
| R_RF_VREG | C104 1L101 2U102 VDD_INU102 DCC_FB |
| R_RF_VRPA | C105 1U102 VR_PA |
| R_RF_XTA | U102 XTAY101 1 |
| R_RF_XTB | U102 XTBY101 2 |
| R_ROL | JS102 VRXU101 GPIO3 |
| R_SCK | U101 GPIO6U102 SCK |
| R_THR | JS101 VRYU101 GPIO0 |
| R_VBAT | BT101 +C101 1U103 INU103 EN |
| R_YAW | JS101 VRXU101 GPIO1 |
| REGOUT | C5 1U2 REGOUT |
| RF_ANT | C11 1J1 RFL2 2 |
14 more connections in the editor.
- C10
- Capacitor
- C11
- Capacitor
- C101
- Capacitor
- C102
- Capacitor
- C103
- Capacitor
- C104
- Capacitor
- C105
- Capacitor
- C106
- Capacitor
- C107
- Capacitor
- C108
- Capacitor
- D1
- Diode
- D2
- Diode
- D3
- Diode
- D4
- Diode
- D101
- LED
- L2
- Inductor
- L101
- Inductor
- L102
- Inductor
- Q1
- N-MOSFET
- Q2
- N-MOSFET
- Q3
- N-MOSFET
- Q4
- N-MOSFET
- R1
- Resistor
- R2
- Resistor
- R3
- Resistor
- R4
- Resistor
- R5
- Resistor
- R6
- Resistor
- R7
- Resistor
- R8
- Resistor
- R9
- Resistor
- R10
- Resistor
- R11
- Resistor
- R12
- Resistor
- R101
- Resistor
- R102
- Resistor
- R103
- Resistor
- Y1
- Crystal
- Y101
- Crystal
Parts
- Capacitor× 19
- Resistor× 15
- Mounting hole× 8
- Coreless motor 7×16 mm + 45 mm propeller× 4
- Diode× 4
- Inductor× 4
- N-MOSFET× 4
- Crystal× 2
- ESP32-C3 (RISC-V, Firmware)× 2
- Thumb joystick (2 axes + push)× 2
- LDO regulator (TPS7A20)× 2
- Li-ion cell (protected)× 2
- SX1280 (2.4 GHz LoRa / FLRC transceiver)× 2
- Antenna socket U.FL (2.4 GHz)× 2
- LED
- MPU-6050 chip (QFN-24, accelerometer + gyro, I²C)
- Toggle switch SPDT (sub-miniature, THT)
Firmware
src/main.c · 247 lines · the first 120 here, all of it in the editor
/* Micro quadcopter flight controller with an RC receiver (ESP32-C3 + SX1280, Zephyr).
*
* The SX1280 listens for the remote's packets (rc_link.h: four channels, arm flag, sequence,
* CRC, 250 Hz); a radio thread takes each packet on DIO1, checks it, keeps the channels, the
* RSSI / SNR of the packet status and the link quality (share of the last 100 packets that
* arrived), and answers a telemetry request at once. The control loop (250 Hz, as
* firmware/esp32c3/quad) flies what the sticks say:
* throttle stick -> collective thrust 0..100 % PWM (no fixed hover value: the pilot hovers)
* pitch / roll sticks -> attitude set point ±25°, yaw stick -> yaw rate ±120 °/s
* Arming: the arm switch on and the throttle stick at the bottom (< 5 %); the switch off
* disarms. Failsafe: no valid packet for 500 ms -> level, the throttle falls from where it was to
* zero within 1 s, then disarmed; flying again needs the switch and low throttle, as at the start.
*
* Pins: MPU-6050 on I2C0 (SDA GPIO1, SCL GPIO3), motors GPIO4..7 (LEDC PWM 1 kHz), SX1280 on
* GP-SPI2 (SCLK GPIO0, MOSI GPIO18, MISO GPIO19, NSS GPIO10), BUSY GPIO9, DIO1 GPIO8.
*
* Axes as on the MPU-6050 and the frame: x forward, y left, z up. Roll > 0: right side down;
* pitch > 0: nose down; yaw > 0: counter-clockwise seen from above. Motors (Crazyflie order):
* M1 front-right CW, M2 back-right CCW, M3 back-left CW, M4 front-left CCW.
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/spinlock.h>
#include <math.h>
#include <stdio.h>
#define SX_BUSY_PIN 9
#define SX_DIO1_PIN 8
#include "sx1280.h"
#include "rc_link.h"
#define MPU 0x68
#define LOOP_US 4000 /* 250 Hz */
#define DEG (180.0f / 3.14159265f)
#define FAILSAFE_MS 500
static const struct pwm_dt_spec motor[4] = {
PWM_DT_SPEC_GET(DT_NODELABEL(m1)), PWM_DT_SPEC_GET(DT_NODELABEL(m2)),
PWM_DT_SPEC_GET(DT_NODELABEL(m3)), PWM_DT_SPEC_GET(DT_NODELABEL(m4)),
};
static const struct device *i2c = DEVICE_DT_GET(DT_NODELABEL(i2c0));
/* gains (per degree / degree per second; motor command 0..1), as firmware/esp32c3/quad */
#define ANGLE_P 6.0f
#define RATE_P 0.0012f
#define RATE_I 0.0010f
#define RATE_D 0.00002f
#define YAW_P 0.0020f
#define MAX_ANGLE 25.0f
#define MAX_YAW 120.0f
enum { DISARMED, ARMED, FAILSAFE };
static const char *const STATE[] = { "disarmed", "armed", "failsafe" };
/* ---- what the radio thread received (guarded by lock) ---------------------------------------- */
static struct k_spinlock lock;
static struct {
uint16_t ch[4];
uint8_t flags;
int64_t at; /* ms of the last valid packet, 0 = none yet */
int rssi, snr, lq;
uint32_t ok, bad;
} rx;
static volatile int state = DISARMED;
static void radio_thread(void)
{
uint8_t hist[100] = { 0 }; /* per sequence slot: 1 = arrived */
int hn = 0, last_seq = -1;
sx_rx(0xFFFF);
for (;;) {
uint16_t f = sx_wait_irq(K_MSEC(100));
if (!f) continue;
if (f & SX_IRQ_TX_DONE) { sx_rx(0xFFFF); continue; } /* telemetry sent: listen again */
if (!(f & SX_IRQ_RX_DONE)) { sx_rx(0xFFFF); continue; }
uint8_t p[RC_LEN];
int rssi, snr;
sx_read_buf(p, RC_LEN);
sx_packet_status(&rssi, &snr);
if ((f & SX_IRQ_CRC_ERROR) || !rc_valid(p)) {
k_spinlock_key_t k = k_spin_lock(&lock); rx.bad++; k_spin_unlock(&lock, k);
continue;
}
/* link quality: the sequence numbers missed since the last packet count as lost (the remote
* counts only packets it sends: its telemetry slots leave no gap) */
int seq = p[0];
int gap = last_seq < 0 ? 1 : (uint8_t)(seq - last_seq);
if (gap > 100) gap = 100;
for (int i = 1; i < gap; i++) { hist[hn] = 0; hn = (hn + 1) % 100; }
hist[hn] = 1; hn = (hn + 1) % 100;
last_seq = seq;
int lq = 0;
for (int i = 0; i < 100; i++) lq += hist[i];
k_spinlock_key_t k = k_spin_lock(&lock);
for (int i = 0; i < 4; i++) rx.ch[i] = p[2 + 2 * i] | p[3 + 2 * i] << 8;
rx.flags = p[1];
rx.at = k_uptime_get();
rx.rssi = rssi; rx.snr = snr; rx.lq = lq; rx.ok++;
k_spin_unlock(&lock, k);
if (p[1] & RC_FLAG_TLM) {
uint8_t t[RC_LEN] = { (uint8_t)seq, (uint8_t)(0x80 | state), (uint8_t)(-rssi), (uint8_t)snr, (uint8_t)lq };
rc_seal(t);
sx_write_buf(t, RC_LEN);
sx_tx(0);
}
}
}
K_THREAD_STACK_DEFINE(radio_stack, 2048);
static struct k_thread radio;
static void set_motor(int k, float u)
{
if (u < 0.0f) u = 0.0f;
if (u > 1.0f) u = 1.0f;
pwm_set_pulse_dt(&motor[k], (uint32_t)(u * motor[k].period));
}Open in the simulator