Warning Light

From the name of GPIO (General-purpose input/output), we can see that these pins have both input and output functions. In the previous two projects we used the output function, in this project, we will use the input function to input the Slide value, and then control the LED to blink, like a warning light.

Schematic

_images/sche_warning_light.png

Wiring

_images/warning_light.png
  1. Connect the 3V3 pin of Pico to the positive power bus of the breadboard.

  2. Connect one end (either end) of the 220 ohm resistor to GP15, and insert the other end into the free row of the breadboard.

  3. Insert the anode lead of the LED into the same row as the end of the 220Ω resistor, and connect the cathode lead across the middle gap of the breadboard to the same row.

  4. Connect the LED cathode to the negative power bus of the breadboard.

  5. Insert the slide switch into the breadboard.

  6. Use a jumper wire to connect one end of slide switch pin to the negative bus.

  7. Connect the middle pin to GP14 with a jumper wire.

  8. Use a jumper wire to connect last end of slide switch pin to the positive bus

  9. Use a 10K resistor to connect the middle pin of the slide switch and the negative bus.

  10. Use a 104 capacitor to connect the middle pin of the slide switch and the negative bus to realize debounce that may arise from your toggle of switch.

  11. Connect the negative power bus of the breadboard to Pico’s GND.

When you toggle the slide switch, the circuit will switch between closed and open.

Code

After the code has run, toggle the Slide switch to one side and the LED will flash. Toggle to the other side and the LED will go out.

How it works?

For switchs, we need to set their mode to INPUT in order to be able to get their values.

void setup() {
    // initialize the LED pin as an output:
    pinMode(ledPin, OUTPUT);
    // initialize the switch pin as an input:
    pinMode(switchPin, INPUT);
}

Read the status of the switchPin in loop() and assign it to the variable switchState.

switchState = digitalRead(switchPin);

If the switchState is HIGH, the LED will flash.

if (switchState == HIGH)
{
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
}

Otherwise, turn off the LED.

else
{
    digitalWrite(ledPin, LOW);
}