Projects / Blue Pill light-controlled tone

Blue Pill light-controlled tone

Intermediate2.3 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V.

  2. STM32852 lux

    Build the divider

    Photoresistor from 3.3 to PA0, 10 kilo-ohm from PA0 to GND.

  3. STM32

    Wire the buzzer

    PB8 through 330 ohms to the plus, minus to GND.

  4. STM32

    Upload the sketch

    Wave your hand across the sensor for 3 seconds.

  5. STM32

    Move your hand over the sensor

    The pitch rises and falls.

  6. STM32

    Power-cycle in a different light

    The range recalibrates.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ST-Link GNDBoard GNDGround
ST-Link SWDIOBoard DIODebug data
ST-Link SWCLKBoard DCLKDebug clock
ST-Link 3.3VBoard 3.3 pinPower from the programmer (do not also plug in USB)
Photoresistor one legBlue Pill 3.3 pinTop of the divider
Photoresistor other leg and 10 kilo-ohm resistorBlue Pill PA0Divider output
10 kilo-ohm resistor other legBlue Pill GNDBottom of the divider
Passive buzzer plus330 ohm resistor to Blue Pill PB8Tone output
Passive buzzer minusBlue Pill GNDReturn

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.

CheckExpected
Cover the sensorLow pitch
Uncover itHigher pitch
No movement during start-upDefault range is used
Hand moves slowlyPitch glides smoothly
Different room lightRecalibrates 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 →

Ask Lark AI

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.