Hold an On/Off State

by littleBits

Published on May 15, 2014

Detect when a button changes from off to on and on to off.

*To complete this exercise, you will need to download the Arduino software.

    This sketch turns a momentary switch (like the button) into a toggle switch, just like the latch module does. This means that every time the button is pressed it will hold its state either on or off. This change of state (on to off/off to on) is detected by the Arduino and recorded, allowing you you to keep track of the number of times the button in pressed (in the serial monitor).  This is called a state change detection, or edge detection.

    For more “getting started” sketches, click here.

    For support check out the littleBits and Arduino forums.

    To learn more about coding, find examples on Arduino’s website.

    How To Make It


    Download the Arduino software to your computer (it's free!) and open the "Hold an On/Off State" sketch. The "Hold an On/Off State" sketch can be found on this page in the "other files" section.


    Build your circuit (see the photo gallery for an image of the setup). Make sure the button is connected to the A0 input pin and the bargraph is connected to the D5 output pin.


    Plug the micro USB cable into both the Arduino module and a USB port on your computer.


    Turn your power module on. You should see a red light illuminate on the power module and a yellow light flash on your Arduino module.


    In the Arduino program, select your board. Go to Tools>Board>Arduino Leonardo. This littleBits Arduino module is modeled after the Arduino Leonardo and is therefore compatible.


    Next select the serial port that the Arduino will communicate over by going back to "Tools" and selecting the correct port in the "Port" section. The name of the port will depend if you are on a Mac or Windows computer. On a Mac, it will start with /dev/tty.usbmodem... and a PC will start with COMM… In Windows, you can look for the USB serial device in the ports section of the Windows Device Manager. If you are on a Linux machine, the port will look like /dev/ttyUSB…


    To upload the Arduino sketch to your Arduino module, press the arrow button in the top left corner of the screen. You will see a blue bar at the bottom of the window that says "compiling sketch", and you should see two yellow lights flash on your Arduino module when this happens. The upload is complete when you see "done uploading" at the bottom of your sketch window.


    Once the program is uploaded, you will see that when you press the button, the bargraph will hold its state either on or off. If you open the serial monitor (the little magnifying glass icon in the top right corner of the sketch window), you will see that the Arduino counts how many times you press the button.

    Related Projects

    Street Light

    Someone walks down the street and steps on the presser sensor and the street light lights up. 

    Simon Clone

    Combining the Arduino module and a few basic littleBits modules, I made a simple clone of the classic 1980s electronic game of Sim...

    Speech Puppet

    We pantograph, has developed a robot puppet that speech.Place the microphone on the desk ,  Light sensor reacts.  Dr. st...