arduino 2 pin button


If I press any of these buttons. I press this button goes that way and if I press any of the other buttons it actually basically starts from wherever I press. We need to be able to read eight buttons and to write eight LEDs and that normally takes 16 pins. I only have connected here, two of the Arduino pins basically used two pins, a five and a four and the ground, and the power and that’s also ground and that’s it so basically ground power, and these two wires connected to the io expander that we talked about In the last video, but unlike in the last video now there’s, only one and it’s doing both input and output at the same time, which is pretty cool, so here’s the diagram. There are eight of these, of course, but I only dropped one they’re, all the same. Let’S talk about the two scenarios. One of them needs to read the button and the other one is to actually turn on the LED let’s turn on the LED first that’s simpler. Normally, all these pins will be all high, so it will be a plus here and a plus there, and therefore the LED will not let up because they’re balls plus to turn on one of these LEDs. What you would do in the code is you set the desired LED bit to be low, and so, when this is low, now all of a sudden, we have the current going through here through the LED and lit up so that’s how you would actually turn on Any of these LEDs by putting the particular bit one of those eight bits low now to actually read the button, you would first set the actual bits too high.

This is still will not lit up, but now it could detect. When you press this button, we will be bringing that pin low to ground and it will be able to detect. It is now low. So let’s look at the code so here’s the code, maybe it’s about two screenful. It shouldn’t take too long to go through since our io expander is a nice quality device. We need a wire library. I do not know why they call it wire and certify square C library, but that’s what we need I’m going to paste it here to notepad plus plus. So I could magnify the code like this so that’s, the library we need to talk to our io expander, which is on the I square C bus. We need some variable to keep track of what is the actual address of our I Square C device and I’m going to skip these for now and I’m going to go down here to the setup we need to initialize the wire library and since I’m, on the Nano I can take the default io port for I Square C, which is a 4 and a 5 and here’s the address of our I Square C device. The IOA expander is at address 20 next let’s talk about the two functions. I wrote to make life easier to communicate with the io expander. One of them is to write data to the io X vendor the other one is going to be to read data from so let’s talk about the write first, so you just tell it what pattern you want to display on these LEDs.

The first thing we do is we tell the wire library that we’re about to send something at that I Square C address, and then we actually send their bits and then we just say we’re done. This is going the other way. Instead of writing we’re about to read something from these switches, this is something specific to the io expander. It requires to actually set all the outputs to one before we can do a read and then we actually start doing the read and to do the rate is very similar, so we say we are about to communicate with that address and now we are Wes. Some data from that address and we tell it how many bytes we want. So when the data arrive, we put it into this variable and then we’re done. We basically return that variable onto whoever call us now that we know how to read and write to our expander let’s. Look at our loop loop is pretty simple. We just keep on reading those switches as long as nobody press any other switches. All these bits will be one remember, because when we press the button we’ll bring that to ground. So if nobody pressed that all these bits will be all ones. So as long as it is one we don’t want to do anything, but as soon as it is not all one, we do something now that we know that somebody press one of these switches. We need to figure out which one of those switches is actually press and to do that.

I just basically loop through bit index 0 through 7 I’ll check this bit and then I’ll check that bit and so on and so forth. Until I reach this one and for every one of those I just say, look at the bits that we got from the io expander and check that particular index, so bit index will be starting with 0. So we’ll check is this bit zero? Meaning did somebody press that button? If not we continue on, is? Is this bit zero, meaning somebody press that switch and so on and so forth until we find one that actually is low, which means that’s the one that’s being pressed and then when that happens, we know the index 0. 1. 2. 3 let’s say we press this. One we know that it’s bit index 3, that somebody press – and we just passed that bit index 3 to the explosion, so the animation could start doing the animation. From that point to the left and to the right, which we will see next so let’s say we pass in a 3 here, so that’s a start edge is going to be 3. What we’re doing basically is we’re trying to figure out something to its left and to its right and just continue on on both direction so to do that loop. I have basically this I going from 0 through 7 and it’s going to go both direction. We’Ll see here in a second, so on the lower side we say take wherever you’re starting and subtract 0 originally, which means it’s going to start wherever it was, and then the higher side we add 0, which is going to be whatever it was.

If so, you’ll stay at 3 and these two, if statement here, make sure that we are not trying to set a bit that is like outside. That is greater than the number of bits we have here or less than number of bits. So as long as we’re within the range we’re actually going to clear that bit, remember we’re going to assume that all the bits is going to be set to high and high actually means that they are off. Remember with this is plus, and that plus means off. So to turn the LED on, we actually want to clear the bit. So originally we only clear the bit. That is the one that is passed in here. So that will be three. This will be three BB 3, so we clear BIP 3 and we write it to the io expander. So bit 3 is the only one though it’s going to be low because that’s you only been cleared, all the other ones are still high, and so bit B is the only one that’s going to be turned on and then the next time around within this loop Here I will be close to one so now we are going to subtract one from this one we’re going to add one from that one. So basically, instead of three we’re going to have one of them setting this one there one setting that one again, we start with all of them being one meaning none of our on and we clear the bit for and bit two and that will turn on those Two LEDs and all the other ones will be off, and then we go on and so on and so forth, that’s yet bit five and that one will be on and not know the other ones will be off, because every time we go through a loop, we Set the bits all to back to one here and then after we set a bit, we actually wait here if we clear them right away.

We can’t see it. So we wait for 90 milliseconds, so we could actually see that and then we clear the display, meaning set them all back to high meaning turn them all off, and we just do this over and over again until we do this seven times guaranteeing that if we Start from here we hit or that one or we start from here we have that one if we start on the middle. Actually we did not have to go all the way to seven, but it doesn’t matter, because we have this excitement over here. That checks make sure that we do not set a bit that is beyond our boundary but that’s everything. So I was here to code in a youtube description. You can check it out and download and play with it. It’S been a fun project. I think it’s. So cool to be able to deal with basically sixteen io pins with just the two wires.


arduino 2 pin button Video







arduino 2 pin button news








arduino 2 pin button Social






👋 Want to bring Tony Stark-like gesture control to your projects? Learn how with the BLE-enabled MKR WiFi 1010 and Nano 33 BLE Sense boards using the ArduinoBLE library.
safe image.php?d=AQDyi1ELcW4VGzhx&w=720&h=720& dChOhNMmeF - arduino 2 pin button

Arduin-yo ho ho! A fairground favorite, the pirate ship is a fun way to explore the oscillation of a pendulum. How much fun, you ask? Access our Science Kit Physics Lab preview and see for yourself:

🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today:

70537898 550174072395145 8798850142103928832 n.jpg? nc cat=102& nc oc=AQmCfMPwMf1oPpYqFhzbxfnGdbrmsjZXrEEBy1ox5Z8EJtiYMADoFNZ5mnSNRgCCY74& nc ht=scontent - arduino 2 pin button



Originally posted 2018-04-26 02:18:25.

(Visited 84 times, 1 visits today)

About The Author

You might be interested in

Comment (43)

    1. If you look at the loop code it will draw the animation for the lowest button pressed.

      As buttons are only read at the start of the loop, while the animation is being displayed, pressing the button will do nothing

  1. checking bit one by one eh…hmmm i’m expecting the use of interrupt pin, but this is could be sometin’ else

  2. I did something similar before watching this video
    I used the same tecnique as in the 4×4 button matrix video and an 8×1 neopixel stick but didn’t have animations like that but mine is in full color(I only used red and green)

  3. My question is can you use a pulse at +5v at each switch. Perhaps using a 74hc595 8-bit shift register instead?

    1. Yes, instead of using this chip, you could have the Arduino sequentially power up one of output pin of a shift register and then have one Arduino pin read a common line.

  4. Great video! Got a few questions for you, one is: can this be used on the Arduino Due, it sounds crazy but yes It looks like my project is going to max out the pins on the Due, and I wanted to know is there any conflict when working with the ARM processor instead of the AVR? 2nd: is the communication fast enough to initiate interrupts and limit switches, I noticed from past experience that the I2C bus really isn’t adequate for running multiple stepper motors in shield format at high speeds but could this shift register activate limit switch interrupts without any delay? and 3rd question: how did you make your wired connection lay flat like that?? its just beautiful, I applaud you, well done! 🙂

    1. Thanks Rich,
      1. Yes, this chip should work with any microcontroller that supports I2C protocol.
      2. I don’t know how fast you need things to be, but serial is by nature “slow”. The datasheet says the clock max is 100Khz, but we need several clock cycles to send command bytes.
      3. I treat breadboarding like arts and crafts. It is satisfying to me to see pretty wires. So, I color code by line type, cut each wire to the perfect length, and use needlenose pliers to bend it just at the right places. Thx for noticing that Rich!

  5. Hi Hari
    I am a Arduino beginner and cannot find a suitable solution through Ardiuno website.

    How can I program the Arduino with two PCF8574?
    I am not sure if it is work, but going to connect the 2nd PCSF8574 pin9 (P4) to the first PCSF8574 pin4(P0) making it become a “16bits” information. And, connecting the Vcc, SDA, SCL same as the first one.
    I think of changing it to be uint16_t address; but is it ok to “WriteIo(B1111111111111111)” ??
    ALso,, can only read for 1byte, how can I make it to be 2 or more bytes?

    Sorry if confusing you.

    1. Hi, welcome to the fun world of Arduino!
      Unlike the more popular 74HC595 shift register, the PCF8574 is NOT designed to be daisy chained. 🙁

      You CAN connect multiple PCF8574 to the same Arduino by wiring a different address to each PCF8574.
      Unless you also need to have many inputs, the shift register is much easier to use.

      As far as binary constants, the Arduino IDE only supports 8 bits at a time 🙁
      However, IF you use the shift register chip (74HC595) then you can use the command ShiftOut() repeatedly as often as you have shift register chips in the daisy chain.

      Here is a video I did on Shift Registers.

      Let me know if you rather use the PCF8574 and I can help you connect more than one.

    1. Good question! I haven’t tried it, but I think this is what would happen.
      It is not multi-tasking, so pattern would complete before it reads any button.
      However, since pressing the button provides path to ground for the LED, that LED would be on for as long as you press the button.

  6. Hello Hari
    I have done a little playin’ around with your little demo, and it works brilliant! – Thanks for sharing it.

    I would like to use your program “the other way around” – So 8 active high push buttons and 8 active low LED’s – hereby I can drive the LED’s via a MOSFET and do PWM.
    I therefore made some small changes to your program – And the reading works, but the animation is hardly visible.
    Can you detect the error for me?


    uint8_t address;

    void WriteIo(uint8_t bits)

    uint8_t ReadIo()
    WriteIo(0x00); // PCF8574 require us to set all outputs to 0 before doing a read.

    Wire.requestFrom((int)address, 1); // Ask for 1 byte from slave
    uint8_t bits =; // read that one byte
    return bits;

    void ExplosionAnimation(uint8_t startAt)
    for (int i = 0; i < 8; i++) { uint8_t bits = 0x00; // All bits off // Turn on bits as long as it is not beyond our one byte "display" int lowerSide = startAt - i; int higherSide = startAt + i; if (lowerSide >= 0) bitSet(bits, lowerSide);
    if (higherSide <= 7) bitSet(bits, higherSide); WriteIo(bits); // Display the computed bit pattern delay(90); // Allow humans to see the bit pattern WriteIo(0x00); // Erase "display" } } void setup() { Wire.begin(); // Arduino needs (SDA=A4,SCL=A5) //Wire.begin(0,2); // ESP8266 needs (SDA=GPIO0,SCL=GPIO2) address = 0x20; } void loop() { uint8_t bits = ReadIo(); // Read all switches //-- Don't do anything unless they press a switch -- if ( bits != 0) // Unless they're all high... { //-- Find lowest pressed switch -- for (byte bitIndex = 0; bitIndex < 8; bitIndex++) if (bitRead(bits, bitIndex) == 1) { ExplosionAnimation(bitIndex); exit; } } }

    1. Hi Tomas,
      Your code looks good. I don’t see any mistakes.

      I wonder if the reason it doesn’t work well is because the chip is designed to sink current to ground rather than supply positive voltage. on page 12 of the datasheet, I noticed a symbol labeled 100 micro Amp on the positive side. I don’t know what that symbol means, but maybe that’s the max positive current it can provide? Furthermore all the examples on pages 15 and 16 has are wired up with cathode of LED onto the chip (that is the chip provides ground to light up the LED).

      BTW, shouldn’t PWM still work regardless whether your MOSFET pays attention to + or – side of the PWM? All we’re doing is changing the ratio, no? Granted the ratio would be backward, but you could easily account for that in how you code things.

      Interesting problem. Thx for the comment!

    2. Hi again Hari
      Thanks for responding so quickly 🙂
      I see your point – Of course it would be better to sink the current for the PCF8574 – but in the datasheet it says: “Latched Outputs With High-Current Drive
      Capability for Directly Driving LEDs” – But the I_o max values talk for themselves…
      I’ll just place the MOSFET at the anode of the LED’s instead – (I would like to control the brightness of all LED’s simultaneously)


    1. There is no built-in way to PWM the outputs of the IO Expander. But nothing is stopping you from pulsing the outputs of the IO expander yourself by setting the value high and low repeatedly, just like PWM does.

    2. Thanks for the prompt reply. I came across this PCA9685, will look into that, if that does not work will try out your suggestion.

  7. Hi ! I want to use your circuit with a Lenonardo and just 8 buttons( 2 pins push-pull). I just need to connect each P0-7 to each button, and then the second pin of each button is linked to ground ? Thanks in advance for your reply.

    1. @Hari Wiguna I think I will do the same schematic as yours with an arduino Nano, as I don’t get anything with my arduino Leonardo..

    2. Hmm, that’s frustrating. It should work with the Leonardo. Have you tried switching the wiring to use digital pins 2 and 3 instead of analog pins A4 and A5? Have you tried running one of the I2C examples to see if the Leonardo can talk to the I2C device?

    3. @Hari Wiguna I found the problem… I am using PCF8574A instead of PCF8574N, so adress isn’t 0x20… I ran this code to find it :
      On the leonardo, I tried every pins (but the problem wasn’t there..). I got SDA and SCL specific pins on the board, they work.
      I just put buttons between pcf8574 pins and ground, and it works.

      Thanks for your help ! Too bad I can’t post a picture of my logitech g27 with all the new buttons ! 🙂

    4. Great job! and thanks for sharing the info! Ah, so the pullup is unnecessary for inputs. Good to know.
      If you don’t mind the picture being out in public on the Internet, you can upload it to one of the free image sharing service such as
      and share the link here. Glad you got it to work!

  8. Great vid! Really interesting
    But i got some questions, what happens if you press more than 1 button? Can the circuit read everything?

    1. Thanks Rodrigo.
      All eight button states are reported as bits in the one byte that is returned on the read operation.
      So, yes, you can press more than one button and all will be reported in the read.

  9. Would be much easier to get a grasp of what is happening if the video covered initially, how to do just switches, then how to do just outputs, and then combine into your “Christmas tree” display.
    Trying to follow your method unless one has a good grasp of the process of how the reads and writes operate, is almost impossible.
    Fine for you or those that know or those that just want to copy and paste.
    Will have to get the code and study it.

  10. If I use this IC more than one with single arduino
    Then the address of this two separate IC s will different
    Am I correct??

    1. Yes, you are corredt. You can have two (or more) PCF8574 ICs connected to the same Arduino as long as each PCF8574 is configured to have different addresses.

  11. I found a library on github named as ___”PCF8574_library-master”___.a sample programe is given over there

    #include “Arduino.h”
    #include “PCF8574.h”

    // Set i2c address
    PCF8574 pcf8574(0x39);

    void setup()

    // Set pinMode to OUTPUT
    pcf8574.pinMode(P0, OUTPUT);
    pcf8574.pinMode(P1, INPUT);

    void loop()
    pcf8574.digitalWrite(P0, HIGH);
    pcf8574.digitalWrite(P0, LOW);

    Now if I want to use two IC then how write the another IC s address & how define the output/input pin in the same programme
    Please help me.
    Waiting for your kind help…………….

    1. There are many libraries for this chip, so I’m not sure which one you’re trying to use, but here is one that is on Arduino website and it illustrates using more than one address.
      PCF8574 PCF_38(0x38); // use PCF_38 to refer to this chip
      PCF8574 PCF_39(0x39); // use PCF_39 to refer to this chip
      Good luck!

  12. For the ExplosionAnimation function, use this instead: for (int i = 0; i < (8 - startAt); i++) ...It's a little messy to continue the loop unnecessarily, particularly when there's a delay() in there.


Your email address will not be published. Required fields are marked *