Controlling an LED (3/5): Turning on/off multiple LEDs and buzzer
Turning On/Off Multiple LEDs and Buzzer – CreateLabz

Turning On/Off Multiple LEDs and Buzzer

01 Overview

This is the 3rd tutorial in the Controlling the LEDs series. From the first tutorial, the potentiometer was used to control the brightness of a single LED. This was made possible by using the analog output of the Arduino and the Pulse Width Modulation (PWM) technique. However, this tutorial will switch on and off multiple LEDs instead of controlling their brightness, making use of the digital output of the Arduino. This concept will be discussed further in this tutorial.

Furthermore, a buzzer is used to generate sound that changes its behavior (pitch) when specific LEDs light up.

Project Use Case

This project uses five LEDs to indicate the value read from the potentiometer, with each LED representing a 20% increment. It is a simple way to turn a knob position into a visual bar made of LEDs, plus an audible alert through the buzzer when the value gets high.

02 Hardware and Software Components

Gather everything below before you start. This is your checklist — names, models, and versions only.

Hardware Components

Component Description
Arduino Uno × 1 The main controller board that reads the potentiometer and switches the LEDs and the buzzer.
LED (5 mm) × 5 The output display that indicates the potentiometer value in 20% increments. Color options: red, yellow, green, blue, white.
Potentiometer × 1 Rotary knob used as the analog input that controls which LED lights up.
220 ohm resistor × 5 Resistors used to limit the current for the LEDs.
330 ohm resistor × 1 Resistor available for the buzzer.
Passive Buzzer × 1 Generates a tone that changes pitch when the high LED cases are active.
Breadboard × 1 Holds the components in place for easy wiring and troubleshooting.

You can buy all this hardware at Createlabz.

Software Tools

Software Version / Details
Arduino IDE The official Arduino development environment used to write and upload the code. Download the latest version from arduino.cc.

Project Files

All required files are available in the project repository. Download the repository before proceeding to the Software Setup section.

Repository: github.com/createlabz/createlabz-public — arduino-upgradedStarterKit/PotentioControlledLEDs_withBuzzer

File Description
PotentioControlledLEDs_withBuzzer (folder) The complete Arduino code for switching multiple LEDs and the buzzer based on the potentiometer.

03 Application Discussion

Here is what each component does and why it is part of this project.

This project makes use of 5 LEDs to indicate the value of the potentiometer. The values are distributed by an increment of 20%. Its main difference from the first tutorial is that, instead of manipulating the LED brightness, the LEDs are switched on and off depending on the value the potentiometer gives out. To understand more about the potentiometer and the LED's functionalities, head on to the first blog post.

Buzzer

Passive buzzer photo

The buzzer can be found in the Upgraded Arduino Kit. These are commonly found in alarm devices, timers, and computers. In order to know the polarity of this component, it is similar to the LED, having legs of varying length. The polarity can also be seen marked on the side.

The necessary code needed to make the buzzer function are the following:

Function Purpose
tone(pin, frequency) Plays a tone on the buzzer.
noTone(pin) Stops the generation of the square wave created by tone().

LED Array × 5

Five LEDs are used as the output of the project. They are stored in an array so that each LED can be addressed by its index. As the potentiometer value climbs, a different LED lights up, creating a bar-like indication of the input.

04 Hardware Setup

Wire the components to the board using the tables below.

Wiring Diagram

Potentiometer, five LEDs, and buzzer wiring diagram

Potentiometer to Arduino

Pin / Signal Connects To
Analog Output (wiper) A0
Outer leg 5V
Outer leg GND

LED Array to Arduino

LED Board Pin Description
LED 1 13 Positive leg to pin; other leg to GND through a 220 ohm resistor
LED 2 12 Positive leg to pin; other leg to GND through a 220 ohm resistor
LED 3 11 Positive leg to pin; other leg to GND through a 220 ohm resistor
LED 4 10 Positive leg to pin; other leg to GND through a 220 ohm resistor
LED 5 9 Positive leg to pin; other leg to GND through a 220 ohm resistor
Note: Each LED is connected in series with a 220 ohm resistor between the LED and GND.

Buzzer to Arduino

Pin / Signal Connects To
Signal (longer leg / +) 8
GND (shorter leg / −) GND
Note: The buzzer has polarity, similar to an LED. The legs vary in length and the polarity is marked on the side. The 330 ohm resistor is available for the buzzer if needed.

Assembly Instructions

  1. Place all the components on the breadboard.
  2. Connect the potentiometer — the wiper to A0, and the outer legs to 5V and GND.
  3. Connect the positive leg of each LED to its assigned digital pin (13, 12, 11, 10, 9).
  4. Connect the other leg of each LED to GND through a 220 ohm resistor.
  5. Connect the buzzer — the signal leg to pin 8 and the other leg to GND.

05 Software Setup

Follow these steps in order. Do not skip any step.

Project Repository: github.com/createlabz/createlabz-public — arduino-upgradedStarterKit/PotentioControlledLEDs_withBuzzer
Download this first before proceeding.

Step 1 — Install the Arduino IDE

  1. Download and install the latest version of the Arduino IDE from arduino.cc.
  2. Accept the default settings during installation.

Step 2 — Board Settings

Use exactly these settings in your IDE. Wrong settings will cause upload failures.

Setting Value
Board Arduino Uno
Port Select the COM port shown under Tools > Port

Step 3 — Upload the Code

  1. Open the Arduino IDE and upload the code on the Arduino.
  2. You can download the code from the repository above, or copy the code below into a new sketch.
  3. Select the correct board and port.
  4. Click Upload.

06 Code

Copy the sketch below into the Arduino IDE, or download it from the repository. Read the Code Breakdown section to understand what each part does.

PotentioControlledLEDs_withBuzzer.ino

Arduino / C++
int ledArray[] = {13, 12, 11, 10, 9};

int buz = 8;

int potentio = A0;
int PotenInput = 0;


void setup() {
  // put your setup code here, to run once:

  //Use a for loop to set all LEDs as OUTPUT
  for(int i = 0; i < 6; i++)
{
  pinMode(ledArray[i], OUTPUT);
}

  pinMode(buz, OUTPUT);
  pinMode(potentio, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  PotenInput = analogRead(potentio);
  int num = map(PotenInput, 0, 1022, 0, 4);

  switch(num)
  {
    case 0: //very low (0%-20%)
    digitalWrite(ledArray[0], HIGH);
    digitalWrite(ledArray[1], LOW);
    digitalWrite(ledArray[2], LOW);
    digitalWrite(ledArray[3], LOW);
    digitalWrite(ledArray[4], LOW);
    noTone(buz);
    break;
    case 1: //low (20%-40%)
    digitalWrite(ledArray[0], LOW);
    digitalWrite(ledArray[1], HIGH);
    digitalWrite(ledArray[2], LOW);
    digitalWrite(ledArray[3], LOW);
    digitalWrite(ledArray[4], LOW);
    noTone(buz);
    break;
    case 2: //medium (40%-60%)
    digitalWrite(ledArray[0], LOW);
    digitalWrite(ledArray[1], LOW);
    digitalWrite(ledArray[2], HIGH);
    digitalWrite(ledArray[3], LOW);
    digitalWrite(ledArray[4], LOW);
    noTone(buz);
    break;
    case 3: //high (60%-80%)
    digitalWrite(ledArray[0], LOW);
    digitalWrite(ledArray[1], LOW);
    digitalWrite(ledArray[2], LOW);
    digitalWrite(ledArray[3], HIGH);
    digitalWrite(ledArray[4], LOW);
    tone(buz, 500);
    break;
    case 4: //very high (80%-100%)
    digitalWrite(ledArray[0], LOW);
    digitalWrite(ledArray[1], LOW);
    digitalWrite(ledArray[2], LOW);
    digitalWrite(ledArray[3], LOW);
    digitalWrite(ledArray[4], HIGH);
    tone(buz, 1000);
    break;
  }
  delay(300);
}

07 Code Breakdown

Here is what each part of the code does. Read this after uploading.

Arrays

An array is a variable that can hold more than one value, as long as the contents are of the same data type. The indexing of the array starts with zero.

Example
foodArray[] = {chicken, beef, pork};

foodArray[0] = chicken
foodArray[1] = beef
foodArray[2] = pork

In this project, a for loop was used to set all the LEDs in the array as outputs.

Key Functions

Function Purpose
int variable Creates a variable integer.
pinMode(pin, mode) Configures the identified pin to behave as an input or an output.
analogRead() Reads an analog input. This can range from 0 to 1023 depending on the input used.
digitalWrite() Sets the designated pin to a HIGH (5V) or LOW (0V) value.
map(fromLow, fromHigh, toLow, toHigh) Used to compress the potentiometer output into 5 values for the switch-case statement.
tone(pin, frequency) In this example, the buzzer is set to send a 500 Hz and a 1 kHz signal to pin "buz".
noTone(pin) Code put in place in order to stop any tone coming from the buzzer when not necessary.

General Program Workflow

  1. The program reads the potentiometer value using analogRead().
  2. The value is mapped from 0–1022 to 0–4 using map().
  3. A switch statement turns on only one LED depending on the mapped value.
  4. The buzzer plays a 500 Hz tone at the "high" (60–80%) case and a 1000 Hz tone at the "very high" (80–100%) case.
  5. noTone() silences the buzzer in the lower cases.
  6. The loop waits 300 ms before repeating.

08 Testing and Calibration

After uploading, verify each of the following to confirm the system is working correctly.

Potentiometer and LED Check

Turn the potentiometer slowly from minimum to maximum. Each 20% increment should light the next LED in the array (0–20% → LED 1, 20–40% → LED 2, 40–60% → LED 3, 60–80% → LED 4, 80–100% → LED 5). Only one LED should be on at a time.

Common Issue: If more than one LED lights up, or the wrong LED lights up, check the LED wiring and confirm each LED is connected to the correct digital pin with a 220 ohm resistor.

Buzzer Behavior

The buzzer stays silent while the value is in the lower cases. It plays a 500 Hz tone when the 60–80% case is active and a 1000 Hz tone when the 80–100% case is active. If you hear no sound, check the buzzer polarity and that it is connected to pin 8.

Common Issue: A reversed buzzer will not sound. The longer leg (marked +) must go toward the signal pin.

09 System Demonstration

The image and video below show the working system. Use these to verify your output matches what is expected.

System Setup

Complete system setup with the potentiometer, five LEDs, and buzzer

Video Demonstration

10 Conclusion

This tutorial tackled the digital output of the Arduino, making use of the HIGH and LOW states in switching the LEDs. This also discussed further how the PWM technique works. This setup can be used in various other projects, and the buzzer can be utilized to act as a warning indicator or alarm with different input components.

Another fun project to try is controlling the LED using your voice! This will be discussed as part 4 of the Controlling the LEDs series.

Other Tutorials in This Series

Possible Improvements and Future Enhancements

  • Replace the switch statement with a loop that checks each threshold, making the code shorter and easier to extend to more LEDs.
  • Use an LED strip or RGB LEDs to create smoother transitions between levels.
  • Connect the buzzer to different input components (for example, a sensor) so it acts as a warning indicator or alarm.
  • Add a display (LCD or Serial Monitor) to show the exact potentiometer value alongside the LEDs.

11 References

The following references were used while building this project.

  • evive Analog Output (PWM)
    https://thestempedia.com/tutorials/evive-analog-output-pwm/
  • Arduino PWM Tutorial
    https://create.arduino.cc/projecthub/muhammad-aqib/arduino-pwm-tutorial-ae9d71
  • Tutorial: Passive Buzzer
    https://bgsu.instructure.com/courses/1157282/pages/tutorial-passive-buzzer

12 Project Authors

  • Nolf Ivan Labios
  • Quality Checked by:
  • Elijah Joseph Orias
  • Lukie-Mar Lonzaga
  • Miles Darren Bagnol
Turning On/Off Multiple LEDs and Buzzer – CreateLabz
Arduino, Arduino starter kit, Arduino uno, Buzzer, Color led, Led, Multiple leds, Potentiometer

Leave a comment

All comments are moderated before being published