# Pin state fluctuation

**URL:** https://discourse.nodered.org/t/pin-state-fluctuation/79647
**Category:** General
**Created:** [8 July 2023 15:02 UTC](https://discourse.nodered.org/t/pin-state-fluctuation/79647 "2023-07-08T15:02:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)
#### Post date: [8 July 2023 15:02 UTC](https://discourse.nodered.org/t/pin-state-fluctuation/79647/1 "2023-07-08T15:02:33Z")

</div>

I use a wemos mini d1 for a project with my pi and Node-Red

When I provide 3V to a pin (D4/GPIO 2) it should give me "high" state. when not connected it should be "Low"

But when I switch the toggle Node-Red start to act strange...

This is _one_ toggle movement:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/099a8b8a01bfe10d7c786270c83c26ed7027b9f2.png)

cleared the debug and toggled the switch back and got this:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a7fe2ef571e50aebf14a46c388ad817155ade9cb.png)

could someone help me with how I can get 1 high when the pin state is high? and only 1 low when the pin state is low?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [8 July 2023 15:41 UTC](https://discourse.nodered.org/t/pin-state-fluctuation/79647/3 "2023-07-08T15:41:16Z")

</div>

If, each time you change the connection, you get a short sequence of high and low and after a few milliseconds the value stabilises on the correct signal, then that is caused by Switch bounce as it opens or closes. You need to implement debounce logic in the s/w on the D1.

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [8 July 2023 15:46 UTC](https://discourse.nodered.org/t/pin-state-fluctuation/79647/4 "2023-07-08T15:46:01Z")

</div>

Or if you determine it is contact bounce and s/w doesn't fix it you can put in a hardware debounce circuit

---

<div class="post-metadata">

### Author: ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)
#### Post date: [8 July 2023 16:10 UTC](https://discourse.nodered.org/t/pin-state-fluctuation/79647/5 "2023-07-08T16:10:39Z")

</div>

Brilliant! It works like a charm

```auto
const int triggerPin = D4; // Pin to monitor
bool isTriggered = false; // Flag to track trigger status
bool triggerState = LOW; // Current state of the trigger pin
bool lastTriggerState = LOW; // Previous state of the trigger pin
long debounceDelay = 50; // Debounce delay time in milliseconds
long lastDebounceTime = 0; // Time of the last pin state change

void setup() {
  Serial.begin(115200);
  pinMode(triggerPin, INPUT_PULLUP);

```

```auto
// Read the state of the trigger pin
  int reading = digitalRead(triggerPin);

  // Check if the current state of the trigger pin is different from the previous state
  if (reading != lastTriggerState) {
    // Reset the debounce timer
    lastDebounceTime = millis();
  }

  // Check if the debounce delay has passed
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // Check if the current state of the trigger pin is different from the stored state
    if (reading != triggerState) {
      triggerState = reading;

      // Check for trigger transition from low to high
      if (triggerState == HIGH) {
        Serial.println(20);
        isTriggered = true;

        // Add your message or data to send here
      } else {
        // Check for trigger transition from high to low
        Serial.println(50);
        isTriggered = false;

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [22 July 2023 16:10 UTC](https://discourse.nodered.org/t/pin-state-fluctuation/79647/6 "2023-07-22T16:10:41Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
