powered by TinkerSpace.in
Join free
TinkerPy Tutorial ๐Ÿ“˜ Part 2 ยท Coder ยท Chapter 17 of 24

Buzzer & RGB LED

Give your project a voice and a rainbow! One line plays a door bell, a police siren or Jana Gana Mana on a buzzer, and one line paints an RGB LED in any of millions of colours โ€” blinking, breathing, chasing and twinkling all by itself.

๐Ÿ“–Key words

buzzer_play()

Plays a ready-made tone or song: buzzer_play(speaker, "door_bell"). The next line runs when it ends.

Passive buzzer

A buzzer that can play different notes, so tunes sound like music. An active buzzer can only beep the rhythm.

RGB LED

An LED with a red, green and blue light inside. TinkerPy uses WS2812B RGB LEDs: a board with 1, 2 or 3 LEDs, or a strip of up to 60.

rgb_color()

Lights one LED or all in a colour: rgb_color(lamp, all, "cyan", 60) โ€” the brightness 0โ€“100 is optional.

rgb_hex_color()

Mixes any colour from red, green and blue, 0โ€“255 each: rgb_hex_color(lamp, 1, 255, 120, 0) is orange.

๐ŸŽฌSee it work

Mix any colour

Move the red, green and blue sliders to mix a colour โ€” those three numbers are what rgb_hex_color() needs.

Which kind of sound?

Sort each buzzer_play() tone name into its group.

๐Ÿง How it works

buzzer_play() โ€” tones and songs

  • Pick Buzzer on an output in Circuit Connections: it writes import buzzer and speaker = set_output(buzzer) โ€” choose passive for songs: set_output(buzzer, passive).
  • Simple: beep, double_beep, triple_beep, long_beep, chirp_up, chirp_down, success, error, notify, start_up.
  • Alerts: warning, alarm, ambulance, police, fire_truck, school_bell, school_long_bell, door_bell, countdown, quiz_wrong, phone_ring, marimba_ring, message_beep, retro_phone.
  • Tunes: happy_birthday, jingle_bells, jana_gana_mana, twinkle_twinkle, ode_to_joy, mary_lamb, old_macdonald, merry_christmas. Listen to them all on the board: Menu โ†’ Buzzer Tones.
  • The program waits while a tone plays, so keep long songs for when nothing else must happen.

Setting up RGB LEDs and colours

  • Pick RGB LED on an output and give the number of LEDs: lamp = set_output(rgb_led, 3) (or rgb_begin(lamp, 3) โ€” the same thing). Up to 60 LEDs per strip.
  • Which LED: 1, 2, 3 โ€ฆ or all. Colours: red, green, blue, yellow, orange, purple, pink, cyan, white, warm_white, off, random, rainbow โ€” or a code like "#FF8800".
  • rgb_color() sets a colour; rgb_hex_color() mixes red, green and blue numbers (great with readings and variables).
  • rgb_flip(lamp, all, "warm_white") under a button: press for ON, press again for OFF.

Effects that run by themselves

  • rgb_blink(lamp, 1, "red", 500, 500) blinks ยท rgb_breathe(lamp, all, "purple", 3000) fades in and out.
  • rgb_swap(strip, all, "red", "blue", 300) police lights ยท rgb_chase(strip, all, "rainbow", 80) a running light.
  • rgb_wipe(strip, all, "orange", 60) fills LED by LED and stays on ยท rgb_twinkle(strip, all, "white") sparkles like stars.
  • They keep going while your code carries on โ€” even during a wait().

Meters: rgb_bar() and rgb_heat()

  • rgb_bar(meter, all, "rainbow", value, 100) lights more LEDs as the value goes up โ€” "rainbow" goes green โ†’ yellow โ†’ red.
  • rgb_heat(lamp, 1, "red", t, 20, 40): blue at 20, red at 40 โ€” a thermometer you can read from across the room.
  • Feed them any reading: analog_read(), distance(), dht_temperature() or a counter.

๐Ÿ’ปCode

# Add the libraries you need
import buzzer

# Give your pins easy-to-remember names
i1 = set_name("bell_button")
i2 = set_name("party_button")
o1 = set_name("speaker")

# ===== Musical doorbell =====

# This code block executes only once
setup:
    bell_button = set_input(push_button, active_low)
    party_button = set_input(push_button, active_low)
    speaker = set_output(buzzer, passive)

# This code block executes forever
loop:
    if bell_button == ON:  # when bell_button is ON
        buzzer_play(speaker, "door_bell")  # play the "door_bell" tone on the buzzer (speaker) โ€” the next line waits until it ends
    elif party_button == ON:  # otherwise, when party_button is ON
        buzzer_play(speaker, "happy_birthday")  # play the "happy_birthday" tone on the buzzer (speaker) โ€” the next line waits until it ends

A musical doorbell on a passive buzzer named speaker: bell_button plays the ding-dong door_bell, party_button plays Happy Birthday. Because buzzer_play waits until the sound ends, one press plays the whole tone.

# Add the libraries you need
import rgb_three

# Give your pins easy-to-remember names
i1 = set_name("mode_button")
i2 = set_name("knob")
o1 = set_name("lamp")

# ===== Mood lamp =====

# This code block executes only once
setup:
    mode_button = set_input(push_button, active_low)
    knob = set_input(potentiometer, adc)
    lamp = set_output(rgb_led, 3)

# This code block executes forever
loop:
    if mode_button == ON:  # when mode_button is ON
        rgb_flip(lamp, 3, "warm_white")  # press: LED 3 reading light on / off
        wait(300)  # pause here for 300 milliseconds before continuing
    k = analog_read(knob)  # 0 to 100
    if k < 10:  # when k < 10
        rgb_breathe(lamp, 1, "purple", 3000)  # calm breathing
    elif k < 50:  # otherwise, when k < 50
        rgb_color(lamp, 1, "cyan", k * 2)  # brighter as you turn
    else:  # otherwise
        rgb_hex_color(lamp, 1, 255, 255 - k * 2, 0)  # orange to red
    wait(100)  # pause here for 100 milliseconds before continuing

A mood lamp on a 3-LED board. The knob chooses the mood for LED 1: breathing purple at the bottom, cyan that gets brighter in the middle, then orange to red mixed with rgb_hex_color. Each press of mode_button flips LED 3 on as a warm white reading light, then off again.

# Add the libraries you need
import rgb_strip

# Give your pins easy-to-remember names
o1 = set_name("strip")

# ===== Festival light show =====

# This code block executes only once
setup:
    strip = set_output(rgb_led, 8)

# This code block executes forever
loop:
    rgb_wipe(strip, all, "orange", 60)  # fill up, one LED at a time
    wait(2000)  # pause here for 2000 milliseconds before continuing
    rgb_chase(strip, all, "rainbow", 80)  # a moving rainbow
    wait(4000)  # pause here for 4000 milliseconds before continuing
    rgb_twinkle(strip, all, "warm_white", 120)  # fairy lights
    wait(4000)  # pause here for 4000 milliseconds before continuing
    rgb_swap(strip, all, "red", "blue", 300)  # police lights
    wait(4000)  # pause here for 4000 milliseconds before continuing
    rgb_blink(strip, all, "random", 300)  # a new colour every blink
    wait(4000)  # pause here for 4000 milliseconds before continuing

A festival light show on an 8-LED strip: wipe, chase, twinkle, swap and blink. Each effect runs by itself, so the program only waits a few seconds before starting the next one.

# Add the libraries you need
import dht11
import ultrasonic
import rgb_strip
import rgb_single

# Give your pins easy-to-remember names
i1 = set_name("weather")
o1 = set_name("meter")
o2 = set_name("heat_lamp")

# ===== Heat and level meter =====

# This code block executes only once
setup:
    weather = set_input(dht11)
    i3 = set_input(ultrasonic_trig)  # cannot be changed
    i4 = set_input(ultrasonic_echo)  # cannot be changed
    meter = set_output(rgb_led, 8)
    heat_lamp = set_output(rgb_led, 1)

# This code block executes forever
loop:
    d = distance()  # set d to distance()
    if d < 0:  # when d < 0
        d = 100  # nothing in range: treat it as far away
    rgb_bar(meter, all, "rainbow", 100 - d, 100)  # more LEDs as you come closer
    t = dht_temperature(weather)  # set t to dht_temperature(weather)
    rgb_heat(heat_lamp, 1, "red", t, 20, 40)  # blue when cool, red when hot
    wait(500)  # pause here for 500 milliseconds before continuing

Two meters (TinkerPy Mega): the 8-LED meter strip fills up as something comes closer to the ultrasonic sensor, and the 1-LED heat_lamp turns from blue (20 ยฐC) to red (40 ยฐC) with the DHT11 on i1.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐ŸŽ‚Birthday surprise

A passive buzzer can play happy_birthday โ€” hide one inside a gift box with a button and surprise a friend.

๐Ÿช”Diwali lights

Festival light strings are often the same kind of RGB LEDs โ€” rgb_chase and rgb_twinkle make them dance.

๐ŸŒˆMillions of colours

Red, green and blue each have 256 levels, so rgb_hex_color() can mix over 16 million different colours.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Musical doorbell

Two buttons, one passive buzzer: the front door plays door_bell and the back door plays marimba_ring. Add an RGB LED that flashes with rgb_blink while the bell rings.

๐Ÿงฐ You need: TinkerPy board, passive buzzer, 2 push buttons, RGB LED board
Mood lamp

Put a 3-LED RGB board inside a paper lantern. A knob mixes the colour with rgb_hex_color and a button switches a calm rgb_breathe mode on and off.

๐Ÿงฐ You need: TinkerPy board, RGB LED board, potentiometer, push button, paper lantern

โญPoints to remember

  1. buzzer_play(name, "tone") plays a ready-made tone or song.
  2. The next line runs only after the tone ends.
  3. Tunes need a passive buzzer: set_output(buzzer, passive).
  4. Hear every tone on the board: Menu โ†’ Buzzer Tones.
  5. set_output(rgb_led, n) sets up n RGB LEDs (up to 60).
  6. Choose LED 1, 2, 3 โ€ฆ or all.
  7. rgb_color sets a named colour; brightness is optional.
  8. rgb_hex_color mixes red, green and blue, 0โ€“255 each.
  9. rgb_flip turns the LEDs on and off with each press.
  10. blink, breathe, swap, chase, wipe and twinkle run by themselves.
  11. rgb_wipe fills up and stays on.
  12. rgb_bar is a level meter; rgb_heat goes blue โ†’ colour.
๐Ÿคฏ
Did you know?

Each WS2812B RGB LED has its own tiny chip inside, so a whole strip of 60 LEDs needs only one data wire from your board.

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

5 quick questions to check yourself โ€” free with your TinkerPy account.

Log in