You are on page 1of 31

LAB ACTIVITY #11.

The Blinky Circuit Design

SOURCE CODE:

import time
import RPi.GPIO as gpio

gpio.setmode(gpio.BCM)
gpio.setup(20, gpio.OUT)

while True:
gpio.output(20, True)
time.sleep(0.25)
gpio.output(20, False)
time.sleep(0.25)

CIRCUIT DIAGRAM:

SIMULATION RUN:

https://drive.google.com/file/d/1XrYOSXydR3SlvyuRK6b1IcUzE9pp7ho4/view?
usp=sharing
LAB ACTIVITY #11.2

Alternate Blinking LEDs Circuit Design

SOURCE CODE:

import time
import RPi.GPIO as gpio

gpio.setmode(gpio.BCM)
gpio.setup(20, gpio.OUT)
gpio.setup(18, gpio.OUT)

while True:
gpio.output(20, True)
time.sleep(0.25)
gpio.output(20, False)
time.sleep(0.25)

gpio.output(18, True)
time.sleep(0.25)
gpio.output(18, False)
time.sleep(0.25)

CIRCUIT DIAGRAM:

SIMULATION RUN:
https://drive.google.com/file/d/1pdqcV501GCq62MkLPmqfHEYy9nAATnR9/view?
usp=share_link
LAB ACTIVITY #11.3

On Your Own: MCU Activity #1 & #2 are variations in the previous LED circuit

application design (as in Arduino Lesson)

SOURCE CODE:

from goto import *


import time
import RPi.GPIO as gpio

LED = [25, 24, 23, 22, 27, 18, 17, 4, 6, 12, 13, 16, 19, 20, 26, 21]
LEDindex = 15

def peripheral_setup () :

gpio.setmode(gpio.BCM)
for i in range(LEDindex+1):
gpio.setup(LED[i], gpio.OUT)

pass

def peripheral_loop () :
for i in range(int(LEDindex/2)+1):
gpio.output(LED[i], True)
gpio.output(LED[LEDindex-i], True)
time.sleep(0.1)
gpio.output(LED[i], False)
gpio.output(LED[LEDindex-i], False)

pass

def main ():


peripheral_setup()
while True :
peripheral_loop()
pass

if __name__ == '__main__' :
main()
CIRCUIT DIAGRAM:

SIMULATION RUN:

https://drive.google.com/file/d/1pdqcV501GCq62MkLPmqfHEYy9nAATnR9/view?

usp=share_link
LAB ACTIVITY #11.4

Interfacing 7-Segment Display with RPi

SOURCE CODE:

from goto import *


import time
import RPi.GPIO as IO

DISPLAY = [0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F]

def PORT(pin): # assigning GPIO logic by taking 'pin' value


if(pin&0x01 == 0x01):
IO.output(21,1) # if bit0 of 8bit 'pin' is true, pull PIN13 high
else:
IO.output(21,0) # if bit0 of 8bit 'pin' is false, pull PIN13 low

if(pin&0x02 == 0x02):
IO.output(26,1) # if bit1 of 8bit 'pin' is true, pull PIN6 high
else:
IO.output(26,0) # if bit1 of 8bit 'pin' is false, pull PIN6 low

if(pin&0x04 == 0x04):
IO.output(20,1)
else:
IO.output(20,0)

if(pin&0x08 == 0x08):
IO.output(19,1)
else:
IO.output(19,0)

if(pin&0x10 == 0x10):
IO.output(16,1)
else:
IO.output(16,0)

if(pin&0x20 == 0x20):
IO.output(13,1)
else:
IO.output(13,0)

if(pin&0x40 == 0x40):
IO.output(12,1)
else:
IO.output(12,0)
def peripheral_setup () :
IO.setwarnings(False)
IO.setmode (IO.BCM)

IO.setup(21,IO.OUT)
IO.setup(26,IO.OUT)
IO.setup(20,IO.OUT)
IO.setup(19,IO.OUT)
IO.setup(16,IO.OUT)
IO.setup(13,IO.OUT)
IO.setup(12,IO.OUT)

pass

def peripheral_loop () :
for x in range(10): # run the loop 10X incrementing x value from 0 - 9
pin = DISPLAY[x] # assigning value to 'pin' for each digit
PORT(pin); # showing each digit on display
time.sleep(1)
pass

def main ():


peripheral_setup()
while 1 :
peripheral_loop()
pass

if __name__ == '__main__' :
main()
CIRCUIT DIAGRAM:

SIMULATION RUN:

https://drive.google.com/file/d/1O1V4B7HTmPsYrFiq9Ms7nfX3z4c7iOF3/view?

usp=share_link
LAB ACTIVITY #11.5

Interfacing 16x2 Liquid Crystal Display with RPi

SOURCE CODE:

from goto import *


import time
import RPi.GPIO as GPIO

# Define GPIO to LCD mapping


LCD_RS = 4
LCD_E = 17
LCD_D4 = 18
LCD_D5 = 27
LCD_D6 = 22
LCD_D7 = 23

# Define some device constants


LCD_WIDTH = 16 # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

def lcd_init():
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)
def lcd_byte(bits, mode):
GPIO.output(LCD_RS, mode) # RS

# High bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)

if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin


lcd_toggle_enable()
# Low bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)

if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin


lcd_toggle_enable()

def lcd_toggle_enable():
# Toggle enable
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)

def lcd_string(message,line):
# Send string to display
message = message.ljust(LCD_WIDTH," ")
lcd_byte(line, LCD_CMD)
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)

def peripheral_setup () :
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7

# Initialise display
lcd_init()
pass

def peripheral_loop () :
# Send some test
lcd_string("Rasbperry Pi",LCD_LINE_1)
lcd_string("16x2 LCD Test",LCD_LINE_2)
time.sleep(2) # 2 second delay
# Send some text
lcd_string("MABUHAY!",LCD_LINE_1)
lcd_string("URS Giants",LCD_LINE_2)
time.sleep(2)
pass

def main ():


peripheral_setup()
while 1 :
peripheral_loop()
pass

if __name__ == '__main__' :
main()
CIRCUIT DIAGRAM:

SIMULATION RUN:

https://drive.google.com/file/d/1rw1q43Sur50IMQMBCYysH36HFHh_XHv4/view?

usp=share_link
ASSIGNMENT #11

SOURCE CODE:

from goto import *


import time
import RPi.GPIO as IO

A = 21
B = 26
C = 20
D = 19
E = 16
F = 13
G = 12

rowPins = [27, 22, 23, 24]


columnPins = [18, 17, 4]

input = "0"

DISPLAY = [0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F]

def letterH():
IO.output(A,0)
IO.output(B,1)
IO.output(C,1)
IO.output(D,0)
IO.output(E,1)
IO.output(F,1)
IO.output(G,1)

def letterA():
IO.output(A,1)
IO.output(B,1)
IO.output(C,1)
IO.output(D,0)
IO.output(E,1)
IO.output(F,1)
IO.output(G,1)

def PORT(pin):
if(pin&0x01 == 0x01):
IO.output(A,1)
else:
IO.output(A,0)

if(pin&0x02 == 0x02):
IO.output(B,1)
else:
IO.output(B,0)

if(pin&0x04 == 0x04):
IO.output(C,1)
else:
IO.output(C,0)

if(pin&0x08 == 0x08):
IO.output(D,1)
else:
IO.output(D,0)

if(pin&0x10 == 0x10):
IO.output(E,1)
else:
IO.output(E,0)

if(pin&0x20 == 0x20):
IO.output(F,1)
else:
IO.output(F,0)

if(pin&0x40 == 0x40):
IO.output(G,1)
else:
IO.output(G,0)

def readLine(line, characters):


global input
IO.output(line, IO.HIGH)
time.sleep(0.02)
if(IO.input(columnPins[0]) == 1):
if (characters[0] == "*"):
letterA()
else:
input = characters[0]
pin = DISPLAY[int(input)]
PORT(pin);
elif(IO.input(columnPins[1]) == 1):
input = characters[1]
pin = DISPLAY[int(input)]
PORT(pin);
elif(IO.input(columnPins[2]) == 1):
if (characters[2] == "#"):
letterH()
else:
input = characters[2]
pin = DISPLAY[int(input)]
PORT(pin);

IO.output(line, IO.LOW)

def peripheral_setup () :
IO.setwarnings(False)
IO.setmode (IO.BCM)

IO.setup(A,IO.OUT)
IO.setup(B,IO.OUT)
IO.setup(C,IO.OUT)
IO.setup(D,IO.OUT)
IO.setup(E,IO.OUT)
IO.setup(F,IO.OUT)
IO.setup(G,IO.OUT)

for i in range(len(rowPins)):
IO.setup(rowPins[i], IO.OUT)

for i in range(len(columnPins)):
IO.setup(columnPins[i], IO.IN, pull_up_down=IO.PUD_DOWN)

pass

def peripheral_loop () :
readLine(rowPins[0], ["1","2","3"])
readLine(rowPins[1], ["4","5","6"])
readLine(rowPins[2], ["7","8","9"])
readLine(rowPins[3], ["*","0","#"])

pass

def main ():


peripheral_setup()
while 1 :
peripheral_loop()
pass

if __name__ == '__main__' :
main()
CIRCUIT DIAGRAM:

SIMULATION RUN:

https://drive.google.com/file/d/1bCU2z19L1rvrqHj213AgERKWaHZmQcYH/view?

usp=share_link
CHALLENGE #11

SOURCE CODE:

from goto import *


import time
import RPi.GPIO as GPIO

LEDgreen = 20
LEDyellow = 26

LCD_RS = 19
LCD_E = 16
LCD_D4 = 13
LCD_D5 = 12
LCD_D6 = 6
LCD_D7 = 5

LCD_WIDTH = 16
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0

E_PULSE = 0.0005
E_DELAY = 0.0005

rowPins = [27, 22, 23, 24]


columnPins = [18, 17, 4]
input = ""
enter = False

def lcd_init():
lcd_byte(0x33,LCD_CMD)
lcd_byte(0x32,LCD_CMD)
lcd_byte(0x06,LCD_CMD)
lcd_byte(0x0C,LCD_CMD)
lcd_byte(0x28,LCD_CMD)
lcd_byte(0x01,LCD_CMD)
time.sleep(E_DELAY)

def lcd_byte(bits, mode):


GPIO.output(LCD_RS, mode)

GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)

if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)

lcd_toggle_enable()
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)

if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)

lcd_toggle_enable()

def lcd_toggle_enable():
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)

def lcd_string(message,line):
message = message.ljust(LCD_WIDTH," ")
lcd_byte(line, LCD_CMD)
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
def readLine(line, characters):
global input, enter
GPIO.output(line, GPIO.HIGH)
time.sleep(0.02)
if(GPIO.input(columnPins[0]) == 1):
if (characters[0] == "*"):
input = ""
enter = False
else:
input += characters[0]
elif(GPIO.input(columnPins[1]) == 1):
input += characters[1]
elif(GPIO.input(columnPins[2]) == 1):
if (characters[2] == "#"):
enter = True
else:
input += characters[2]

GPIO.output(line, GPIO.LOW)

def peripheral_setup () :
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BCM)

GPIO.setup(LCD_E, GPIO.OUT)
GPIO.setup(LCD_RS, GPIO.OUT)
GPIO.setup(LCD_D4, GPIO.OUT)
GPIO.setup(LCD_D5, GPIO.OUT)
GPIO.setup(LCD_D6, GPIO.OUT)
GPIO.setup(LCD_D7, GPIO.OUT)
GPIO.setup(LEDgreen, GPIO.OUT)
GPIO.setup(LEDyellow, GPIO.OUT)

for i in range(len(rowPins)):
GPIO.setup(rowPins[i], GPIO.OUT)

for i in range(len(columnPins)):
GPIO.setup(columnPins[i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

lcd_init()
pass

def peripheral_loop () :
readLine(rowPins[0], ["1","2","3"])
readLine(rowPins[1], ["4","5","6"])
readLine(rowPins[2], ["7","8","9"])
readLine(rowPins[3], ["*","0","#"])

if(enter):
lcd_string("Input: " + input, LCD_LINE_1)
if(input == "1937"):
lcd_string("Code CORRECT", LCD_LINE_2)
GPIO.output(LEDgreen, True)
time.sleep(1)
GPIO.output(LEDgreen, False)
else:
lcd_string("Code INCORRECT", LCD_LINE_2)
GPIO.output(LEDyellow, True)
time.sleep(2)
GPIO.output(LEDyellow, False)
else:
lcd_string("Enter code:", LCD_LINE_1)
lcd_string(input, LCD_LINE_2)

pass

def main ():


peripheral_setup()
while 1 :
peripheral_loop()
pass

if __name__ == '__main__' :
main()
CIRCUIT DIAGRAM:

SIMULATION RUN:

https://drive.google.com/file/d/1foqzsS7fA-BuchiTaPFnRsbzNPRpBitp/view?

usp=share_link

You might also like