How to Change Frequency of Blinking LED with Arduino

Introduction

In this tutorial, we will learn how to Change the Frequency of a Blinking LED with Arduino using two buttons. With the help of one button, we will increase the blinking frequency, while the other button will decrease the frequency of the LED blinking.

This project is perfect for beginners who are looking to learn more about basic electronics and programming. It is also a great way to experiment with LEDs and create unique visual effects for your projects. So let’s get started and learn how to change the frequency of a blinking LED with two buttons!

Hardware Required

You will need the following components for the How to Change Frequency of Blinking LED with Arduino

Components#Buy From Amazon
Arduino UNO1Buy Now
LED 5mm1Buy Now
Buttons2Buy Now
Resistors 220Ω1Buy Now
Resistors 10KΩ2Buy Now
Jumper WiresFewBuy Now
Breadboard1Buy Now

Circuit Connections

For this circuit, we only need an LED, two buttons, and a few resistors. Connect the positive terminal of the LED to digital pin 9 of the Arduino, and the negative terminal of the LED to a 220-ohm resistor. The other end of the resistor should be connected to the ground. Connect one terminal of each button to digital pins 2 and 3 of the Arduino. The other terminal of the buttons should be connected to the 5V supply of the Arduino. Place a 10k ohm pull-down resistor between each button and the ground.

Circuit Diagram

Change-Frequency-of-Blinking-LED-with-Arduino-Circuit

Working Explanations

The code for this circuit is very simple. When the first button is pressed, the frequency of the LED blinking will increase. Similarly, when the second button is pressed, the frequency of the LED blinking will decrease. We will use the millis() function to create a delay between each blink of the LED.

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Code

//For more Projects: www.arduinocircuit.com

const int ledPin = 9;
const int buttonPin1 = 12; // increase the counter
const int buttonPin2 = 13; // Decrease the counter

int lastState1 = HIGH; // the previous state from the input pin
int lastState2 = HIGH;
int currentState1, currentState2;     // the current reading from the input pin
int pushCounter = 0;  // counter for the number of button presses
int delay_t = 100;
int ledState = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop() {
  currentState1 = digitalRead(buttonPin1);
  currentState2 = digitalRead(buttonPin2);
  if (currentState1 != lastState1) {
    if (currentState1 == LOW) {
      pushCounter++;
    }
  }
  lastState1 = currentState1;

  if (currentState2 != lastState2) {
    if (currentState2 == LOW) {
      pushCounter--;
    }
  }

  delay_t = 100 + pushCounter * 100;
  blink_without_delay(delay_t);

  lastState2 = currentState2;
  if (pushCounter >= 10) {
    pushCounter = 0;
  }
  else if (pushCounter < 0) {
    pushCounter = 0;
  }
  delay(50);
}

unsigned long previousMillis = 0; // will store last time LED was updated
void blink_without_delay(long interval) {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
    previousMillis = currentMillis;
  }
}

Applications

This circuit uses in many applications where the frequency of an LED needs to be adjusted, such as

  1. This Project uses Decorative lighting.
  2. This Project uses Christmas lights.
  3. This Project also uses Party lights.

Leave a Comment


error: