Want to Build Your Own Self-Balancing Robot?

By Christine Young, Blogger, Maxim Integrated


In other words, the Complementary filter combines the gyroscope data for very dynamic angular measurements with the accelerometer data for static angular measurements to produce a reliable feedback signal. See Figure 2 for sensor angle estimations.

Angle Estimates
Figure 2: Angle Estimates

Filter Results

Some of the benefits of using the MAX32630FTHR is the ARM Cortex-M4F CPU at the heart of the MAX32630 MCU, 512K of SRAM available to the application, and the microSD card slot available for data logging. To generate the following plots I recorded 14400 samples of the accelerometer horizontal and vertical axis, the gyroscope x-axis, and the pulse width (PWM +D.C.) sent to each motor. The sensor data is saved as floats and the pulsewidth as a signed 32 bit integer. Each of these variables takes four bytes a piece, so for total RAM used for data logging we have the following:

RAM = ((4bytes/var * 4vars) * num_samples) = 230.4KB

Which is only 45% of the available RAM. With a sample rate of 1.25ms (800Hz), 14400 samples ends up being 18 seconds worth of data. The actual size of the file saved to the SD card ends up being considerably larger due to additional data be calculated from the sensor data recorded. See the function ‘saveData’ in the mbed code below.

Figure 3 shows the results of manipulating a MAX32630FTHR by hand.

Dynamic measurement showing sensor data and filter output
Figure 3: Dynamic measurement showing sensor data and filter output

This measurement was an attempt to show the filters ability to remove the drift associated with the gyro and filter out the noise associated with the accelerometer while still providing a reliable pitch estimate for the feedback signal.

Below is a description of what actions were done in sequential order to generate the plot shown in Figure 3. Figures 4 -6 are zoomed in versions of Figure 3.

  • From ~0.7 to 4 seconds the pcb was rolled on the x-axis of the gyroscope to produce an angular velocity. You can see from Figure 4 that the output of the filter tracks the integration of the gyroscope data while filtering out the accelerometer data.

    Rolling pcb
    Figure 4: Rolling pcb
  • From ~4 seconds to 4.75 seconds there was relatively no movement.
  • From ~4.75 to 5.75 the pcb was shaken laterally through the y-axis of the accelerometer. Figure 5 once again shows the accelerometer data being filtered. The coefficient associated with the complementary filter can probably be increased closer to one to remove the small ripples on the filter output due to the accelerometer data in this timeframe.
Figure 5: Shaking pcb
  • From ~5.75 to 7 seconds there was relatively no movement.
  • From ~7 seconds on, the pcb was held at a constant pitch of ~12 degrees. Figure 6 shows relatively static movement of the pcb, so we can see a definite drift in the integration of the gyro data, however, our filter output tracks the accelerometer data at 12 degrees. The filter output, our feedback signal, never approaches the setpoint because these measurements were taken by manually manipulating the attitude of a MAX32630FTHR board not installed on the robot.
Tilting pcb
Figure 6: Tilting pcb

Figures 7 and 8 are plots of data recorded while running the robot. In Figure 7, the accelerometer data is gray despite the legend stating green. The accelerometer data was selected when creating the image, making it easier to see the gyro data and filter output.

Sensor data and filter output
Figure 7: Sensor data and filter output
PWM duty cycle top; feedback signal bottom
Figure 8: PWM duty cycle top; feedback signal bottom

Tuning The Loop

One note on tuning this loop. I found it easier to adjust the coefficient for the Complementary filter with the MAX32630FTHR removed from the robot. With the pcb removed, you can manipulate the board as you will and see how the output of your filter tracks the individual sensor angle measurements. The coefficient of the filter should be less than one as mentioned in the articles, but how much less than one depends on your sample rate and the desired time constant of the filter. Once your filter is behaving correctly, then you can tune the PID loop. There is no point in tuning the PID loop if the feedback path isn’t working.

Next Steps

  • Address backlash due to gearbox imperfections
  • Integrate Ultrasonic sensor for maintaining distance from object
  • Integrate sensor for line following while balancing
  • Design app specific PCB using MAX32630

ARMmbed Program


MAX32630FTHR Balance Bot Schematic
MAX32630FTHR Balance Bot Schematic



  1. Nice article but oddly disembodied. I think it is weird that the engineer is not identified, without explanation. If the article is about the design, then no need to mention the engineer at all, but since you talk about it from the engineer’s angle, why not give him the credit?

    Design has become commoditized but the creative work is still done by people. Until the robots take over, let’s salute the humans.


Please enter your comment!
Please enter your name here