10: Color Picker

This project lets buttons choose which LED turns on.

This is your first “inputs + outputs” code project. The buttons are inputs your code reads, and the LEDs are outputs your code controls.

Coding ideas you will use (once the code is added):

Learn more (optional):

Goal

Press one button to turn on one LED and another button to turn on a different LED.

Parts you need

Important: choose the right resistor

Your kit has 150Ω and 100Ω resistors.

  • For a red or yellow LED, use 150Ω
  • For a green, blue, or white LED, use 100Ω

Using the wrong resistor can damage an LED.

Wiring idea

Build it first, then compare your setup to the diagram. Did you connect each part to the pin you meant to use?

Suggested setup:

Each LED still needs its own resistor, and the short leg (-) of each LED goes to GND.

For the buttons, connect one side to the GPIO pin and the other side to GND. The code uses the board's built-in pull-up resistors, so you do not need extra resistors for the buttons.

Build steps

Try each step, then check your work with the blurred photos below. Did you connect it the way you meant to?

  1. Place two different color LEDs and two buttons on the breadboard. Make sure each button bridges the center of the breadboard. Place two different color LEDs and two buttons on the breadboard.
  2. Connect the GND rail to GND on the RP2040-Zero. Then connect the short leg (-) of both LEDs to the GND rail using resistors. Connect the GND rail to GND on the RP2040-Zero, and both LEDs to GND using resistors.
  3. Connect one side of each button to the GND rail. Connect the buttons to GND.
  4. Connect the long leg (+) of each LED to a GPIO pin: the red LED to GP0 (GPIO 0) and the yellow LED to GP1 (GPIO 1). Connect the LEDs to GPIO pins.
  5. Connect the other side of each button to a GPIO pin: button A to GP2 (GPIO 2) and button B to GP3 (GPIO 3). Connect the other side of the buttons to GPIO pins.

  6. Plug the board into USB, load the code below, then press each button. Press button A: the red LED turns on. Press button B: the yellow LED turns on.

Only one LED should be on at a time, and both should turn off when you let go.

Code

Pin reminder

Wired your LEDs or buttons to different GPIO pins? Update LED_A_GPIO, LED_B_GPIO, BUTTON_A_GPIO, and BUTTON_B_GPIO in the code below to match your wiring.

import machine
import time

# If you wired different GPIO pins, change these numbers to match.
LED_A_GPIO = 0  # red LED in our build
LED_B_GPIO = 1  # yellow LED in our build
BUTTON_A_GPIO = 2
BUTTON_B_GPIO = 3

# Pin mapping
led_a = machine.Pin(LED_A_GPIO, machine.Pin.OUT)
led_b = machine.Pin(LED_B_GPIO, machine.Pin.OUT)

# Buttons connect to GND, so use the internal pull-up resistors.
# Not pressed = 1, pressed = 0
button_a = machine.Pin(BUTTON_A_GPIO, machine.Pin.IN, machine.Pin.PULL_UP)
button_b = machine.Pin(BUTTON_B_GPIO, machine.Pin.IN, machine.Pin.PULL_UP)

print("Color Picker Starting...")

while True:
    if button_a.value() == 0:
        # Button A pressed: LED A only
        led_a.value(1)
        led_b.value(0)
    elif button_b.value() == 0:
        # Button B pressed: LED B only
        led_a.value(0)
        led_b.value(1)
    else:
        # No button pressed: both off
        led_a.value(0)
        led_b.value(0)

    time.sleep(0.01)  # Short pause between checks
Arduino Code Coming Soon

What to notice

Challenge

Change which LED each button controls, or make both LEDs turn on when both buttons are pressed.

Harder challenge: make each LED stay on after you let go of its button, and turn off when you press that button again. (Hint: you will need a variable to remember whether each LED is on or off.)