The adafruitio_06_digital_in example uses digital pin 5 by default on all boards, and that can be modified if needed by changing the BUTTON_PIN define.

Note: If you are using the WICED Feather, you will need to change the BUTTON_PIN define to PC5 instead of the default setting of 5.

    /************************ Example Starts Here *******************************/

// digital pin 5
#define BUTTON_PIN 5

The next chunk of code sets up two boolean variables to track button state, and an Adafruit IO Feed instance for a feed called digital.

// button state
bool current = false;
bool last = false;

// set up the 'digital' feed
AdafruitIO_Feed *digital = io.feed("digital");

In the setup function, we set the BUTTON_PIN as a digital input, and connect to Adafruit IO. The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check config.h for any typos in your username or key.

void setup() {

  // set button pin as an input

  // start the serial connection

  // wait for serial monitor to open
  while(! Serial);

  // connect to
  Serial.print("Connecting to Adafruit IO");

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {

  // we are connected


Next, we have the main loop() function. The first line of the loop function calls; this line will need to be present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO, and processes any incoming data.

void loop() {

  //; is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  //, and processes any incoming data.;

The next chunk of code inside the loop() checks the current state of the button, and saves the state of the button in the current variable. Because we are using a pullup resistor, we will need to flip the button state.

If the button state is LOW it means the button is pressed, so we set current = true;. If the button state is HIGH it means the button is released, so we set current = false;.

We then check if the current button state is equal to the last button state. If it is equal, we will return early and not continue with the rest of the loop.

  // grab the current state of the button.
  // we have to flip the logic because we are
  // using a pullup resistor.
  if(digitalRead(BUTTON_PIN) == LOW)
    current = true;
    current = false;

  // return if the value hasn't changed
  if(current == last)

The final chunk of the loop() function prints the current value to the Arduino Serial Monitor, and sends the current value to the digital feed on Adafruit IO. We also set last = current; so we can tell if the state of the button has changed in the next run of the loop.

  // save the current state to the 'digital' feed on adafruit io
  Serial.print("sending button -> ");

  // store last button state
  last = current;


Upload the sketch to your board, and open the Arduino Serial Monitor. Your board should now connect to Adafruit IO.

Connecting to Adafruit IO....

Adafruit IO connected.

You can now press the button, and you should see button presses being sent to Adafruit IO.

sending button -> 1
sending button -> 0
sending button -> 1
sending button -> 0

Check your dashboard on Adafruit IO, and you should see the gauge respond to button presses.

This guide was first published on Apr 25, 2015. It was last updated on Apr 25, 2015.

This page (Arduino Code) was last updated on Feb 15, 2017.

Text editor powered by tinymce.