This page describes how to make a basic noise maker. This is a classic electronics and microcontroller project. You connect up a light dependent resistor and a buzzer. You get the buzzer to change pitch depending on the amount of light hitting the sensor.
There are other ways to configure a light dependent resistor. This setup worked well for me. A speaker is shown where the buzzer is. Treat it the same way with whatever you are using to buzz and connect the - to GND and the + to pin 0.
Before you flash this program, press the REPL button. It is used to test that we get a number from our LDR that can be used easily with the buzzer. I had to multiply the reading by 2 to get a high enough frequency to pass to my buzzer. Depending on the amount of light and your component, you might get different readings.
from microbit import * while True: light = pin1.read_analog() light = light * 2 print (light) sleep(50)
This program adds the buzzing effect. You have to be pressing button A for the noise to be made. I like to have some way to stop the noise completely.
from microbit import * import music while True: if button_a.is_pressed(): light = pin1.read_analog() light = light * 2 print (light) music.pitch(light,-1) else: music.stop()
You can change the behaviour of the noise maker by spending a bit more time thinking about the frequency that you want to play. Instead of just playing the raw frequency or some multiple of it as we have here, it would be good to play an actual note. You can do this by making a list of the note frequencies you want. Those are shown in the table at the bottom of this page.
The main programming task for this is to successfully choose a note to play based on the reading from the sensor. Arithmetic is your friend. Use the first example program and record all of the information you can about the readings you are getting. Choose your notes carefully. A couple of octaves works well, but you might find a more interesting scale to use.
When you crack the problem of getting the right note, start to think about features. You could go for a relatively small number of notes with button presses or other interactions changing the octave. You could work out how to bend the note.
Light show on the LED matrix to finish the whole project off nicely.