Week 01

Week 01 #

Assignment 01: LED Counter #

Create a circuit and Arduino code that does the following

Circuit #

  • Connect two LEDs to your Arduino using a breadboard
  • Connect one switch to your Arduino using a breadboard

Code #

  1. Read a momentary switch being pressed
  2. When the program starts, both LEDs are off
  3. When the switch is pressed once, the first LED turns on
  4. When the switch is pressed the second time, the second LED turns on (the first one should also still be on)
  5. When the switch is pressed the third time, both LEDs turn off
  6. Repeat this same cycle of LEDs turning on and off in sequence (off, one LED, two LEDs, off…)

Document your work on the course website that you created. Deadline: Thursday, November 1, 13:00

Tips #

Check the tutorials on Digital Input from here

Solutions #

Solution #1

Solution 1 Breadboard

int ledPin1 = 10;
int ledPin2 = 9;
int btnPin = 2;
int btnState = 0;
int prevBtnState = 0;
int counter = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin, INPUT);

void loop() {
  // check the button
  btnState = digitalRead(btnPin);

  // check if the state is different from the previous loop
  if (btnState != prevBtnState) {
    if (btnState == HIGH) {
      // add one to the counter
      // check if the counter is too large
        counter = 0;
  // store the current button state for the next loop
  prevBtnState = btnState;

  switch (counter) {
    case 0:
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
    case 1:
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
    case 2:
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);

  // delay helps to prevent noise from the input signal
  // even better way to do this would be to implement debouncing:
  // https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce
Solution #2

This solution uses 3 LEDs and is a little bit more complicated since it uses arrays and for loops but it is also more flexible in a situation where you have more LEDs than just a couple. I made the video for the class in 2020 (covid year) when we had lots of restrictions in using the spaces.