Learn

Good work getting to this point. We can now use what we’ve learned to light up a few LEDs!

Since each LED instance is attached to a single GPIO pin, we can now create multiple instances each with its own pin.

from gpiozero import LED from time import sleep led1 = LED(1) led2 = LED(2) led1.on() sleep(1) led1.off() led2.on() sleep(1) led2.off()

From the example above we can see that controlling two LEDs involves instantiating a new LED class with a different pin. If you try and instantiate with a pin already assigned you’ll receive an error.

The 2nd part of the code example turns the first LED on, then off - then the second LED is turned on, then off.

Be aware that turning on too many LEDs using a real Raspberry Pi can result in damage to the board. That is why we are just lighting up one at a time.

Now give this a try yourself!

Instructions

1.

Once again, start the emulator with by clicking the Run button.

You should see a 3 led circuit. Each LED is connected to its own GPIO pin and has its own 330Ω current limiting resistor.

The top LED is connected to GPIO14 The middle LED is connected to GPIO15 The bottom LED is connected to GPIO18

There is still only 1 ground wire coming from the GPIO, but that is because the wire uses the blue column on the right side of the board. This allows us to connect the negative terminal of each LED to the blue column using a short black wire.

2.

Create 3 variables:

  • led14 assigned an LED instance connected to GPIO14
  • led15 assigned an LED instance connected to GPIO15
  • led18 assigned an LED instance connected to GPIO18

Be sure to assign a separate GPIO pin to each instance.

3.

Using led14, turn on the top LED, pause for one second, then turn off the top LED.

4.

Now that you’ve blinked one led, blink the other 2 so they blink in order.

  • Turn led15 on, pause for one second, and then turn it off.
  • Turn led18 on, pause for one second, and then turn it off.

You do not need to pause in between turning one LED off and the next one on.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?