Arduino Push Button in proteus Simulation

 Arduino Push Button in proteus Simulation

Here’s the Arduino code for lighting up an LED when a button is pressed twice. It uses a button press counter to detect two consecutive presses, with a debounce mechanism to ensure reliability.

Components needed:

  1. An Arduino board, like the Uno or Nano.

  2. A pushbutton.

  3. An LED of any color.

  4. A 220-ohm resistor for the LED. (if you are doing the hardware version of the project)

  5. A breadboard and some jumper wires. (if you are doing the hardware version of the project)

  6. And, of course, a USB cable to connect your Arduino to your computer." (if you are doing the hardware version of the project)

                                                 Circuit Diagram in Proteus



Below is the Arduino Code:

const int buttonPin = 2;  // Button connected to pin 2

const int ledPin = 13;    // LED connected to pin 13


int buttonState = 0;      // Current state of the button

int lastButtonState = 0;  // Previous state of the button

int pressCount = 0;       // Counter for button presses


unsigned long lastDebounceTime = 0; // Last time the button state changed

const unsigned long debounceDelay = 50; // Debounce delay in milliseconds


void setup() {

  pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor

  pinMode(ledPin, OUTPUT);          // Set LED pin as output

  digitalWrite(ledPin, LOW);        // Ensure LED starts off

  Serial.begin(9600);               // Initialize serial communication

}


void loop() {

  int reading = digitalRead(buttonPin); // Read the current button state


  // Check if the button state has changed (debouncing)

  if (reading != lastButtonState) {

    lastDebounceTime = millis(); // Reset the debounce timer

  }


  // If the state is stable beyond the debounce delay

  if ((millis() - lastDebounceTime) > debounceDelay) {

    // If the button state has changed

    if (reading != buttonState) {

      buttonState = reading;


      // Only act on a button release

      if (buttonState == HIGH) {

        pressCount++; // Increment the button press count

        Serial.print("Button pressed count: ");

        Serial.println(pressCount);


        // If pressed twice, toggle the LED

        if (pressCount == 2) {

          digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle LED state

          pressCount = 0; // Reset the press count

        }

      }

    }

  }


  lastButtonState = reading; // Update the last button state

}

How It Works:

  1. Button Press Detection:

    • The button state is read using digitalRead.
    • Debounce logic prevents multiple counts from a single press due to mechanical noise.
  2. Press Counter:

    • Each time the button is pressed (and released), pressCount is incremented.
    • When pressCount reaches 2, the LED toggles on/off, and pressCount is reset.
  3. Debounce:

    • Ensures only valid presses are counted by checking the elapsed time since the last state change.

Wiring:

  • Button: One terminal to pin 2, the other terminal to GND. Use a pull-up resistor or enable the internal pull-up resistor by using INPUT_PULLUP.
  • LED: Positive leg to pin 13, negative leg to GND via a resistor (220Ω recommended).
 https://youtu.be/haLukcfWrv8

Comments

Popular posts from this blog

School Project: Temperature Controlled Fan using Arduino in Proteus

Arduino code program to display text on the LCD