Controlling an LED (3/5): Turning on/off multiple LEDs and buzzer

Overview:

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 technique. However, this tutorial will switch on and off multiple LEDs instead of controlling its brightness. Thus, 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.

 

Hardware:


  • LED x 5
  • 
    
  • Potentiometer x 1
  • 
    
  • 220 ohm resistor x 5
  • 330 ohm resistor x 1
  • 
    
  • Buzzer
  • 
    
  • Breadboard x 1
  • 
    

     You can buy all this Hardware at Createlabz.

     

    Software:

     

    Application Discussion

    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 LED is 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

    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 the legs in varying length. The polarity can also be seen marked on the side.

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

    tone(pin, frequency) – to play a tone on the buzzer

    noTone(pin) – stops the generation of square wave created by tone()

    Set-up the hardware

    Set-up the software

    Open the Arduino IDE and upload the code on the Arduino.

    The Arduino code can be found in Github here or you can copy the code below

    
    
    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);

    Code Breakdown

    • Int myArray[]
      – an array that holds integers, arrays can hold anything as long as the contents are of the same data type
      • The indexing of the array starts with zero, thus;
        • foodArray[]={chicken, beef, pork};
        • foodArray[0] = chicken
        • foodArray[1] = beef
        • foodArray[2] = pork
      • The for loop was used to indicate all leds in the array as output

    • int var
      – Creates a variable integer. 

    • pinMode(pin,mode) 
      – Configures the identified pin to behave as an input or an output.

    • analogRead
      – This can range from 0-1023 depending on the input used.

    • digitalWrite
      – This 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.

    • noTone()
      – Code put in place in order to stop any tone coming from the buzzer when not necessary.

    • tone()
      – In this example, the buzzer is set to send 500Hz and 1kHz signal to pin ‘buz’.

     

    Output

     

    Conclusion

    This tutorial tackled the digital output of the Arduino, making use of the HIGH and LOW states in switching the LED. This also discussed further how the PWM technique works. This setup can be used in various other projects. 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 LED series.

     

    Checkout other tutorials in the Controlling an LED series:

    Adjusting LED Brightness using a Potentiometer

    Adjusting LED brightness using a Photoresistor

    Music reactive LEDs using Sound Module

     

    Reference

    https://thestempedia.com/tutorials/evive-analog-output-pwm/

    https://create.arduino.cc/projecthub/muhammad-aqib/arduino-pwm-tutorial-ae9d71

    https://bgsu.instructure.com/courses/1157282/pages/tutorial-passive-buzzer

    
    
    ArduinoArduino starter kitArduino unoBuzzerColor ledLedMultiple ledsPotentiometer

    Leave a comment

    All comments are moderated before being published