8 - TUTORIAL - Control a brushless motor with a Teensy and the ESC
Video: https://youtu.be/3op6erX3gIk
#Robotics #Drones #Arduino #Hardware #Teensy_4Full code and manual on GitHub
Quadcopter frame PCB on OSHW lab
Table of Contents:
A) Introduction - components required
We are rapidly progressing with our video series, and this is the eighth video where we will try to control the motor through the Teensy microcontroller.
In this video, all components will be used except for the gyroscope.
Note: To simplify the concept, we will only use one electronic speed controller (ESC) and one motor.
B) How the ESC works
B.1) ESC Components and Circuit
The ESC is a microcontroller like your Teensy,
but it has one specific purpose: to be the commutator that adapts the voltage going to the motor to control the motor’s speed.
BEC
B.2) Motor Speed Modulation controlled by PWM Duty Cycle
The ESC does this by opening and closing the connection between the battery and the brushless motor, thereby controlling the average voltage provided to the motor.
You can control this average voltage through Pulse Width Modulation (PWM), meaning you only need to send a PWM signal from the Teensy to the ESC to control the brushless motor.
To control the motor’s speed, the ESC adjusts the duration of time that the MOSFETs remain open (duty cycle). It doesn’t change the switching frequency itself (which is tied to the rotor's position), but it modifies how long power is applied to the motor coils during each switching cycle. The higher the duty cycle, the more current flows, which generates greater torque and speed. The lower the duty cycle, the less current flows, reducing the motor's speed.
Example with an Arduino Uno:
In summary:
- The input PWM does not directly adjust the switching frequency of the MOSFETs; instead, it tells the ESC the desired speed.
- The ESC modulates the duty cycle of the signals controlling the MOSFETs, which determines how much energy is delivered to the motor, and thus controls the motor’s speed (RPM).
- Higher duty cycle = more power delivered = higher RPM. A lower duty cycle reduces the power being delivered to the motor, which decreases RPM.
This is why each motor requires a separate ESC—you need to control the speed of all motors independently.
C) Circuit and Schematic explained
We will reuse the circuit we built in part 6,
Read more: 6 - TUTORIAL - Take a brushless motor for a spin with a radio receiver
and the Teensy-receiver setup from part 7.
Read more: 7 - TUTORIAL - Connect a receiver to your Teensy (PWM and PPM signals)
The ESC needs to be connected to a Teensy pin capable of sending a PWM signal. You will use pins 1–4 to control all four motors.
For this example, connect the white signal cable of the ESC to pin 1 of the Teensy. The 5-volt and ground cables should be connected to the Teensy’s 5V and ground pins.
Note: the ESC will convert the battery voltage to a constant 5 volts, meaning that your Teensy will be powered directly by the battery.
Circuit Setup Instructions:
Starting with the circuit built in part 6:
- Disconnect the receiver from the ESC.
- Use three male-to-female jumper cables to connect the white signal cable, the red power cable, and the black ground cable to the PPM channel of the receiver.
As explained in part 7,
3. connect the white cable to Teensy pin 14, and the red and black cables to the 5V and ground pins.
4. Now, use three male-to-male jumper wires to connect the ESC to your Teensy:
- Connect the white signal cable to Teensy pin 1.
- Connect the red and black power cables to the 5V and ground pins on the Teensy.
D) Programming with Arduino
For the programming part, you will reuse a lot of the code from the previous video.
D.1) Input Throttle
In the first 18 lines, add a variable for the throttle.
The future value for this variable will lie between 1000 and 2000 microseconds.
D.2) PWM Resolution (12 bit)
You will send PWM signals from pin 1 of your Teensy to the ESC controlling motor 1.
Configuring pin 1 to send PWM signals can be done using the analogWriteFrequency()
function with the pin number and PWM frequency as parameters.
Note: Remember that the PWM frequency used for most ESCs is 250 Hz.
By default, the resolution of PWM signals sent by the Teensy is 8-bit, meaning the signal ranges between 0 and 255, which would give poor control.
You can change the resolution to 12-bit using the analogWriteResolution()
function.
For a frequency of 250 Hz (4000 microseconds), a value of 0 corresponds to 0 microseconds, and a value of 4095 corresponds to 4000 microseconds.
When you want to send a PWM signal in microseconds to the ESC, do not forget to multiply that value by (4095/4000), or 1.024, to convert it to the 12-bit equivalent.
D.3) Avoid undesired motor start
To avoid an uncontrolled motor start (for example, if the throttle stick was not in the lowest position after the last flight), add some lines to the setup process that check if the values sent from the throttle channel (receiver value 2) are greater than 1050 microseconds or lower than 1020 microseconds. If this condition is true, the code will not continue, and the system will stay in an infinite loop. This means that you need to move the throttle stick to its lowest value for the code to proceed and the motor to start.
D.4) Reading and Sending Throttle Values
In the loop section, read the throttle signal sent from the receiver.
The throttle values will range from 1000 microseconds (no throttle) to 2000 microseconds (full throttle).
Send this value to pin 1 (and subsequently the ESC and motor 1) using the analogWrite()
function. Remember to convert the throttle values from microseconds to their 12-bit equivalent by multiplying them by 1.024.
D.5) Testing the Setup
- Connect the Teensy to your computer. The receiver’s LED should start blinking.
- Turn on the radio transmitter. Once the receiver’s LED stays illuminated, it indicates that it is correctly bound to the transmitter.
-
Upload the code to your Teensy.
-
Connect the battery,
-
to hear the starting beeps from the ESC. Remember that you need to move the throttle stick to its lowest position for the code to continue.
Now, you’re ready to start the motor. Increase the throttle to test your electronic circuit.
Conclusion:
Congratulations! You have successfully controlled the motor through your Teensy microcontroller. In the next video, we will explore how you can manage the battery of your quadcopter. Don’t forget to subscribe if you’re enjoying this video series, and see you next time!
Z) 🗃️ Glossary
File | Definition |
---|