Blue Pill light-controlled tone
Build it step by step
0 of 6 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Build the divider
Photoresistor from 3.3 to PA0, 10 kilo-ohm from PA0 to GND.
Wire the buzzer
PB8 through 330 ohms to the plus, minus to GND.
Upload the sketch
Wave your hand across the sensor for 3 seconds.
Move your hand over the sensor
The pitch rises and falls.
Power-cycle in a different light
The range recalibrates.
How do you build a blue Pill light-controlled tone?
A Blue Pill turns the light on a photoresistor into the pitch of a passive buzzer, so waving a hand over it plays notes. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), LDR Photoresistor, Resistor 10 kΩ, Passive buzzer, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 6 steps and about 140 minutes.
- Level
- Intermediate
- Time
- About 140 min
- Parts
- 7
Parts list
- Development BoardsBuy
- InterfacesBuy
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 1
- Motors & ActuatorsBuyPassive buzzer × 1
- PassivesBuyResistor 330 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| Photoresistor one leg | Blue Pill 3.3 pin | Top of the divider |
| Photoresistor other leg and 10 kilo-ohm resistor | Blue Pill PA0 | Divider output |
| 10 kilo-ohm resistor other leg | Blue Pill GND | Bottom of the divider |
| Passive buzzer plus | 330 ohm resistor to Blue Pill PB8 | Tone output |
| Passive buzzer minus | Blue Pill GND | Return |
Code
A photoresistor changes its resistance with light, and put in series with a fixed resistor it makes a voltage divider whose output rises as the light gets brighter. The ADC turns that voltage into a number, and the sketch maps the number to a frequency between 200 and 2000 hertz and plays it on a passive buzzer. A passive buzzer has no oscillator of its own, so the board supplies the tone, which is why the pitch can change. For the first three seconds after power-up the sketch watches the lowest and highest readings while you move your hand over the sensor, and uses those as the ends of the range, so it adapts to a bright room or a dim one. If you do not move, it uses a default range. The 330 ohm resistor limits the current from the pin into the buzzer. Keep the sound modest, hold the buzzer away from your ears, and switch it off if it becomes tiring.
Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const int LDR_PIN = PA0;
const int BUZZER = PB8;
int lo = 4095, hi = 0;
void setup() {
analogReadResolution(12);
unsigned long start = millis();
while (millis() - start < 3000) { // calibrate: move your hand over the sensor
int v = analogRead(LDR_PIN);
if (v < lo) lo = v;
if (v > hi) hi = v;
delay(5);
}
if (hi - lo < 200) { lo = 300; hi = 3500; } // fall back if there was no movement
}
void loop() {
int v = analogRead(LDR_PIN);
int f = map(v, lo, hi, 200, 2000); // brighter light gives a higher pitch
f = constrain(f, 200, 2000);
tone(BUZZER, f);
delay(20);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Cover the sensor | Low pitch |
| Uncover it | Higher pitch |
| No movement during start-up | Default range is used |
| Hand moves slowly | Pitch glides smoothly |
| Different room light | Recalibrates at start-up |
If it does not work
No sound
Check the buzzer is a passive one and wired to PB8.
Pitch does not change
Check the divider and that the wiper goes to PA0.
Pitch is reversed
Swap the photoresistor and the resistor.
Noisy pitch
Add a 100 nanofarad capacitor from PA0 to GND.
Upload fails
Check the ST-Link wires and boot jumpers.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the blue Pill light-controlled tone?
STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), LDR Photoresistor, Resistor 10 kΩ, Passive buzzer, Resistor 330 Ω and Breadboard 830 Tie Points.
How long does the blue Pill light-controlled tone take to build?
About 140 minutes for a intermediate maker, in 6 steps.
How do I know the blue Pill light-controlled tone works?
Cover the sensor: Low pitch. Uncover it: Higher pitch. No movement during start-up: Default range is used.
No sound: what should I check?
Check the buzzer is a passive one and wired to PB8.
Pitch does not change: what should I check?
Check the divider and that the wiper goes to PA0.
Pitch is reversed: what should I check?
Swap the photoresistor and the resistor.
Noisy pitch: what should I check?
Add a 100 nanofarad capacitor from PA0 to GND.
