Motors singing in harmony - Pachelbel's canon

A project log for Ballbot

A dynamically stable omnidirectional robot that balances on a 22cm ball

Brian ChenBrian Chen 12/16/2015 at 08:050 Comments

[this post was first published on my blog @]

Technically I did this last Saturday, but I was too lazy to upload the video with my ridiculously slow internet.

Anyway, here it is: the ballbot playing Pachelbel's canon (aka Canon in D) with it's motors by changing the PWM frequency for the motor controllers! Only the first dozen or so bars are played since this is one of my earlier revisions of the program.

And yes, I still don't have my 18650 battery holders, so I'm using a 4s lipo.

Unfortunately, most of the PWM pins on the Teensy correspond to one FTM timer, and so the PWM frequency of any of the 3 motors needs to be the same at the same time. This explains why no two motors are ever playing at the same time.

BoardTimerPWM PinsDefault Frequency
Teensy 3.1 & 3.2FTM05, 6, 9, 10, 20, 21, 22, 23488.28 Hz
FTM13, 4488.28 Hz
FTM225, 32488.28 Hz

Eventually I stopped playing with the music at the end of the day, and I did get get two motors playing different notes at the same time by using a separate timer, FrequencyTimer2.

On the software side of things, I used an interrupt that acted as a metronome, except my metronome was ticking at every semiquaver instead of crotchet. I treated the semiquaver as the fundamental unit of time and created 3 large 2D arrays for each of the motors to play (the first element of each row corresponding to the frequency and the second

element corresponding to the number of semiquaver beats to be played). You can find all the functions and routines I wrote here.

WARNING: If you try this, your motor controllers can get VERY HOT due to the low frequencies that you need to be operating at. See Eventually, one of my motor controllers burned out the day after, though I'm not sure whether this was the reason.