Make Pin respond to rising edge of signal

Hi folks.
I have a simple pushbutton sketch. The issue is that the LED turns ON only after I let go the switch. In other words, the Arduino Pin reacts on the falling edge of the signal. How can I make the pin turn on the LED on the rising edge of the signal?

Thanks for the replies!!

const int BUTTON = 2;
const int LED = 3;
int BUTTONstate = 0;

void setup()
{
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
  BUTTONstate = digitalRead(BUTTON);
  if (BUTTONstate == HIGH)
  {
    digitalWrite(LED, HIGH);
  } 
  else{
    digitalWrite(LED, LOW);
  }
}

How is your button wired?

Have you tried replacing:

if (BUTTONstate == HIGH)

with

if (BUTTONstate == LOW)

And to be honest you are not checking the signal edge you are checking the state of the signal.

Ty. So how do i check the signal edge?

Something like this:

const int BUTTON = 2;
const int LED = 3;

void setup()
{
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
  int BUTTONstate = digitalRead(BUTTON);
  static int lastBUTTONstate = digitalRead(BUTTON);
  
  if (lastBUTTONstate != BUTTONstate)
  {
    delay(50); // debounce
    if (BUTTONstate == HIGH)
    {
      // button state went from LOW to HIGH
      digitalWrite(LED, HIGH);
    } 
    else
    {
      // button state went from HIGH to LOW
      digitalWrite(LED, LOW);
    }
   lastBUTTONstate  = BUTTONstate; 
  }
}

Like ToddL1962 already pointed out, you have to detect a change of the signal's state :slight_smile:

There is a good example on the Arduino website https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection in which both edges (rising and falling) are detected. With small changes, you can adjust that to only detect the rising edge.

Does Pin 2 have a pull-up or pull-down resistor? If not, you need to fix that.

In #5 @ToddL1962’s edge detector switch logic, there is no reason not to move the delay(50); to after the if/else statement.

50 milliseconds is getting into sluggish button territory, especially for youngsters. Even if the parameter was 10, arguably undetectable and perhaps sufficient denouncing, the delay should not impede the reaction to the switch on principle.

a7

Ty. So I want the LED to go ON the moment I push the switch ( rising edge ). What then would I use; a pull-up or a pull-down resistor?

Think about it :wink:
What kind of signal (HIGH or LOW) is needed at your inputpin if you want to get a rising edge when you push the button?

You still haven’t told us how your switch is wired.

The easiest way is to use the built-in pull-up resistor and wire the button between Pin 2 and Ground.

const int BUTTON = 2;
const int LED = 3;
int BUTTONstate = 0;

void setup()
{
  pinMode(BUTTON, INPUT_PULLUP);  // Keep pin HIGH when not grounded
  pinMode(LED, OUTPUT);
}

void loop()
{
  BUTTONstate = digitalRead(BUTTON);
  if (BUTTONstate == LOW)  // Pin goes LOW when connected to Ground
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
}

If you MUST have Pin 2 go from LOW to HIGH when pressed, there are two ways:

  1. Use a Normally Closed button with the above code.
  2. Use your original code, add an external pull-down resistor (10k Ohms) between Pin 2 and Ground, and connect the button between Pin 2 and +5V.

From sketch of Post-1, it appears that the switch-point (DPin-2) is externally pulled down.

Yes but only if the OP knows how to wire a switch, which we haven't established yet.

1. The DPin-2 of UNO Board can be made to respond to Rising Edge of an incoming signal with the following setup of Fig-1.

risingEdgeDpin2
Figure-1:

2. Test Sketch:

void setup() 
{
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
  bitSet(EICRA, ISC00); //set for rising edge
  bitSet(EICRA, ISC01);
  //-------------------
  while(bitRead(EIFR, INTF0) != HIGH)//sensing rising edge at DPin-2
  {
    ;
  }
  digitalWrite(13, HIGH);
}

void loop() 
{
  
}

3. Upload sketch of Step-2.
4. Gently press Button and check that L (Built-in LED of UNo) has turned on.

Note: If DPin-2 is to be reserved for INT0 interrupt and @Mars-Sojourner still wants a rising edge detection of an incoming signal, then he may use DPin-5 and configure TC1 to operate in rising edge external pule counting mode (Fig-2).


Figure-2:

Test Sketch:

void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(5, INPUT);   //no need DPin-5 is automatically INPUT in TC1 external count mode
  
  TCCR1A = 0x00;
  TCCR1B = 0x00;
  TCNT1 = 0xFFFF;  //preset value
  TCCR1B = 0x07;    //start TC1 to count external rising edge
  while(bitRead(TIFR1, TOV1) != HIGH)
  {
    ;
  }
  digitalWrite(13, HIGH); 
}

void loop() 
{
  
}

Thank you all for the replies! I will go through them and get back to you if I have more questions. Ty again.

This is the link I am following for wiring.....Working with an LED and a Push Button - Arduino Project Hub

OK so if the code is how you say and the wiring is how you say then it will work like you want.

So the problem is that you are doing something wrong and either the code or the wiring is not how you think it is.

How about posting a photograph where we can see all the wiring?

Wow! Ty for that post!