Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4792

Troubleshooting • GPIO interrupt

$
0
0
I am writing the gpio interrupt code.I dont know how it is work on Raspberry Pi5. I am configuring GPIO 18 pn as gpio input pin in config.txt file

Code:

import gpiodimport time# ConstantsCHIP_NAME = 'gpiochip4'PIN_NUMBER = 18# Callback function to handle the interruptdef gpio_callback(event):    if event.event_type == gpiod.LineEvent.RISING_EDGE:        edge = "RISING"            elif event.event_type == gpiod.LineEvent.FALLING_EDGE:        edge = "FALLING"    else:        edge = "UNKNOWN"    print(f"Interrupt received! Edge: {edge}")        value = event.source.get_value()    print(f"Interrupt received! Edge: {edge}, Value: {value}")# Initialize the GPIO chip and linechip = gpiod.Chip(CHIP_NAME)line = chip.get_line(PIN_NUMBER)# Configure the line for input with an event on both rising and falling edgesline.request(consumer='gpio_interrupt', type=gpiod.LINE_REQ_EV_RISING_EDGE)print(f"Listening for interrupts on GPIO pin {PIN_NUMBER}...")# Attach the callback function to the lineline.set_event_handler(gpio_callback)try:    while True:        time.sleep(1)  # Sleep to keep the script runningexcept KeyboardInterrupt:    passfinally:    line.release()    chip.close()

Statistics: Posted by Fwpriya — Wed Jul 10, 2024 5:07 am



Viewing all articles
Browse latest Browse all 4792

Trending Articles