# Toggle input to pulse output through nano?

**URL:** <https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583>\
**Category:** General Guidance\
**Created:** [May 30, 2022, 6:41pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583 "2022-05-30T18:41:00Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![drknss](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@drknss](https://forum.arduino.cc/u/drknss)\
**Post date:** [May 30, 2022, 6:41pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/1 "2022-05-30T18:41:00Z")

</div>

I was wandering if its possible to connect a simple on/off switch to an arduino nano, so the arduino would process it in a way to convert an input toggle to a output pulse each time the switch is switched either on or off.

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [May 30, 2022, 7:17pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/2 "2022-05-30T19:17:39Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [May 30, 2022, 8:06pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/3 "2022-05-30T20:06:51Z")

</div>

Have a look at the 'StateChangeDetection' example in the Arduino IDE.

![StateChangeDetection](https://europe1.discourse-cdn.com/arduino/original/4X/7/b/3/7b3c1c186c0198d059f0c5502bd297f5961fa39e.png)

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [May 30, 2022, 8:20pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/4 "2022-05-30T20:20:15Z")

</div>

Here is example code that will flash the built-in LED on pin 13 for 1/5 second on each button press. The button is wired to ground and pin 2 with the pin's internal pullup enabled. The code is modified from [the state change for active low inputs tutorial.](https://forum.arduino.cc/t/state-change-detection-for-active-low-inputs/680899)

```cpp
/*
   The circuit:
   - pushbutton attached to pin 2 from ground
   - the internal pullup on pin 2 is enabled
   - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)
*/

// this constant won't change:
const byte buttonPin = 2; // the pin that the pushbutton is attached to
const byte ledPin = 13; // the pin that the LED is attached to

// Variables will change:
boolean buttonState = 0; // current state of the button
boolean lastButtonState = 0; // previous state of the button

void setup()
{
   // initialize the button pin as a input:
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 20;
   if (millis() - timer >= interval)
   {
      timer = millis();

      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);

      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         // if the state has changed, increment the counter
         if (buttonState == LOW)
         {
            digitalWrite(ledPin, HIGH);
            delay(200);
            digitalWrite(ledPin, LOW);
         }
         // save the current state as the last state, for next time through the loop
         lastButtonState = buttonState;
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [May 30, 2022, 10:40pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/5 "2022-05-30T22:40:17Z")

</div>

Crude debounce there!  
😁

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [May 30, 2022, 11:23pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/6 "2022-05-30T23:23:24Z")

</div>

> [@Paul\_B](#):
>
> Crude debounce there!  
> 😁

Thanks, I guess?

---

<div class="post-metadata">

**Author:** ![drknss](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@drknss](https://forum.arduino.cc/u/drknss)\
**Post date:** [May 31, 2022, 6:53am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/7 "2022-05-31T06:53:38Z")

</div>

Thanks! I'll try it when I get home. But I was wondering if the Arduino can works as a relay because I just need it to complete a circuit without supplying any power like it does for the LED on pin 13.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [May 31, 2022, 11:02am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/8 "2022-05-31T11:02:49Z")

</div>

> [@drknss](#):
>
> I was wondering if the Arduino can works as a relay

OK, so **now** you need to explain **exactly** what your proposed application is, what you want to connect it to, and why. You **must** give a Web link to the device and circuit to which you propose to connect it.

---

<div class="post-metadata">

**Author:** ![drknss](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@drknss](https://forum.arduino.cc/u/drknss)\
**Post date:** [May 31, 2022, 11:29am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/9 "2022-05-31T11:29:28Z")

</div>

So basically I need to connect the Arduino nano to a keyboard circuit board that connects to a keyboard so when I flip a switch it presses a button once. It is basically like this guy did but with toggle switches: [FAQ #231: Button box made from a keyboard](https://www.xsimulator.net/community/faq/button-box-made-from-a-keyboard.231/)  
I don't have a web link to the keyboard circuit board but I only need to short the connection between two signals on the keyboard circuit.

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [May 31, 2022, 11:33am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/10 "2022-05-31T11:33:17Z")

</div>

[Toggle Library](https://github.com/Dlloydev/Toggle)

In this example, pin 3 could be your "relay" (open drain output limited to 5V, 20mA):

```cpp
#include <Toggle.h>

const byte buttonPin = 2;
const byte openDrainPin = 3;
const byte ledPin = LED_BUILTIN;
const byte blinkMs = 100; // blink and open drain period (ms) after button press or release
bool blinkState = false, lastBlinkState = false;

Toggle sw1(buttonPin);

void setup() {
  sw1.begin(buttonPin);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  sw1.poll();

  blinkState = sw1.blink(blinkMs, 2); // blinks on press and release
  if (blinkState && !lastBlinkState) { // rising edge
    digitalWrite(ledPin, HIGH);
    pinMode(openDrainPin, OUTPUT);
  } else if (!blinkState && lastBlinkState) { // falling edge
    digitalWrite(ledPin, LOW);
    pinMode(openDrainPin, INPUT);
  }
  lastBlinkState = blinkState;
  sw1.onPress();
  sw1.onRelease();
}

```

Take a look at the `pressCode()` function and example. Lots you can do (225 things) with just one button.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [May 31, 2022, 11:47am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/11 "2022-05-31T11:47:27Z")

</div>

> [@drknss](#):
>
> I don't have a web link to the keyboard circuit board but I only need to short the connection between two signals on the keyboard circuit.

OK, you use a [74HC4066 Quad analog switch](https://assets.nexperia.com/documents/data-sheet/74HC_HCT4066.pdf) controlled by the Arduino.

Note the Arduino must be connected to the same power as the keyboard - but I think you knew that anyway because it just makes sense. 👍

---

<div class="post-metadata">

**Author:** ![drknss](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@drknss](https://forum.arduino.cc/u/drknss)\
**Post date:** [June 1, 2022, 6:50am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/12 "2022-06-01T06:50:24Z")

</div>

This code is perfect but the thing is I have 4 buttons that need 4 separate in and out pins on the Arduino and I'm still learning C#, so I was wondering what to change/duplicate to make it 4 buttons instead of 1.  
This is what I did so far but I don't know if it's correct and I don't know how to change the last void loop bit of code.

> [@](#):
>
> #include \<Toggle.h\>
> 
> const byte buttonPin1 = 2;  
> const byte buttonPin2 = 3;  
> const byte buttonPin3 = 4;  
> const byte buttonPin4 = 5;
> 
> const byte openDrainPin1 = 6;  
> const byte openDrainPin2 = 7;  
> const byte openDrainPin3 = 8;  
> const byte openDrainPin4 = 9;  
> const byte ledPin = LED\_BUILTIN;  
> const byte blinkMs = 100; // blink and open drain period (ms) after button press or release  
> bool blinkState = false, lastBlinkState = false;
> 
> Toggle sw1(buttonPin1);  
> Toggle sw1(buttonPin2);  
> Toggle sw1(buttonPin3);  
> Toggle sw1(buttonPin4);
> 
> void setup() {  
> sw1.begin(buttonPin1);  
> sw1.begin(buttonPin2);  
> sw1.begin(buttonPin3);  
> sw1.begin(buttonPin4);  
> pinMode(ledPin, OUTPUT);  
> }
> 
> void loop() {  
> sw1.poll();
> 
> blinkState = sw1.blink(blinkMs, 2); // blinks on press and release  
> if (blinkState && !lastBlinkState) { // rising edge  
> digitalWrite(ledPin, HIGH);  
> pinMode(openDrainPin1, OUTPUT);  
> } else if (!blinkState && lastBlinkState) { // falling edge  
> digitalWrite(ledPin, LOW);  
> pinMode(openDrainPin1, INPUT);  
> }  
> lastBlinkState = blinkState;  
> sw1.onPress();  
> sw1.onRelease();  
> }

Also, I don't know how to paste the code into the forum very well.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [June 1, 2022, 11:04am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/14 "2022-06-01T11:04:46Z")

</div>

> [@drknss](#):
>
> I have 4 buttons that need 4 separate in and out pins

OK, so the [74HC4066](https://assets.nexperia.com/documents/data-sheet/74HC_HCT4066.pdf) has exactly four such switches.

---

<div class="post-metadata">

**Author:** ![drknss](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@drknss](https://forum.arduino.cc/u/drknss)\
**Post date:** [June 1, 2022, 11:19am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/15 "2022-06-01T11:19:16Z")

</div>

I dont have a 74HC4066

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [June 1, 2022, 11:24am UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/16 "2022-06-01T11:24:19Z")

</div>

Suggest you go get one.

It will **do** the job you are asking.

The only other real alternative - not **quite** so easy - is to use four [2N7000 FETs](https://www.onsemi.com/pdf/datasheet/nds7002a-d.pdf).

---

<div class="post-metadata">

**Author:** ![drknss](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@drknss](https://forum.arduino.cc/u/drknss)\
**Post date:** [June 1, 2022, 12:28pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/17 "2022-06-01T12:28:50Z")

</div>

But the code that was pasted above by dlloyd did the job the only problem is that I need it to do that to 4 separate switches which I am trying to fix now.  
But thanks anyways for suggesting different solutions!👍  
Also if i can't do it by code, i do have 4 2N7000s that i can use.

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [June 1, 2022, 12:57pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/18 "2022-06-01T12:57:40Z")

</div>

Four buttons, four open drain outputs:

```cpp
#include "Toggle.h"

const byte buttonPin1 = 2;
const byte buttonPin2 = 3;
const byte buttonPin3 = 4;
const byte buttonPin4 = 5;
const byte openDrainPin1 = 6;
const byte openDrainPin2 = 7;
const byte openDrainPin3 = 8;
const byte openDrainPin4 = 9;

const byte ledPin = LED_BUILTIN;
const byte blinkMs = 100; // blink and open drain period (ms) after any button press or release
bool blinkState1 = false, lastBlinkState1 = false;
bool blinkState2 = false, lastBlinkState2 = false;
bool blinkState3 = false, lastBlinkState3 = false;
bool blinkState4 = false, lastBlinkState4 = false;

Toggle sw1(buttonPin1);
Toggle sw2(buttonPin2);
Toggle sw3(buttonPin3);
Toggle sw4(buttonPin4);

void setup() {
  sw1.begin(buttonPin1);
  sw2.begin(buttonPin2);
  sw3.begin(buttonPin3);
  sw4.begin(buttonPin4);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  sw1.poll();
  sw2.poll();
  sw3.poll();
  sw4.poll();

  // first button, first open drain output
  blinkState1 = sw1.blink(blinkMs, 2); // blinks on press and release
  if (blinkState1 && !lastBlinkState1) { // rising edge
    digitalWrite(ledPin, HIGH);
    pinMode(openDrainPin1, OUTPUT);
  } else if (!blinkState1 && lastBlinkState1) { // falling edge
    digitalWrite(ledPin, LOW);
    pinMode(openDrainPin1, INPUT);
  }
  lastBlinkState1 = blinkState1;
  sw1.onPress();
  sw1.onRelease();

  // second button, second open drain output
  blinkState2 = sw2.blink(blinkMs, 2); // blinks on press and release
  if (blinkState2 && !lastBlinkState2) { // rising edge
    digitalWrite(ledPin, HIGH);
    pinMode(openDrainPin2, OUTPUT);
  } else if (!blinkState2 && lastBlinkState2) { // falling edge
    digitalWrite(ledPin, LOW);
    pinMode(openDrainPin2, INPUT);
  }
  lastBlinkState2 = blinkState2;
  sw2.onPress();
  sw2.onRelease();

  // third button, third open drain output
  blinkState3 = sw3.blink(blinkMs, 2); // blinks on press and release
  if (blinkState3 && !lastBlinkState3) { // rising edge
    digitalWrite(ledPin, HIGH);
    pinMode(openDrainPin3, OUTPUT);
  } else if (!blinkState3 && lastBlinkState3) { // falling edge
    digitalWrite(ledPin, LOW);
    pinMode(openDrainPin3, INPUT);
  }
  lastBlinkState3 = blinkState3;
  sw3.onPress();
  sw3.onRelease();

  // fourth button, fourth open drain output
  blinkState4 = sw4.blink(blinkMs, 2); // blinks on press and release
  if (blinkState4 && !lastBlinkState4) { // rising edge
    digitalWrite(ledPin, HIGH);
    pinMode(openDrainPin4, OUTPUT);
  } else if (!blinkState4 && lastBlinkState4) { // falling edge
    digitalWrite(ledPin, LOW);
    pinMode(openDrainPin4, INPUT);
  }
  lastBlinkState4 = blinkState4;
  sw4.onPress();
  sw4.onRelease();
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 28, 2022, 12:58pm UTC](https://forum.arduino.cc/t/toggle-input-to-pulse-output-through-nano/997583/19 "2022-11-28T12:58:09Z")

</div>

Closed on November 28, 2022, 12:58pm UTC

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