[Logo for The C Shore Landing Page]
The C Shore

LED On, LED Off

Table of Contents

LED On, LED Off

Hardware

  • Connect the long leg of the LED to GPIO 17, using the breadboard and a female to male jumper wire.
  • Connect the short leg of the LED to a resistor using the breadboard, and connect the other side of the resistor to GND (ground) on the Pi.

Picture of LED connected to GPIO using breadboard

  • When the Pi executes led.on() GPIO17 will become +3.3V (high) and electricity will flow from GPIO17 through the LED and resistor, and finally to GND (ground / 0 Volts).

Software

Once you have the LED and resistor connected, you should load 01-led-on-off.py using the Load button in Mu.

from gpiozero import LED            # Information the computer needs
from time import sleep              # More information the computer
                                    # needs

led = LED(17)                       # Use GPIO 17 for the LED

led.on()                            # Turn LED on and leave it on
sleep(1)                            # Wait for 1 second
led.off()                           # Turn LED off and leave it off

Questions

  • These are practice for what you will be doing with the robo-car code we will provide for you to use with your robo-car.

  • You will need to modify the robo-car code using the techniques you learn here.

  • Where do you tell the computer to use GPIO 17?

  • How do you turn the LED on?

  • What does sleep(1) do?

  • How long does the LED stay on, and why?

  • How do you turn the LED off?


Exercises

  • Change the amount of time the LED stays on.

Another Question

  • How would make the LED turn on and off a second time?

For the exercises below you probably don't want to type the answers, so we're going to show you how to make a copy of the code you need. You should have a handout titled ‘Avoid Typing: Cut & Paste Handout’. We've done this so you can refer to it as needed.

Let's walk through using cut & paste to do the exercise below. Don't worry, this gets easier with practice.

More Exercises

Remember: This should be your partner's turn to be pilot.

  • Turn the LED on and off a second time.

Repeat the Exercise With the Other Partner Piloting

  • Turn the LED on and off a second time.

Because the last exercise was a repeat we won't swap until after this next exercise.

Another Question

  • How would make the LED turn on and off a third time?

Another Exercise

  • Turn the LED on and off a third time.

And Another Question

  • How would you turn the LED on and not turn it off?

More Exercises

Please swap the hot seat

  • Turn the LED on and leave it turned on.

And Another Exercise

And another swap

  • Turn the LED on for 1 second, turn if off, then turn it on for 2 seconds, and turn it off.