Projects / Blue Pill self-levelling servo platform

Blue Pill self-levelling servo platform

Intermediate2.8 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V.

  2. STM32

    Wire the sensor

    SDA to PB7, SCL to PB6, power at 3.3 V.

  3. STM3259°

    Wire the servo

    Signal to PA1, power from the separate supply, grounds joined.

  4. STM3259°

    Fix the sensor to the platform

    X axis along the servo shaft.

  5. STM32

    Upload the sketch

    Hold the base level while it starts.

  6. STM32

    Tilt the base slowly

    The platform stays level.

  7. STM32

    If it tilts the wrong way

    Flip the sign in the angle line.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ST-Link GNDBoard GNDGround
ST-Link SWDIOBoard DIODebug data
ST-Link SWCLKBoard DCLKDebug clock
ST-Link 3.3VBoard 3.3 pinPower from the programmer (do not also plug in USB)
MPU6050 VCCBlue Pill 3.3 pinPower at 3.3 V
MPU6050 GNDBlue Pill GNDGround
MPU6050 SDABlue Pill PB7I2C data
MPU6050 SCLBlue Pill PB6I2C clock
Servo signalBlue Pill PA1Pulse output
Servo power (red)Separate 5 V supply, not the boardA servo can pull hundreds of milliamps
Servo groundSupply ground and Blue Pill GNDCommon 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 &deg) {
  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.

CheckExpected
Base level at power-upPlatform level
Tilt the base slowly by 20 degreesPlatform stays close to level
Tilt past 60 degreesServo stops at its limit
Quick shakeBrief error, then it settles
Servo on its own supplyBoard 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 →

Ask Lark AI

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.