Buttons

Create a new sketch and upload the following code to read the button presses in the Serial Console

Download: file
const int buttonA = 5;     // the number of the pushbutton pin
const int buttonB = 11;     // the number of the pushbutton pin

void setup() {  
  Serial.begin(9600);
  
  Serial.println("microbit is ready!");
  
  pinMode(buttonA, INPUT);  
  pinMode(buttonB, INPUT);   
}

void loop(){
  if (! digitalRead(buttonA)) {
    Serial.println("Button A pressed");
  }
  if (! digitalRead(buttonB)) {
    Serial.println("Button B pressed");
  }
  delay(10);
}

Open up the Serial console at 9600 baud and press the reset button on the back of the microbit to restart the software

You will see the welcome message and then try pressing some buttons!

This guide was first published on Sep 27, 2017. It was last updated on Sep 27, 2017. This page (Buttons) was last updated on Sep 27, 2017.