Using a potentiometer to turn LEDs on and off

This lab consists in using a potentiometer to control three LEDs. According to the potentiometer value, a specific LED is turned on or off. The main loop reads the potentiometer through the ADC, switching more LEDs on as its value increases or switching more LEDs off as its value decreases.

Pseudo code

Let's begin with a pseudo code (actual coding is up to you):

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
int main() {
    while(1) {
        value = adc->read();
        if(value < 10%) {
            led1->off();
            led2->off();
            led3->off();
        } else if(value > 10% && < value 30%) {
            led1->on();
            led2->off();
            led3->off();
        } else if(value > 30% && < value 80%) {
            led1->on();
            led2->on();
            led3->off();
        } else {
            led1->on();
            led2->on();
            led3->on();
        }
        delay(500); //ms
    }
    return 0;
}

The pseudo-code is self-explaining: an infinite loop reading the ADC channel to which the potentiometer has been connected and a composed conditional to switch on/off the LEDs through the respective GPIO ports. The delay at the end of the loop is a good practice in terms of energy, since a tight loop, checking the potentiometer faster then it actually can vary would be useless. A well implemented delay could put the MCU in a low-power mode while keeping a timer counting.

Schematic

Schematic

Part List

For this lab, you'll need the following components:

Assembly

Schematic

Solutions

Tools