October 4, 2016

Executing my First GPIO in Raspberry Pi #5

Welcome Amigos' !!!!


As a quick replay use the first  3 steps in the last post and start a first program


>> mkdir myprograms
>> cd myprograms
>> sudo nano myfirst.py

while doing this, a window appears to let you type the program, but before that take a good look @ GPIO pin configuration



now, copy and paste these lines in the opened terminal window
#1.  this is the program to turn LED on and off  in 5 seconds interval

import RPi.GPIO as GPIO  # Import GPIO module
import time   # Import Time Module

GPIO.setmode(GPIO.BOARD)   # Use board pin numbering
GPIO.setup(7, GPIO.OUT)   # Setup GPIO Pin 7 to OUT

GPIO.output(7,True)  # Switch on pin 7
time.sleep(5)   # Wait
GPIO.output(7,False)   # Switch off pin 7
time.sleep(5)   # Wait


GPIO.cleanup()  


Once you have complete pasting or typing  Press "Cntrl + x" for exit and "Cntrl + y" for saving the file and give an "Enter" to finalize the process.

Before executing the program, connect a LED in the Pin 7  and there are several ground pins available such as Pin 6,9,14,20,25,30,34 & 30

#1.  How about turning LED on and off  in 5 seconds interval continuously


import RPi.GPIO as GPIO  # Import GPIO module
import time   # Import Time Module

GPIO.setmode(GPIO.BOARD)   # Use board pin numbering
GPIO.setup(7, GPIO.OUT)   # Setup GPIO Pin 7 to OUT

while(True):
    GPIO.output(7,True)  # Switch on pin 7
    time.sleep(5)   # Wait
    GPIO.output(7,False)   # Switch off pin 7
    time.sleep(5)   # Wait


GPIO.cleanup()  


tada.... just while loop is included....

Remember in other programming language such as C lang. after a looping statement a braces '{'  will be started and ended @ some place to execute more than one line in a loop. 
But in python you have to give a "TAB space" or "4 space bars" before each and every line to execute those lines in loop. (remember this may some time show you an error)

After saving this program file, try to run it using the below code

sudo python myfirst.py

# sudo - is used to run this program as super user

this will make the LED's to glow  or whatever connected to the respective pins

We will see the detailed view of programming in the upcoming python tutorial

see you
----

Arun

No comments:

Post a Comment