Switch light and sound simultaneously

FunduinoPublished on

LED und Piezo gleichzeitig ansteuern – und Variablen kennenlernen

Eine LED blinkt, ein Piezo piept – beides gleichzeitig und im Sekundentakt. Technisch ist das der Blink-Sketch mit einem zweiten Ausgang.

Der eigentliche Lerninhalt steckt aber im ersten Programmabschnitt: Hier lernst du Variablen kennen. Die Pins bekommen Namen, und im restlichen Programm steht dann „LED“ statt „5“. Das macht den Code lesbar – und wenn du später umsteckst, änderst du nur eine Zeile.

Aufbau und Programm gibt es für vier Boards: Arduino UNO, Wemos D1 Mini, ESP32-Wroom und NodeMCU.

Breadboard setup with LED and piezo buzzer on the Arduino UNO
Setup on the Arduino UNO, pin 5 and pin 7

You need this

5 V buzzer - electromagnetic sounder, 2400 Hz 5 V buzzer - electromagnetic sounder, 2400 Hz 0,90 € *Item no. F23108736About the product — 5 V buzzer - electromagnetic sounder, 2400 Hz
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 6 parts together 20,35 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

The task

An LED and a piezo speaker should blink and beep at the same time. Technically, this is the Blink sketch with a second output - but the learning objective is something else: Variables.

Instead of using the numbers 5 and 7 throughout the program, you give the two pins names before setup. The program then uses “LED” and “Beep” instead of the pin numbers. If you want to switch pins later, you only need to change a single line.

Active or passive speaker?

For this project, you need an active speaker, also called a piezo speaker. It produces a tone by itself as soon as voltage is applied - which is exactly why a digitalWrite is enough in the sketch.

Both types look almost identical. You can recognize the active one by its closed, black underside; the passive one has an open underside.

Passive speaker with an open bottom and active speaker with a closed bottom side by side
Left passive (open), right active (closed)

Setup and program for the Arduino UNO

The LED is connected to Pin 5, the piezo buzzer to Pin 7. The LED needs a 220-ohm series resistor - LEDs operate at 2 to 3 volts, while the microcontroller supplies 5 volts.

Breadboard setup with LED and piezo buzzer on the Arduino UNO
Setup on the Arduino UNO, pin 5 and pin 7
Arduino UNO
// Dieses Mal nutzen wir auch den ersten Programmabschnitt.
// Hier werden Variablen eingetragen. Das bedeutet, dass sich hinter einem Buchstaben oder einem Wort eine Zahl verbirgt.
// Bei uns ist die LED an Pin 5 angeschlossen und der Piezo-Speaker an Pin 7.
// Damit man die beiden Pins später nicht verwechselt, benennen wir Pin 5 und Pin 7 einfach um.
int LED = 5;  // Das Wort „LED“ steht jetzt für die Zahl „5“.
int Pieps = 7;  // Das Wort „Pieps“ steht jetzt für die Zahl „7“.
void setup()  // wir starten mit dem Setup.
{
  pinMode(LED, OUTPUT);  // Pin 5 (Pin „LED“) ist ein Ausgang.
  pinMode(Pieps, OUTPUT);  // Pin 7 (Pin „Pieps“) ist ein Ausgang.
}
void loop()  // Das Hauptprogramm beginnt.
{
  digitalWrite(LED, HIGH);  // Schalte die LED an.
  digitalWrite(Pieps, HIGH);  // Schalte den Piezo-Lautsprecher an.
  delay(1000);  // Warte 1000 Millisekunden. (Es piepst und leuchtet.)
  digitalWrite(LED, LOW);  // Schalte die LED aus.
  digitalWrite(Pieps, LOW);  // Schalte den Piezo aus.
  delay(1000);  // Warte 1000 Millisekunden. (kein Lärm, kein Licht)
}
// Hier am Ende springt das Programm an den Start des Loop-Teils.
// also wird es gleich wieder piepsen und leuchten. Wenn man die Pause (delay) verkleinert oder vergrößert,
// piepst und leuchtet es schneller oder langsamer.

Assembly and program for the Wemos D1 Mini

On the Wemos D1 Mini, the LED and piezo are connected to D5 and D6. A “D” must be placed before the pin numbers in the program.

Breadboard setup with LED and piezo buzzer on the Wemos D1 Mini
Setup on the Wemos D1 Mini, D5 and D6
Wemos D1 Mini
int LED = D5;  // The word “LED” now stands for D5.
int Pieps = D6;  // The word “Pieps” now stands for D6.
void setup()  // We start with the setup.
{
  pinMode(LED, OUTPUT);  // Pin D5 (pin “LED”) is an output.
  pinMode(Pieps, OUTPUT);  // Pin D6 (pin “Pieps”) is an output.
}
void loop()  // The main program begins.
{
  digitalWrite(LED, HIGH);  // Turn on the LED.
  digitalWrite(Pieps, HIGH);  // Turn on the piezo speaker.
  delay(1000);  // Wait 1000 milliseconds. (It beeps and lights up.)
  digitalWrite(LED, LOW);  // Turn off the LED.
  digitalWrite(Pieps, LOW);  // Turn off the piezo.
  delay(1000);  // Wait 1000 milliseconds. (no noise, no light)
}

Setup and program for the ESP32-Wroom

On the ESP32-Wroom, the LED and piezo are connected to pin 26 and pin 27.

Breadboard setup with LED and piezo buzzer on the ESP32-Wroom
Setup on the ESP32-Wroom, pins 26 and 27
ESP32-Wroom
int LED = 26;  // The word “LED” now stands for the number “26”.
int Pieps = 27;  // The word “Pieps” now stands for the number “27”.
void setup()  // We start with the setup.
{
  pinMode(LED, OUTPUT);  // Pin 26 (pin “LED”) is an output.
  pinMode(Pieps, OUTPUT);  // Pin 27 (pin “Pieps”) is an output.
}
void loop()  // The main program begins.
{
  digitalWrite(LED, HIGH);  // Turn the LED on.
  digitalWrite(Pieps, HIGH);  // Turn the piezo speaker on.
  delay(1000);  // Wait 1000 milliseconds. (It beeps and lights up.)
  digitalWrite(LED, LOW);  // Turn the LED off.
  digitalWrite(Pieps, LOW);  // Turn the piezo off.
  delay(1000);  // Wait 1000 milliseconds. (no noise, no light)
}

Assembly and program for the NodeMCU

The LED and piezo buzzer on the NodeMCU are connected to D1 and D2.

Breadboard setup with LED and piezo buzzer on the NodeMCU
Setup on the NodeMCU, D1 and D2
NodeMCU
int LED = D1;  // The word “LED” now stands for D1.
int Pieps = D2;  // The word “Pieps” now stands for D2.
void setup()  // We start with setup.
{
  pinMode(LED, OUTPUT);  // Pin D1 (pin “LED”) is an output.
  pinMode(Pieps, OUTPUT);  // Pin D2 (pin “Pieps”) is an output.
}
void loop()  // The main program begins.
{
  digitalWrite(LED, HIGH);  // Turn on the LED.
  digitalWrite(Pieps, HIGH);  // Turn on the piezo speaker.
  delay(1000);  // Wait 1000 milliseconds. (It beeps and lights up.)
  digitalWrite(LED, LOW);  // Turn off the LED.
  digitalWrite(Pieps, LOW);  // Turn off the piezo.
  delay(1000);  // Wait 1000 milliseconds. (no noise, no light)
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.