I’ve recently been getting interested in electronics again, so I dusted off the Elegoo Uno R3 kit and took apart a cheap drone from Aliexpress to understand how it functions via reverse engineering. Unfortunately, the microcontroller in the drone was a closed system. Apart from monitoring voltages to the MOSFET gates that control the drone motor speed, there wasn’t much interesting I could do with it.

My end goal in all this is to build and modify a custom FPV drone. I began looking at some flight controllers, but felt like I would be skipping over some interesting learning. Instead, opting to build a basic flight controller from scratch with an Elegoo Nano.

I plan to build the features piece by piece to get a deeper understanding of what’s going on inside the drone’s flight controller. The first challenge I wanted to tackle,which I discuss in this post is controlling motors using an analogue joystick. 

Joysticks allow fine grain control over motor acceleration by controlling the voltages in a system. These voltages can then be mapped to a speed function, which is then translated to motor speed using pulse width modulation (PWM) delivery.

What I Used:

  1. Joystick Analogue
  2. 3.7V LiPo battery
  3. Elegoo Nano
  4. 220Ω Resistor
  5. PN2222A Transistor
  6. Diode Rectifier
  7. Breadboard
  8. Coreless DC Motor

First thing is to plug the Nano across the centre gap of the breadboard.

I started by hooking up the joystick analogue.

  • 5V goes to the 5V pin on the Nano.
  • GND into the breadboards negative side of the power rail.
  • VRx goes to A0 analog pin on the Nano.
  • VRy goes to A1 pin on Nano (optional)
  • SW is not necessary in this experiment

Then connect the Nano’s GND pin (on the analogue side) to the breadboards negative side of the power rail.

With everything hooked up, you can upload this sketch with the Arduino IDE.

				
					const int xPin = A0;
const int yPin = A1;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int xVal = analogRead(xPin);
  int yVal = analogRead(yPin);

  Serial.print("X: ");
  Serial.print(xVal);
  Serial.print("  Y: ");
  Serial.println(yVal);

  delay(100);
}
				
			

If you open up the serial monitor tab in Arduino IDE you should now see the joystick positions output. Wiggle it around to get an idea of the values assigned to different positions. Typically, the values start range from 0-1023. At rest, the values should read at around 512.

All that’s left is to hook up a motor.

Plug the batteries + positive terminal into the power rail of the breadboard (the opposite side of the one being used by the Nano GND pin).

Connect the GND pin from the Nano’s  digital side into the breadboards negative rail.

Then run the 220Ω resistor into the Nano’s D9 pin, and then to the Base connector of the transistor (see the link above for the transistors data sheet).

The transistors emitter pin goes to the GND in the breadboard rail. And the collector pin connects with the motor’s negative terminal.

The motors + positive side needs to be connected to the breadboards power rail. To ensure there’s no voltage kickback to the transistor, a diode should be wired in parallel.

Plug the diodes’ cathode in series with the motor positive terminal, and the diodes’ anode to the breadboards negative rail. This will stop any voltage spikes frying the transistor.

To complete the circuit now connect the motor’s negative side to the transistors’ collector pin.

Here’s the Arduino sketch to convert joystick values into power delivery to the motor.

				
					const int pwmPin = 9;
const int analogPin = A0;

void setup() {
  pinMode(pwmPin, OUTPUT);
  digitalWrite(pwmPin, LOW);
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(analogPin);
  int speed;

  if (raw <= 508) {
    speed = map(raw, 0, 508, 255, 0);
  } else {
    speed = 0;
  }

  analogWrite(pwmPin, speed);

  Serial.print("Joystick: ");
  Serial.print(raw);
  Serial.print(" | Motor Speed: ");
  Serial.println(speed);

  delay(50);
}
				
			

If you move the joystick to the left along the X axis, the motor will start spinning. You’ll notice the speed can be controlled smoothly using the joystick rather than just off/on functionality.

Real cool to see this in action, and it’s got me excited to experiment with more functionality.