Learn. Create. Enjoy.
Learn how to assemble the Fork Lift add-on for Stackr and program the servo motor that drives the parallelogram lifting mechanism.
The easiest way to assemble the Fork-Lift is by following the instructions in the video below.
This example uses the ESP32Servo library to drive the fork lift servo. It sweeps through 0°, 90°, and 180° so you can verify wiring and motion before tuning angles for your build. This program has been created just to test if the servo works and to get you familiar with the ESP32Servo library. You can use it with the servo connected to Stackr without assembling the fork lift add-on.
#include <ESP32Servo.h>
Servo forkServo;
void setup()
{
Serial.begin(115200);
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
// Initialize servo at 90 degrees
forkServo.setPeriodHertz(50);
forkServo.attach(D10);
forkServo.write(90);
}
void loop()
{
forkServo.write(0);
delay(1000);
forkServo.write(90);
delay(1000);
forkServo.write(180);
delay(1000);
}
Library Inclusion: The ESP32Servo library manages PWM timers on the ESP32 so hobby servos run reliably on the Arduino Nano ESP32.
#include <ESP32Servo.h>Timer Allocation: Four hardware timers are reserved for servo PWM channels before the servo is attached.
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);Servo Setup: The servo is configured at 50 Hz, attached to pin D10, and centered at 90° in setup().
forkServo.setPeriodHertz(50);
forkServo.attach(D10);
forkServo.write(90);Loop Pattern: The fork servo sweeps 0° → 90° → 180° with a one-second pause at each position, then repeats.
forkServo.write(0);
delay(1000);
forkServo.write(90);
delay(1000);
forkServo.write(180);This example uses a set of cycles to create a smooth motion for the fork lift. A control_mode state machine steps the angle slowly between 45° and 135°, then returns to center before repeating.
#include <ESP32Servo.h>
Servo forkServo;
int angle = 90;
int control_mode = 0;
void setup()
{
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
forkServo.setPeriodHertz(50);
forkServo.attach(D10);
forkServo.write(angle);
}
void loop()
{
if (control_mode == 0) {
if (angle < 135) {
angle++;
forkServo.write(angle);
delay(50);
} else {
control_mode = 1;
}
}
else if (control_mode == 1) {
if (angle > 45) {
angle--;
forkServo.write(angle);
delay(50);
} else {
control_mode = 2;
}
}
else if (control_mode == 2) {
if (angle < 135) {
angle++;
forkServo.write(angle);
delay(50);
} else {
control_mode = 3;
}
}
else if (control_mode == 3) {
if (angle > 45) {
angle--;
forkServo.write(angle);
delay(50);
} else {
control_mode = 4;
}
}
else if (control_mode == 4) {
if (angle < 90) {
angle++;
forkServo.write(angle);
delay(50);
} else {
control_mode = 0;
}
}
}
State Variables: angle tracks the current servo position; control_mode selects which part of the motion cycle is running (0 through 4).
int angle = 90;
int control_mode = 0;Mode 0 — Raise: Increment angle from 90° up to 135°.
Mode 1 — Lower: Decrement angle from 135° down to 45°.
Mode 2 — Raise again: Same upward sweep to 135° (second cycle in the dual-servo version).
Mode 3 — Lower again: Same downward sweep to 45°.
Mode 4 — Return to center: Increment angle until 90°, then restart at mode 0.
else if (control_mode == 4) {
if (angle < 90) {
angle++;
forkServo.write(angle);
delay(50);
} else {
control_mode = 0;
}
}Install the ESP32Servo library from the Arduino Library Manager (Sketch → Include Library → Manage Libraries, then search for “ESP32Servo”).