Arduino Switches

The first part of interfacing with hardware is being able to manage digital inputs and outputs. With the built in Circuit Playground hardware & the Arduino library it's super easy!

This quick-start example shows how you can use one of the Circuit Playground buttons as an input  to control another digital output - the built in LED

#include <Adafruit_CircuitPlayground.h>

void setup() {
  // Initialize the circuit playground
  CircuitPlayground.begin();
}

void loop() {
  // If the left button is pressed....
  if (CircuitPlayground.leftButton()) {
      CircuitPlayground.redLED(HIGH);  // LED on
  } else {
      CircuitPlayground.redLED(LOW);   // LED off
  }
}

Note that we made the code a little less elegant than necessary, the if/then could be replaced with a simple CircuitPlayground.redLED(CircuitPlayground.leftButton()) but I wanted to make it super clear how to test the inputs.

Press Button A (the one on the left), and the onboard red LED will turn on!

Without Library Assist

If you are interested in 'lower level' interfacing to the hardware, you don't have to use the CircuitPlayground helper library. This code shows manually setting the button/LED pins to inputs & outputs as well as setting and reading the state of the pins:

#include <Adafruit_CircuitPlayground.h>

void setup() {
  pinMode(CPLAY_LEFTBUTTON, INPUT_PULLDOWN);
  pinMode(CPLAY_REDLED, OUTPUT);
}

void loop() {
  // If the left button is pressed....
  if (digitalRead(CPLAY_LEFTBUTTON)) {
      digitalWrite(CPLAY_REDLED, HIGH);  // LED on
  } else {
      digitalWrite(CPLAY_REDLED, LOW);  // LED off
  }
}

Note that on the M0/SAMD based CircuitPython boards, at least, you can also have internal pullups with INPUT_PULLUP when using external buttons, but the built in buttons require INPUT_PULLDOWN.

Maybe you're setting up your own external button with pullup or pulldown resistor. If you want to turn off the internal pullup/pulldown just set the pinMode() to plain INPUT

Last updated on 2017-12-01 at 04.56.58 PM Published on 2017-10-12 at 10.43.39 PM