Dancing Segway

Group project with Anusha Sonathalia, Ellie Peatman and Rowan Dixon.

A segway that self-balances and dances to music using a PyBoard microcontroller, PID control and beat detection.

Electronics •  Coding  •  Signal Processing & Analysis  •  Electronic System Design  •  Testing  •  Teamwork  •  Python  •  MatLab  •  PyBoard

The aim of this project was to develop our understanding of the theory and implementation signal processing and electrical systems.


The segway (designed by Electronics Professor Peter Cheung) uses a PyBoard running Python scripts and has various inputs (microphone, gyroscope and more) and outputs (DC motors, a small OLED screen and more). 


Balancing is achieved using Proportional-Integral-Derivative (PID) control which adjusts the motor speeds to compensate for the segway tipping. In addition, a beat-detection algorithm uses an Interrupt Service Routine to adjust the desired pitch angle of the segway whenever the energy of the music is high enough above a certain level. This causes the segway to 'dance'.

Technical Features

The first two graphs below show testing and analysis of the gyroscope and accelerometer in MatLab. It was necessary to combine both and pass them through a complimentary filter to remove the errors from each. 


The third graph shows the energy level of a sample of the BeeGee’s ‘Staying Alive’. The energy peaks roughly fall on the beat.

More on PID control and the algorithm

The PID control causes the motor speeds to adjust as the segway senses from an accelerometer and gyroscope that it is tipping away from its desired pitch angle (normally perfectly upright). The adjusted motor speeds cause the segway to move in the direction it is tipping, thus righting itself and avoiding falling. 


The adjustment is dependent on how far the segway is tipping (proportional), how fast it is tipping (derivative) and a summation over time of the ‘error’ — its set angle minus how far away from it it is tipping (integral). How much each of these terms affects the adjustment needs to be fine-tuned to achieve stable balancing - when doing this the algorithm would initially request user-input for the gain of each term, achieved by reading a potentiometer on the PyBoard.


The algorithm running on the PyBoard also detects musical beats by measuring the energy level of the music and identifying when this is significantly above average (in pop/rock music this is typically on the beat). 


When beats are detected an Interrupt Service Routine (ISR) is used to briefly stop the balancing algorithm so that the segway’s target angle can be changed. When the angle is not directly upright, the segway constantly moves in the direction it is tipping to maintain it’s angle, thus ‘dancing’. 


Various dance ‘moves’ were based on different target angles, and were selected based on further parameters in the music, such as the extent to which the energy was higher than average, or how much high- or low- end frequencies are currently present in the music.

The third graph shows the energy level of a sample of the BeeGee’s ‘Staying Alive’. The energy peaks roughly fall on the beat.