Exercises with GPIOs as Outputs

Example 2.1.a using if-elif
Make the following connections (see picture):

Connect PIN 25 (GROUND) of the Raspberry Pi to the GND of the experiment board.
Connect PIN 22 of the Raspberry Pi to LED 0 of the Lower Monitor on the experiment board.

Then, run the following program (file Example 2.1.A):

import RPi.GPIO as GPIO # Import library for GPIO (pin) access
GPIO.setmode(GPIO.BOARD) # Specify that we use the BOARD numbers (pin numbers 1-40 on the connector)
GPIO.setwarnings(False) # Disable the warning “This channel is already in use”

# ******** PIN CONFIGURATION *************************************
GPIO.setup(22, GPIO.OUT) # Configure pin 22 as an output (OUT)
# ****************************************************************

GPIO.output(22, 0) # Turn off the LED when the program starts

# It could be on if a previous program left it on

while True:

x = input(“Enter on or off: “)
if x == ‘on’:

GPIO.output(22, 1)

elif x == ‘off’:

GPIO.output(22, 0)

The program asks the user to enter on or off and turns the LED on or off consequently (video). Observe that if the user enters a string different from “on” or “off” the program does not generate any error message but simply asks the user again to “Enter on or off: “. Instead in the next Example 2.1.b (video) the program prints the error message: “You must enter either on or off”.

Example 2.1.busing if-elif-else
Here an if-elif-else structure is used (file Example 2.1.B):

import RPi.GPIO as GPIO # Import library for GPIO (pin) access
GPIO.setmode(GPIO.BOARD) # Specify that we use the BOARD numbers (pin numbers 1-40 on the connector)
GPIO.setwarnings(False) # Disable the warning “This channel is already in use”

# ******** PIN CONFIGURATION *************************************
GPIO.setup(22, GPIO.OUT) # Configure pin 22 as an output (OUT)
# ****************************************************************

GPIO.output(22, 0) # Turn off the LED when the program starts
# It could be on if a previous program left it on

while True:

x = input(“Enter on or off: “)
if x == ‘on’:

GPIO.output(22, 1)

elif x == ‘off’:

GPIO.output(22, 0)

else:

print(‘ERROR: You must enter either on or off!‘)

The program now generates a message if the user enters a string that is different from “on” or “off” as shown in this video.

Assignment 2.A
Connect LED 2, LED 1 and LED 0 to pins 26, 24 and 22 respectively (picture) and connect pin 25 (GROUND) to GND. Write a program that implements the following (video):

  • Print “Which LED should be on?“, “Enter L0, L1, L2 or off:
  • If the user enters L0, then L0 is turned on and the other two LEDs are off.
  • If the user enters L1, then L1 is turned on and the other two LEDs are off.
  • If the user enters L2, then L2 is turned on and the other two LEDs are off.
  • If the user enters off, then all the LEDs are off.

The program should be implemented as a loop so that after the user enters L1, L2 or L3 the program should ask the question again “Which LED should be on?“, “Enter L0, L1, L2 or off:” etc.

You can do this program in two ways: either using if-elif or using an if-elif-else. If you use if-elif-else you can print a message in case the user enters a string different from “L1”, “L2”, “L3” or “off”. For instance you can print the message “ERROR: You must enter L0, L1, L2 or off!“.

Assignment 2.B
Modify assignment 2.A. This time the program should implement the following (video):

  • Print “How many LEDs should be on?”, “Enter one, two, three or none:”
  • If the user enters one, only L0 should be on.
  • If the user enters two, L0 and L1 should be on.
  • If the user enters three, all three LEDs should be on.
  • If the user enters none, all the three LEDs should be off.

You can do this program in two ways: either using if-elif or using an if-elif-else. If you use if-elif-else you can print a message in case the user enters a string different from “one”, “two”, “three” or “none”. For instance you can print the message “You must enter one, two, three or none!“.

Assignment 2.C
Modify assignment 2.B. This time the program should implement the following (video):
Print “How many LEDs should be on?“, “Enter 1, 2, 3 or 0:
If the user enters 1, only L0 should be on.
If the user enters 2, L0 and L1 should be on.
If the user enters 3, all three LEDs should be on.
If the user enters 0, all the three LEDs should be off.

Discussion
The program of assignment 2.C is very similar to the one of assignment 2.B. But 2.C can be done in two different ways, depending on whether you choose to work with strings or you convert the input string to an integer number.

Assignment 2.D
This program uses only one LED (see picture).
Connect PIN 25 (GROUND) of the Raspberry Pi to the GND of the experiment board.
Connect PIN 22 of the Raspberry Pi to LED 0 of the Lower Monitor on the experiment board.
The program asks the user “How many times should the LED blink?“. Then the the program makes the LED blink a corresponding number of times and then terminates. See the video.

Assignment 2.E
Modify the program of the previous assignment. This time the program should not terminate after making the LED blink. Use an infinite loop to repeat the program indefinitely. See the video.