In the previous lesson, you leaned about building a LED circuit and controlling it from your python code. We even made the LED blink.

Python Functions

You can build your own functions and call it from multiple places. Breaking down into functions, improve the readability of your code and minimises code duplication. A function can return value as well.

Now let us rewrite the LED python program with functions. Here is the full code.

import RPi.GPIO as GPIO
import time
led_pin=40
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led_pin, GPIO.OUT)
def blink_led(led_pin):
 GPIO.output(led_pin, GPIO.HIGH)
 time.sleep(0.25)
 GPIO.output(led_pin, GPIO.LOW)
 time.sleep(0.25)

for i in range(1,20):
 blink_led(led_pin)

GPIO.cleanup()

Programming Phases

Requirement

Requirement tells what your program has to do. With out going into too much details, understand the problem that you are trying to solve. Clarify and make sure your program will not miss any important feature.

Example Requirement: Write a program that will blink an LED. When user presses Ctrl C, terminate the program gracefully.

Design

You draw a flow chart, circuit diagram or write the step by step instructions in a notebook. At the design phase, you think about more implementation details. What are all the inputs? Are there any configuration parameters? What will be output? You write down everything in plain English and review with user or the person who gave you the requirement.

Example LED Blinking Design
Output pin to use 40.
Write a function blink_led that will set the output pin high for 0.25 seconds and set the output pin low for 0.25 seconds.
Repeat blink_led function 20 times
cleanup

Code

Write your code in python.

Test

Test your program, by giving several different inputs and running it many times and fix any bugs you find.

Challenges

https://w3cloud.com/academy/raspberry-pi-python-challenges/

Push button start

Your Raspbery PI 3 has 40 GPIO pins. Some of these pins are not for connecting inputs and outputs. Only, those pins that are numbered (like 4,17,27…) can be connected to a input / output circuit.

In this project, when the button switch is pressed, we will play an MP3 clip (Car starting sound clip).

Here is how you connect a switch. One end to the 3.3 volt and another end to a GPIO pin of the raspberry pi 3. When the button is pressed, the input pin will have a high voltage, and the GPIO library will call your callback function. With Raspberry Pi 3, you do not need a pull-down resistor.

 

#This program blinks an LED 20 times
import RPi.GPIO as GPIO 
import time
import os 

input_pin=37 

GPIO.setmode(GPIO.BOARD) 
GPIO.setup(input_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def switchPressed(pin):
 if GPIO.input(input_pin):
   print("Playing sound")
   os.system('omxplayer /home/pi/projects/sound/carstart.mp3') 

GPIO.add_event_detect(input_pin, GPIO.RISING, callback=switchPressed,bouncetime=300)
try:
 while True:
  time.sleep(1)
except KeyboardInterrupt:
 print("goodbye")
 GPIO.cleanup()

LED Blinks when the switch is pressed

Now, let us put the previous two techniques that we learned together. When the switch is pressed, blink the LED. Turn off the LED when the switch is not pressed.

 

#This program blinks an LED when the switch is pressed
#Author Karthik@W3Cloud
import RPi.GPIO as GPIO
import time
import os

input_pin=37
led_pin=40
led_on=False

GPIO.setmode(GPIO.BOARD)
GPIO.setup(input_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led_pin, GPIO.OUT)
def blink_led(led_pin):
 GPIO.output(led_pin, GPIO.HIGH)
 time.sleep(0.25)
 GPIO.output(led_pin, GPIO.LOW)
 time.sleep(0.25)

def switch_pressed(pin):
 global led_on
 if GPIO.input(input_pin):
 print("Turning on LED")
 led_on=True
 else:
 print("Turning off LED")
 led_on=False

GPIO.add_event_detect(input_pin, GPIO.BOTH, callback=switch_pressed)
try:
 while True:
 if led_on:
 blink_led(led_pin)
 else:
 time.sleep(1)
except KeyboardInterrupt:
 print("goodbye")
 GPIO.cleanup()

Motion Activated Blinking LED

Motion sensor that you use in this project is HC-SR501. This project is almost similar to the previous switch project, except we connect the motion sensor. The same python code that you used for the previous project, will work with the new circuit as well.

The motion sensor HC-SR-501 has 3 pins, VCC, out and ground. Connect the VCC to 5V horizontal line your bread board. Connect the GND pin to the ground of you bread board. Connect the out pin to the pin 37 input pin as shown in the circuit diagram.