Skip to content

LED and Buzzer

Level Beginner
Board V1 & V2
Parts micro:bit · LED + 220 Ω resistor or active buzzer · Jumper wires / crocodile clips

What you'll build

Hold button A to switch on an LED (or sound a buzzer) connected to pin 0.

Wiring

Part Part pin micro:bit pin
LED or active buzzer + (long leg) P0 (through a 220 Ω resistor for an LED)
LED or active buzzer − (short leg) GND
LED or active buzzer+ (long leg)− (short leg)micro:bitP0GND

Code

led-buzzer-example.py
'''
Example: Control LED or Buzzer (On/Off), connected with Pin0 in micro:bit
By: Meqdad Darwish
'''
from microbit import *

while True:
    if button_a.is_pressed():
        pin0.write_digital(1)
        sleep(2000)
    else:
        pin0.write_digital(0)

How it works

pin0.write_digital(1) sets pin 0 to 3 V (on); write_digital(0) sets it to 0 V (off). The same code drives an LED or an active buzzer, since both just need power to work.

Try this next

  • Blink the LED with sleep() instead of holding the button.
  • Use pin0.write_analog(0–1023) to dim the LED.
  • Combine with the PIR sensor to make a motion alarm.