Digital Electronics (USIU)

From Raspberry Pi Min-Grant project
Revision as of 22:18, 28 July 2015 by Snamuye (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Digital Electronics

Study of Logic Gates – AND, OR, NOT

In-Lab Exercise 1: Realizing a AND gate
  • Step 1  Connect the Circuit as shown in the image below

RTENOTITLE

  • Step 2  load the python code into your favorite code editor and copy paste the following code and save it as buttonpressed.py

In our case we used the command line and opened the nano editor and added the following code

#nano buttonpressed.py

import RPi.GPIO as GPIO

import time

#import random

GPIO.setmode(GPIO.BCM)

GPIO.setup(6,GPIO.IN,pull_up_down = GPIO.PUD_UP)

while True:

   input = GPIO.input(6)  

   if input == False:

       print ("Button Pressed")

       time.sleep(0.2)  


In-Lab Exercise 2: Realizing a OR gate
  • Step 1  Connect the Circuit as shown in the image below

RTENOTITLE

  • Step 2  load the python code into your favorite code editor and copy paste the following code and save it as LightsOn.py
  • In our case we used the command line and opened the nano editor and added the following code

#nano LightsOn.py

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

led = 4

GPIO.setup(led, GPIO.OUT)

GPIO.output(led,1)

time.sleep(0.5)

GPIO.output(led, 0)

time.sleep(0.05)


In-Lab Exercise 3: Realizing a NOT gate
  • Step 1  Connect the Circuit as shown in the image below

RTENOTITLE

  • Step 2  load the python code into your favorite code editor and copy paste the following code and save it as SwitchLightsOn.py

In our case we used the command line and opened the nano editor and added the following code

#nano SwitchLightsOn.py

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(6,GPIO.IN,pull_up_down = GPIO.PUD_UP)

led = 4

GPIO.setup(led, GPIO.OUT)

while True:

   input = GPIO.input(6)

   if input == False:

       print ("Button Pressed")

       time.sleep(0.2)

       GPIO.output(led,1)

       time.sleep(0.5)

       GPIO.output(led, 0)

       time.sleep(0.05)


In-Lab Experiment 1: Realizing AND gate

Wire up the circuit as shown in Figure

  1. Edit the Program shown in the following (python) code  below
    (Note: In python the spacing/indentation of the code is important to the code)

RTENOTITLE

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN, pull_up_down = GPIO.PUD_UP)

GPIO.setup(26, GPIO.IN, pull_up_down = GPIO.PUD_UP)

GPIO.setup(16, GPIO.OUT)
a = 0
b = 0

print "\n2 Input AND Gate"

print "\n"

print "A | B | AB"

print "----------"

print "1 | 1 | 1"

print "1 | 0 | 0"

print "0 | 1 | 0"

print "0 | 0 | 0"

print "\n"

 

while True:

    button1 = GPIO.input(6)

    button2 = GPIO.input(26)

    if button1 == False:

        a = 1

    if button2 == False:

       b = 1

    if (a == 1) and (b == 1):

       GPIO.output(16, 1)

    else:

        GPIO.output(16, 0)

    time.sleep(1)

    a = 0

    b = 0

 

  1. Run the Program and Observe LED when
  • Switch A is pressed
  • Switch B is pressed
  • When Both Switch A and B are pressed
  • Both Switches A and B are processed simultaneous