Professional Documents
Culture Documents
Regardless of the Processor in Being Used: HC12, PIC, Raspberry Pi, Arduino , Photon
if GPIO.input(channel):
print('Input was HIGH')
else:
print('Input was LOW')
#OR
while (GPIO.input(channel)):
print('Input was HIGH')
The above two statements have disadvantage. The program will loop until the
condition is met. This will tied the CPU until the condition is met.
To avoid such infinite loop, you can use a wait_for_edge function. This function is
predefined in the GPIO library. The function polls the associated pin and keeps
waiting for it to change its status either with a rising or a falling edge or both.
Moreover using this function the user can specify a timeout parameter to exit the wait
loop if the edge didn’t occur with the specify timeout. This timeout feature gives an
advantage over a normal while loop.
The syntax for such function is as follow:
GPIO.wait_for_edge (18, GPIO.FALLING, timeout=1000)
#where 18 is the input pin number, edge on such pin can be RISING, FALLING or
BOTH and the timeout is the time to exit the wait if the edge did not occur in
milliseconds.
Using such functions you will be wasting you processor time and resources hence the
need for interrupts arise.
GPIO Interrupts
Each GPIO pin, when configured as a general-purpose input, can be configured as an
interrupt driven event. Interrupt can be activated as follows:
•Synchronous Rising/Falling edge:
The normal rising/falling edge detection has a small amount of synchronization
built into the detection. Every system clock cycle, the pin is sampled with the
criteria for generation of an interrupt being a stable transition within a 3-cycle
window, i.e. a record of "1 0 0" for falling or "0 1 1“ for rising.
•Asynchronous Rising/Falling edge:
Asynchronous detection bypasses this synchronization to enable the detection
of very narrow events.
Once an interrupt edge is detected, The CPU will start executing the related interrupt
service routine (function).
Asyn. Asyn.
Falling Edge Rising Edge
R = Power
W = Ground
B = Output
Phototransistor IR LED
Lab Work: