Saturday, 12 May 2018

Digital Input to turn on led.


Hello Folks…

It’s been a while ever since I have done the first GPIO test on my Raspberry Pi 2. Now it’s the time to proceed with this again. Raspberry Pi 2 with linux-based operating system has made me scratch my head initially. Thanks god ! I have found many resources which I can learn the python script with a quick manner. 

As I have learned and familiarized with Arduino IDE, the basic grasp of Arduino’s code helped me to progress too.

In this part, I would like to share the some of the details of using digital pins in Raspberry Pi 2.

The components used are:

1. Raspberry Pi 2


2. A red LED


3. 220-ohm resistor


4.Jumper wires


5.Momentarily push button.


6.10k-ohms resistor



Let’s setup the circuit which I had drawn by using Fritzing:


In the script:

When I pressed on the button momentarily, it will turn on; meanwhile it will turn off after the button is not pressed. 

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(16, GPIO.IN)

while True:
        readButton = GPIO.input(26)
        if readButton == True:
                GPIO.output(26, 1)
                time.sleep(0.01)
        else if readButton == False:
                GPIO.output(26, 0)
                time.sleep(0.01)


How about a latching switch?

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmodel(GPIO.BCM)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(16, GPIO.IN)

while True:
        val == GPIO.input(16)
        if val == True and oldval == False:
                state = not state
                time.sleep(0.001)
                oldval = val
        if state == 1:
                GPIO.output(26, 1)
                Print’ON’
        else:
                GPIO.output(26, 0)
                Print’OFF’
               

That’s all ? 

Stay tune in my next blog.

Bye !




No comments:

Post a Comment