Skip to content

Reading the value of a GPIO pin works, but onFalling, etc. fail to trigger #141

@bottlehall

Description

@bottlehall

Board Type

RaspberryPi3 Model B

Operating System

Ubuntu 24.04.1 LTS

Swift Version

Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: aarch64-unknown-linux-gnu

Installed from swiftARM as binary

Description

A KY-038 sound sensor's digital output (triggered when a threshold is reached) is connected to GPIO pin 23. The board can run with 3.3-5V supply. I am running it at 5V without pull-up resistors.

I have cobbled this code from several sources. It executes without error but none of the closures are triggered.

import Foundation
import SwiftyGPIO

let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPi3)
var gp = gpios[.P23]!

gp.direction = .IN
gp.bounceTime = 0.5

gp.onRaising{
    gpio in
    print("Transition to 1, current value:" + String(gpio.value))
}

gp.onFalling{
    gpio in
    print("Transition to 0, current value:" + String(gpio.value))
}

gp.onChange{
    gpio in
    gpio.clearListeners()
    print("The value changed, current value:" + String(gpio.value))
}

RunLoop.main.run()

If I replace the RunLoop.main.run() with:

while true {
    print("GPIO.value=\(gp.value)")
}

Then I can see the value changing from 0 to 1 appropriately. I have tried changing .pull but this has no effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions