How to Interrupts Programming In Raspberry pi

 You want to respond to some event, such as a button push, without having to continually poll the input pin to see if its state has changed. Today I show you How to Interrupts Programming In RAspberry pi

Required Component connect  LED Raspberry Pi :

1.Raspberry pi 
 
 
 
 
 

This book will help you to gain more knowledge of Raspberry pi  Software and Hardware Problems and Solutions

Raspberry Pi Cookbook

Circuit diagram Interrupts Programming Raspberry pi:

shows how you can wire this Button Switch using a solderless breadboard and male-to-female jumper leads
  push button raspberry pi

Code Interrupts Programming Raspberry pi:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
def my_callback(channel):
 print('You pressed the button')
 GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 GPIO.add_event_detect(18, GPIO.FALLING, callback=my_callback)
i = 0
while True:

 i = i + 1
 print(i)
 time.sleep(1)
When you Button Push you can see This 1 2 3 You pressed the button 4 You could detect when a button has been pressed or a GPIO input has changed by simply checking repeatedly in a loop; for example:
while True:
if GPIO.input(18) == False:
# put the code to be actioned here
time.sleep(0.1)
The disadvantage here is that you can’t do much else while you are checking for button presses. A second disadvantage is that if the button the press is very quick, it could come and go before you can register it with the GPIO.input. They allow you to associate a function with one of the pins so that when the voltage at the input changes either from low to high or vice versa, you can trigger the function to be run. You can see how this works in the preceding example program. First, define a function called my_callback that takes a single argument. This argument specifies the input that triggered the interrupt, allowing you to use the same handler function for a number of interrupts.
def my_callback(channel):
 print('You pressed the button')
In this case, the callback function just displays a message. The line of code that does the actual linking is:
GPIO.add_event_detect(24, GPIO.FALLING, callback=my_callback)
The first parameter specifies the pin (24). If this is set to FALLING, the function will only be called if the GPIO pin goes from high to low. This is the case in this example, as the switch pulls the input low against the internal pull-up resistor. If, on the other hand, the second argument is set to RISING, the function will only be called when the input goes from low to high (when the switch is released). The event handler function does not stop the main counting loop while it runs; it actually runs in its own separate thread of execution. Switches often bounce when pressed. This means they don’t always transition cleanly from open to closed but bounce between the two, possibly several times, making it appear that the button was pressed multiple times in very rapid succession when actually it was pressed only once. If you keep pressing the button, you’ll probably see this reflected in the output as the message appearing more than once for one button press. The library actually has an option to stop bounce from being a problem, by preventing the interrupt from being triggered again within a certain amount of time. To make use of this feature, just add the extra optional parameter bounce time to the add_event_detect call.
GPIO.add_event_detect(18, GPIO.FALLING, callback=my_callback, bouncetime=100)

If you want to know more about raspberry pi then click on the link below

RASPBERRY PI TUTORIALS FOR BEGINNERS

No comments

Theme images by Dizzo. Powered by Blogger.