Blue Pill self-levelling servo platform
Build it step by step
0 of 7 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Wire the sensor
SDA to PB7, SCL to PB6, power at 3.3 V.
Wire the servo
Signal to PA1, power from the separate supply, grounds joined.
Fix the sensor to the platform
X axis along the servo shaft.
Upload the sketch
Hold the base level while it starts.
Tilt the base slowly
The platform stays level.
If it tilts the wrong way
Flip the sign in the angle line.
How do you build a blue Pill self-levelling servo platform?
A Blue Pill reads the tilt of an MPU6050 and turns a servo to cancel it, so a small platform stays level as its base is tilted. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), MPU6050 IMU, SG90 Micro Servo and Breadboard 830 Tie Points. It takes 7 steps and about 170 minutes.
- Level
- Intermediate
- Time
- About 170 min
- Parts
- 5
Parts list
- Development BoardsBuy
- InterfacesBuy
- SensorsBuyMPU6050 IMU × 1
- Motors & ActuatorsBuySG90 Micro Servo × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| MPU6050 VCC | Blue Pill 3.3 pin | Power at 3.3 V |
| MPU6050 GND | Blue Pill GND | Ground |
| MPU6050 SDA | Blue Pill PB7 | I2C data |
| MPU6050 SCL | Blue Pill PB6 | I2C clock |
| Servo signal | Blue Pill PA1 | Pulse output |
| Servo power (red) | Separate 5 V supply, not the board | A servo can pull hundreds of milliamps |
| Servo ground | Supply ground and Blue Pill GND | Common ground |
Code
This is the simplest kind of control loop: measure, compare, correct. An MPU6050 accelerometer measures gravity, so when the base is still it knows how far it is tilted. The sketch turns two of the three readings into a roll angle, smooths it so vibration does not move the servo, and then moves the servo the opposite way by the same number of degrees, which cancels the tilt of the platform. At start-up it measures the level position over half a second, so hold the base level while it powers on. The travel is limited to 60 degrees either side to keep the servo and the linkage safe. This only works while the base moves slowly, because quick movements add acceleration that the sensor cannot tell from gravity. The servo needs its own 5 V supply, and the grounds must be joined. If the platform tilts twice as much instead of cancelling, flip the sign in the angle line.
Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Servo.h>
const uint8_t MPU = 0x68;
const int SERVO_PIN = PA1;
Servo plat;
float smooth = 0, zeroRoll = 0;
bool readRoll(float °) {
Wire.beginTransmission(MPU);
Wire.write(0x3B); // first accelerometer register
if (Wire.endTransmission(false) != 0) return false;
Wire.requestFrom(MPU, (uint8_t)6, (uint8_t)true);
if (Wire.available() < 6) return false;
int16_t x = (Wire.read() << 8) | Wire.read();
int16_t y = (Wire.read() << 8) | Wire.read();
int16_t z = (Wire.read() << 8) | Wire.read();
(void)x;
deg = atan2((float)y, (float)z) * 180.0 / PI; // roll about the X axis
return true;
}
void setup() {
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); Wire.write(0); // wake the sensor
Wire.endTransmission(true);
plat.attach(SERVO_PIN, 700, 2300);
plat.write(90);
delay(1500); // hold the base level while it starts
float a, sum = 0; int n = 0;
for (int i = 0; i < 50; i++) { if (readRoll(a)) { sum += a; n++; } delay(10); }
if (n) { zeroRoll = sum / n; smooth = zeroRoll; }
}
void loop() {
float a;
if (readRoll(a)) smooth = 0.9 * smooth + 0.1 * a; // low-pass filter
float tilt = smooth - zeroRoll;
int angle = 90 - (int)tilt; // turn the other way to cancel the tilt
angle = constrain(angle, 30, 150); // keep inside a safe range
plat.write(angle);
delay(10);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Base level at power-up | Platform level |
| Tilt the base slowly by 20 degrees | Platform stays close to level |
| Tilt past 60 degrees | Servo stops at its limit |
| Quick shake | Brief error, then it settles |
| Servo on its own supply | Board does not reset when the servo moves |
If it does not work
Platform tilts the wrong way
Flip the sign in the angle line.
Servo twitches
Increase the smoothing factor or add a capacitor across the servo supply.
No reading
Check SDA and SCL on PB7 and PB6 and the address 0x68.
Board resets
Power the servo from its own supply.
Upload fails
Check the ST-Link wires and boot jumpers.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the blue Pill self-levelling servo platform?
STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), MPU6050 IMU, SG90 Micro Servo and Breadboard 830 Tie Points.
How long does the blue Pill self-levelling servo platform take to build?
About 170 minutes for a intermediate maker, in 7 steps.
How do I know the blue Pill self-levelling servo platform works?
Base level at power-up: Platform level. Tilt the base slowly by 20 degrees: Platform stays close to level. Tilt past 60 degrees: Servo stops at its limit.
Platform tilts the wrong way: what should I check?
Flip the sign in the angle line.
Servo twitches: what should I check?
Increase the smoothing factor or add a capacitor across the servo supply.
No reading: what should I check?
Check SDA and SCL on PB7 and PB6 and the address 0x68.
Board resets: what should I check?
Power the servo from its own supply.
