Single bulb flashing with Pi:

Bulb will be flashed for every 2 seconds after running the following code.

Circuit:

fullsizerender-11

Program:
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
state = True
while True:
 GPIO.output(11,True)
 time.sleep(2)
 GPIO.output(11,False)
 time.sleep(2)

This slideshow requires JavaScript.

Traffic lights demonstration using Pi:

The lights in the following circuit is lights are controlled by the program.

On input “R”-> Red light glows

On input “Y”-> Yellow light glows

On input “G”-> Green light glows

Circuit:

fullsizerender-12

Program:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.output(11,False)
GPIO.output(12,False)
GPIO.output(13,False)
state=True
def green():
 GPIO.output(12,False)
 GPIO.output(13,False)
 GPIO.setup(11,GPIO.OUT)
 GPIO.output(11,True)
def yellow():
 GPIO.output(11,False)
 GPIO.output(13,False)
 GPIO.setup(12,GPIO.OUT)
 GPIO.output(12,True)
def red():
 GPIO.output(12,False)
 GPIO.output(11,False)
 GPIO.setup(13,GPIO.OUT)
 GPIO.output(13,True)
def light(a):
 if a==’r’:
  red()
 elif a==’g’:
  green()
 elif a==’y’:
  yellow()
else:
 print(“r->red,g->green,y->yellow”)
while True:
 a=raw_input(“r->red,g->green,y->yellow”)
 light(a)

 

 

 

 

Flashing LED on button press:

On pressing the button light will be flashing infinatle

Program:

import  RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(40,GPIO.OUT)
while True:
    if (GPIO.input(7)==1):
      while True:
        print(“Pressed on”)
        GPIO.output(40,True)
        time.sleep(2)
        GPIO.output(40,False)
        time.sleep(2)

    GPIO.cleanup()

 

Multiple light blink on a button press:

On pressing the button program will make lights to glow in the sequence given like traffic signals.

  1. Red light for 2 seconds
  2. Flashing yellow thrice
  3. Green light for another 2 seconds and the loop continues in this way.

Circuit:

fullsizerender-17

 

Program:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(38, GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(37,GPIO.OUT)
GPIO.setup(36,GPIO.OUT)
GPIO.setup(40,GPIO.OUT)
while True:
if (GPIO.input(7)==1):

while True:
GPIO.output(37,True)
time.sleep(3)
GPIO.output(37,False)
i=0
while i<3:
GPIO.output(40,True)
time.sleep(0.2)
GPIO.output(40,False)
i+=1
GPIO.output(36,True)
time.sleep(3)
GPIO.output(36,False)

GPIO.output(36,False)
GPIO.output(37,False)
GPIO.output(40,False)

This slideshow requires JavaScript.

Class interaction:

  1. Aneesh had a doubt regarding his circuit. So, I tried to help him out. There is a problem in assumption regarding pushbutton internal circuit.
  2. Lavanya had a doubt regarding program and circuit. She was unable to catch button press in the right loop.

References:

http://razzpisampler.oreilly.com/ch07.html

http://raspberrywebserver.com/gpio/detecting-a-button-press-through-GPIO.html

Past class work on button input for arduino.

 

 

 

Advertisements