FlatFlap is a compact actuator made to flap, however in this guide we will focuses on position control - keeping the flap in a fixed position using a controlled duty cycle. This method is useful for applications that require the flap to stay in a specific angle for long periods of time, instead of continuous oscillation.
FlatFlap operates by passing current through its coil, generating a magnetic field that interacts with its magnet. Instead of applying a short pulse or oscillating the square wave, here, we are going to use Pulse Width Modulation (PWM) to hold the flap at a desired angle.
The duty cycle of the PWM signal controls the strength of the magnetic field, thus changing the angle of the flap.
Several factors influence position accuracy and stability:
If you're using the DriveCell library, the following example demonstrates how to set different positions:
#include <drivecell.h>
#define IN1_pin1 2
#define IN1_pin2 3
DriveCell FlatFlap1(IN1_pin1, IN1_pin2);
void setup() {
FlatFlap1.Init();
}
void loop() {
FlatFlap1.Drive(true, 100); // Maximum hold strength
delay(3000);
FlatFlap1.Drive(true, 75); // Hold with 75% power
delay(3000);
FlatFlap1.Drive(true, 50); // Hold with 50% power
delay(3000);
FlatFlap1.Drive(true, 25); // Hold with 25% power
delay(3000);
}
This code gradually adjusts the duty cycle to hold the flap at different positions.
Understanding the Functions:
Init()
→ Initializes DriveCell and sets up the input pinsDrive(bool direction, uint8_t power_percent)
true
(north) / false
(south)⚠ Note: The Drive() function uses a high-speed PWM timer, making it compatible only with CodeCell and ESP32-based devices.
If you're not using an ESP32 device, you can adjust PWM in Arduino using the following code. However, ensure that the waveform frequency is set correctly.
#define FLAP_PIN1 2
#define FLAP_PIN2 3
void setup() {
pinMode(FLAP_PIN1, OUTPUT);
pinMode(FLAP_PIN2, OUTPUT);
digitalWrite(FLAP_PIN2, LOW);
}
void loop() {
analogWrite(FLAP_PIN1, 191); // 75% Duty Cycle (191/255)
digitalWrite(FLAP_PIN2, LOW);
delay(5000); // Hold for 5 seconds
analogWrite(FLAP_PIN1, 127); // 50% Duty Cycle
delay(5000);
analogWrite(FLAP_PIN1, 63); // 25% Duty Cycle
delay(5000);
}
By using PWM the FlatFlap can maintain specific angles for long peroids of time, making this feature useful for robotics, haptics, and art. Check out the DriveCell GitHub Repository for more code examples and technical documentation!
Join our Community ~ Be the first to know about new products and get exciting deals!
© 2025 Microbots.