Home Resources Contact

Tilt Theremin

Now that we have a speaker hooked onto the Micro:bit, let's try something a little different. As we mentioned before, there is something called an accelerometer in the Micro:bit. We want to turn the information from the accelerometer (how much the Micro:bit is tilted) into a frequency (pitch) for the sound. To do this we need to think about the numbers we're getting in and the numbers we're wanting to put out. This gets a bit messy. It's a really useful technique, but if the words don't make sense, just try putting in the code and playing with the numbers.

The accelerometer will measure an acceleration (any time the velocity changes), but it will also measure the pull of gravity. It does this on three axes: x (left-right), y (forward-backward), and z (rotations, sort of). For each of these values it will return a number. As long as you're not shaking it much, just tilting the Micro:bit left and right makes the x-value of the acceleration go from about -1000 to about 1000 (0 is flat).

We want to turn this number into a range of frequencies (pitches) that we can hear. There is a convenient block to do this called the Map Block under the Math menu. We will take the input from the x-part of the acceleration. If the acceleration is -1000, it will turn into 200 (200 Hz, just below middle C). If the acceleration is 1000, it will turn into 800 Hz (two octaves higher). If it is halfway in between (0), the frequency will be half way in between (500 Hz).

The other thing we're going to do is nest 3 different blocks together, so take a deep breath and let's give this a try:

The end result should look like this:




Give it a try!

The program will be running as fast as it can, which means the frequency may be a little choppy, so we'll give it a little bit of time to sit on a single frequency. In the following program, we've added a Delay Block from the Basics menu and set the delay to 25 milliseconds.



Let's try one last trick. Let's use the forward-backward tilt to control the volume of the speaker, which can range from 0 to 255. We'll use the Acceleration (Y) Block, the Map Block and the Set Volume Block (from the Music menu).



You can play with the numbers in the map function a bit to change the range of volumes and the range of frequencies.