Your first Arduino project: blink, button and buzzer

Adafruit Metro RP2040, an Arduino Uno-shaped development board

Level: Beginner  ·  Time: 1 hour  ·  Board: Arduino Uno R4 or R3 (any Uno-compatible board)

Three parts teach you most of what a microcontroller does. An LED is an output: the board decides when it lights. A push button is an input: the board waits for you. A buzzer is an output that changes over time, which is where timing comes in. Wire all three to an Arduino and you have written your first real program: something that senses the world and reacts to it.

No soldering, no prior electronics and no prior programming. We'll go one part at a time, upload after each change, and finish with a single sketch that runs them together.

What you need

You'll also need one resistor between 150 Ω and 1 kΩ (220 Ω is the classic choice) and the USB cable that fits your board.

Which board? We don't stock Arduino's own boards. An Arduino Uno R4 Minima or an older Uno R3 is the easiest first board and needs no extra setup, and everything below is written for it. The Metro RP2040 above is the same shape and runs the same sketches, but it's a 3.3 V board and needs one extra step in the IDE, which step 1 covers.

1. Install the Arduino IDE

Download the Arduino IDE for Windows, macOS or Linux from arduino.cc/en/software and install it. Open it and plug your board in with the USB cable. The board's power light comes on.

Now tell the IDE what you plugged in. In the toolbar, open the board selector (it says Select Board) and pick your board from the list of detected ports.

  • Uno R3: it should appear straight away. Support for it ships with the IDE.
  • Uno R4 Minima or WiFi: open Tools → Board → Boards Manager, search for UNO R4, and install Arduino UNO R4 Boards. Then select the board.
  • Metro RP2040: open File → Preferences, paste the following into Additional boards manager URLs, then install Raspberry Pi Pico/RP2040 in the Boards Manager and choose Adafruit Metro RP2040.
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

If no port appears at all, the cable is usually the problem: some USB cables only carry power. Try another one.

2. Blink the light that's already there

Every Arduino has a small LED wired to one of its pins, so you can test the whole toolchain before touching a wire. Go to File → New Sketch, replace everything in the window with this, and save it as blink:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

Press the Upload button (the right-pointing arrow). The IDE compiles the sketch, sends it over USB, and after a second or two the small LED starts flashing once a second. That's the whole loop working: your computer, the cable, the board and your code.

Change both 500s to 100 and upload again. The blink speeds up. Those numbers are milliseconds, so 500 is half a second.

3. What the sketch actually says

An Arduino program is called a sketch, and it always has the same two parts:

  • setup() runs once, the moment the board powers up. It's where you say what each pin is for.
  • loop() runs over and over, forever, as fast as the board can manage. It's the behaviour of your project.

And four functions do most of the work:

  • pinMode(pin, OUTPUT) — this pin will drive something.
  • digitalWrite(pin, HIGH) — turn that pin on. LOW turns it off.
  • delay(ms) — wait, doing nothing, for that many milliseconds.
  • digitalRead(pin) — read whether a pin is HIGH or LOW. We'll use this for the button.

LED_BUILTIN is just a name for the pin that onboard LED sits on (pin 13 on an Uno). Using the name means the same sketch works on a board that wired it elsewhere.

4. Your own LED on the breadboard

A breadboard is a grid of clips. In the two big banks of holes, each row of five is joined together under the plastic; the long strips down the edges are the power rails, joined end to end. Anything pushed into the same row is connected.

Unplug the USB cable before you wire anything. Then:

  1. Run a jumper wire from the board's GND pin to the breadboard's blue or black rail. Everything will ground there.
  2. Push the LED's two legs into two different rows. One leg is longer: that's the positive one (the anode). The shorter leg sits next to a flattened edge on the LED's rim.
  3. Put the resistor between the short leg's row and the ground rail. Resistors have no direction, so either way round is fine.
  4. Run a jumper from pin 9 on the board to the long leg's row.

The resistor is not optional. An LED on its own will pull as much current as the pin can give and burn out, usually taking the pin with it. The resistor limits the current to something both parts are happy with.

Plug the USB cable back in and upload this:

const int LED_PIN = 9;

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
}

Same sketch as before, except the pin number now lives in a name at the top. Naming it once means you change the wiring in one place instead of hunting through the code.

Tip: Nothing lighting up? The LED is almost certainly the wrong way round. It only conducts in one direction, and backwards it simply sits there, with nothing to see and nothing damaged. Pull it out, turn it round, push it back in. If it still won't light, check that the resistor really does reach the ground rail and not just a neighbouring row.

5. Add the button

Unplug the USB cable again. The button has two legs: push them into two different rows, then wire one row to pin 2 and the other row to the ground rail.

That's all the wiring a button needs, because of INPUT_PULLUP. The board has a resistor built in that gently holds the pin at HIGH when nothing is happening; pressing the button connects the pin to ground and drags it to LOW. So the reading is inverted: LOW means pressed. Without the pull-up, an unpressed pin is connected to nothing at all and reads randomly.

Upload this and open Tools → Serial Monitor. Set the drop-down in the Serial Monitor to 9600 baud to match the sketch, or you'll see nothing but gibberish:

const int BUTTON_PIN = 2;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    Serial.println("pressed");
  } else {
    Serial.println("released");
  }
  delay(200);
}

Hold the button down and the stream of released turns into pressed. The Serial Monitor is the single most useful debugging tool you have: when a project misbehaves, print the values you think you're getting and compare them with reality.

Now make the button do something. Both parts are already wired, so this only needs a code change:

const int LED_PIN = 9;
const int BUTTON_PIN = 2;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Press, and the LED lights. Release, and it goes out. There's no delay() in here at all: loop() is checking the button thousands of times a second, which is exactly what you want from an input.

6. Add the buzzer

Unplug the cable once more. The piezo buzzer has two pins: one to a row wired to pin 8, the other to a row wired to the ground rail. A piezo makes no sound from a steady voltage, so the board has to switch the pin on and off thousands of times a second. tone() does that for you.

const int BUZZER_PIN = 8;

void setup() {
  tone(BUZZER_PIN, 440, 200);   // 440 Hz for 200 ms
  delay(300);
  tone(BUZZER_PIN, 880, 200);   // an octave up
}

void loop() {
}

Two short beeps at power-up, then silence, because loop() is empty. The arguments are the pin, the frequency in hertz and how long to hold it. Higher numbers mean higher notes: 440 Hz is the A that orchestras tune to.

The third argument is what keeps this simple. tone() starts the note and returns immediately, and the board stops it on its own after the time you gave. The delay(300) is there so the first note finishes before the second one starts. Leave the duration out and the note runs until you call noTone(BUZZER_PIN).

7. All three together

Everything is wired. This last sketch beeps once on each press, holds the LED on while you hold the button, and prints what it's doing:

const int LED_PIN = 9;
const int BUTTON_PIN = 2;
const int BUZZER_PIN = 8;

int lastState = HIGH;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {
  int state = digitalRead(BUTTON_PIN);

  // the button has just gone down
  if (state == LOW && lastState == HIGH) {
    digitalWrite(LED_PIN, HIGH);
    tone(BUZZER_PIN, 1760, 80);
    Serial.println("press");
    delay(20);                   // let the contacts settle
  }

  // the button has just come back up
  if (state == HIGH && lastState == LOW) {
    digitalWrite(LED_PIN, LOW);
    delay(20);
  }

  lastState = state;
}

The new idea here is lastState. Without it, loop() would see the button held down thousands of times a second and start a new beep every time, which sounds like a buzz rather than a beep. Comparing the pin now against what it was last time means the code can act on the moment it changed, not on the fact that it's down.

The two short delay(20) calls are debouncing. Metal contacts don't close cleanly: for a millisecond or two they chatter open and shut, and the board is quick enough to count every bounce as another press. Pausing briefly after a change ignores the chatter.

Once the sketch is uploaded it belongs to the board. Unplug the USB cable, plug the board into a phone charger or a battery pack, and it does the same thing without a computer anywhere near it.

Where to go from here

Small changes to this sketch are worth more than a new project. Try a few:

  • Swap digitalWrite(LED_PIN, HIGH) for analogWrite(LED_PIN, 64). Pin 9 can dim as well as switch, and any value from 0 to 255 works.
  • Make the press toggle the LED instead of holding it, so one press turns it on and the next turns it off. You already have the two edges you need.
  • Give the buzzer a short melody: an array of frequencies and a for loop over it.
  • Add a second LED and button on two more pins. Nothing new to learn, which is the point.

Next steps