Actuators
The word “actuators” is just a fancy word for “devices that create motion,” which can include anything ranging from hydraulic pistons to motors. In our case, electric motors will be the key to your robot’s motion! In this competition, you will be dealing with servo motors.
DC Motors
You will be using DC (direct current) motors as the primary means of moving your robot around. However, directly connecting a DC motor to the ESP32 may not be desirable due to lack of direction control and higher voltage/current requirements of the motor. This is where motor controllers come into play.
Motor Controllers
A motor controller, such as the L298N, acts as an intermediary between the microcontroller and the DC motor. It allows the microcontroller to control the motor’s speed and direction without directly handling the high current. The motor controller has the following key components:
- H-Bridge Circuit: This allows the motor to be driven in both forward and reverse directions. Different combinations of IN1 and IN2 will determine the spin direction.
- PWM (Pulse Width Modulation) Control: This allows speed control by varying the duty cycle of the PWM signal.
- Power Supply Terminals: These provide the necessary power to the motor.
- 5V Voltage Regulator: This allows the motor controller to act as a 5V output power supply. This functionality can be disabled by removing the corresponding jumper (see below)
The following information is an adapted version of this guide.
How to Use Motor and Motor Controller?
Battery Pack Power
First we will go over using an external power source for the ESP32. For this competition, you will be able to control your robot with a wireless game controller to navigate around the field (sensor challenges should be completed autonomously). It would be impractical to power your ESP32 through USB on the field, so you will use a battery pack. We need to reduce the voltage of the battery pack down to a level the ESP32 can safely use through the motor controller’s 5V voltage regulator.
Here is a diagram of how your circuit should look when using the battery pack:
- Notice how the blue lines are shared between all components so that they have a common ground
However, if you want to be able to use your motors and plug your ESP32 into your laptop to use the serial monitor for debugging, then you’ll have to change the wiring (or else you will fry your equipment!):
- Note how the line from the motor controller’s 5V line is now gone!
You should now be able use your USB connection safely!
Do NOT power your ESP32 through its 5V pin with the battery pack while simultaneously connecting it to your computer through USB! This may irreversibly damage your devices (only do one at a time).
To control a DC motor using the ESP32, a motor controller, and battery pack (WITH USB CONNECTION), you can connect them together as shown below:
- You will need to remove the jumper shorting the ENA pin to 5V
- You will need to use a screwdriver to clamp the power wires in place (all the wires connecting to the blue screw terminals) into place. Make sure these are secure because wires coming loose and causing a short circuit could be catastrophic!
- Note that the wire polarity on the motor does not matter because DC motors’ spin direction is based on the direction of current flowing through them.
Motor Controller Terminal | ESP32 Pin |
---|---|
5V | 5V |
IN1 | Any GPIO |
IN2 | Any GPIO |
ENA | Any PWM enabled pin |
If you’re not sure about the ESP32 pinout, then check out this page!
The following is an example of configuring and running the motor in software:
#include "sdkconfig.h"
#include <Arduino.h>
#define IN1 27 // Control pin 1
#define IN2 14 // Control pin 2
#define ENA 12 // PWM pin
void setup() {
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
}
void loop() {
// Spin motor
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 255);
delay(1000); // Run for 1 second
// Stop motor
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
delay(1000); // Stop for 1 second
}
Again, more detailed information about the L298N motor controllers can be found here!
Servo Motors
What Are Servos?
Servos are motors that are designed for precise position control. Instead of freely rotating when powered, servos listen to a control signal (usually PWM) to determine where to rotate. Some servos offer limited rotation, while others can rotate continuously (like ours).More advanced servos have other ways to be even more precise such as a feedback system on top of the control signal, but that is not necessary for this competition.
How to Use Servos?
For the servos in our competition, you can use it to precisely control your mechanism for the Mechanical Challenge. To interface it with your ESP32, you will connect the wires as follows:
Servo Wire | ESP32 Pin |
---|---|
Red | 5V |
Black | GND |
White | Any PWM capable pin |
If you’re not sure about the ESP32 pinout, then check out this page!
In this competition, we will be using the Arduino servo library to control the servos. The following is an example of how to spin a servo using the ESP32’s pin 2 as the PWM output.
#include "sdkconfig.h"
#include <Arduino.h>
#include <ESP32Servo.h>
Servo myServo;
void setup() {
myServo.attach(2);
myServo.write(1750);
}
void loop() {
}
Simply input a different value in the write()
function to change how the servo behaves!