innovatorandthinker
Published © CC BY

LED Combination Project with Satisfying Sounds of Buzzer

In this lesson, let's study how to control LED on/off and buzzer to beep in different frequencies.

BeginnerFull instructions provided387
LED Combination Project with Satisfying Sounds of Buzzer

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
USB Cable for Arduino UNO
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Micro - Push Button
Any size/ color micro - push button but it should have 4 - pins.
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
NPN Transistor (S8050)
×1
Passive Buzzer
×1
Male/Male Jumper Wires
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Mastech MS8217 Autorange Digital Multimeter
Digilent Mastech MS8217 Autorange Digital Multimeter
* IT is optional, it's only for measurements of some eletrical components... That's all.

Story

Read more

Schematics

Circuit diagram :-

Code

LED_combination_experiment.ino

Arduino
/***********************************************************
File name:LED_combination_experiment.ino
Description:   With three buttons, three lamps and one buzzer, 
press the different buttons to make the corresponding lamp light 
up, along with the buzzer sounds at different frequencies. 
Made by: ARSH RAJ
Date: 4/08/2022 
***********************************************************/
#define LED1 3       //definition led_1 I/O number is 3     
#define KEY1 2       //definition key_1 I/O number is 2

#define LED2 5      //definition led_2 I/O number is 5
#define KEY2 4      //definition key_2 I/O number is 4

#define LED3 10
#define KEY3 9

#define beepPin 8

int KEY_NUM1 = 0;    //key_1 value (The key value stores the variable, not equal to 1 indicates that the key is pressed)
int KEY_NUM2 = 0;    //key_2 value
int KEY_NUM3 = 0;

void setup()
{
  pinMode(LED1,OUTPUT);              //definition led_1 I/O is OUTPUT
  pinMode(KEY1,INPUT_PULLUP);        //definition led_1 I/O is INPUT_PULLUP
  pinMode(LED2,OUTPUT);
  pinMode(KEY2,INPUT_PULLUP);  
  pinMode(LED3,OUTPUT);
  pinMode(KEY3,INPUT_PULLUP);  
  
  Serial.begin(9600);
}

void loop()
{
  ScanKey1();//Key scan program, when the button is pressed, the subroutine will modify the value of KEY_NUM
  ScanKey2();
  ScanKey3();
}

//Button 1 Scanner
void ScanKey1()            
{
  KEY_NUM1 = 0;                       //Clear variable
  if(digitalRead(KEY1) == LOW)         //There is a key press
  {
    beep1();
    delay(20);                        //Delayed debounce
    if(digitalRead(KEY1) == LOW)       //There is a key press
    {
      KEY_NUM1 = 1;                   //Variable set to 1
      while(digitalRead(KEY1) == LOW); //Wait for the keys to release hands
    }
    Serial.println(digitalRead(LED1));//Serial output current LED status
  }

  if(KEY_NUM1 == 1)              //Is the button pressed
  {   
    digitalWrite(LED1,!digitalRead(LED1));    //LED status flip
  }
  
}

//Button 2 scanner
void ScanKey2()         
{
  KEY_NUM2 = 0;           
  if(digitalRead(KEY2) == LOW)      
  {
    beep2();
    delay(20);          
    if(digitalRead(KEY2) == LOW)    
    {
      KEY_NUM2 = 1;       
      while(digitalRead(KEY2) == LOW);  
    }
      Serial.println(digitalRead(LED2));
  }

  if(KEY_NUM2 == 1)
  {
    digitalWrite(LED2, !digitalRead(LED2)); 
  }
}

//Button 3 scanner
void ScanKey3()         
{
  KEY_NUM3 = 0;           
  if(digitalRead(KEY3) == LOW)      
  {
    beep3();
    delay(20);          
    if(digitalRead(KEY3) == LOW)    
    {
      KEY_NUM3 = 1;       
      while(digitalRead(KEY3) == LOW);  
    }
      Serial.println(digitalRead(LED3));
  }

  if(KEY_NUM3 == 1)
  {
    digitalWrite(LED3, !digitalRead(LED3)); 
  }
}

void beep1()//Set buzzer frequency
{
  tone (beepPin,300);
  delay(200);
  noTone(beepPin);
  }
  void beep2()
{
  tone (beepPin,900);
  delay(200);
  noTone(beepPin);
  }
  void beep3()
{
  tone (beepPin,1400);
  delay(200);
  noTone(beepPin);
  }

Credits

innovatorandthinker

innovatorandthinker

1 project • 0 followers

Comments