https://github.com/pi4j/pi4j-kotlin
Kotlin DSL for Pi4J V2
https://github.com/pi4j/pi4j-kotlin
dsl gpio kotlin kotlin-backend kotlin-bindings kotlin-library pi4j pi4k raspberry-pi raspberry-pi-gpio
Last synced: about 1 year ago
JSON representation
Kotlin DSL for Pi4J V2
- Host: GitHub
- URL: https://github.com/pi4j/pi4j-kotlin
- Owner: Pi4J
- License: apache-2.0
- Created: 2022-09-12T15:04:05.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-25T13:10:02.000Z (almost 2 years ago)
- Last Synced: 2025-03-26T13:21:18.435Z (over 1 year ago)
- Topics: dsl, gpio, kotlin, kotlin-backend, kotlin-bindings, kotlin-library, pi4j, pi4k, raspberry-pi, raspberry-pi-gpio
- Language: Kotlin
- Homepage: https://pi4j.com/kotlin/
- Size: 145 KB
- Stars: 49
- Watchers: 5
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Pi4J - Kotlin
Kotlin Interface & DSL for [Pi4J V2](https://github.com/Pi4J/pi4j-v2)
For Pi4J V1 Kotlin Bindings, check [Pi4K](https://github.com/mhashim6/Pi4K) (no longer supported).
[](https://github.com/KotlinBy/awesome-kotlin)
[](https://search.maven.org/search?q=g:%22com.pi4j%22%20AND%20a:%22pi4j-ktx%22)
[](http://www.apache.org/licenses/LICENSE-2.0)

## Documentation
Full documentation can be found on the [website](https://pi4j.com/kotlin/kotlin-api-docs/)
## Example
This is a minimal working example, make sure
to [check it out](https://github.com/Pi4J/pi4j-kotlin/blob/master/example/src/main/kotlin/MinimalExample.kt) for full
introduction and comments.
``` kotlin
const val PIN_BUTTON = 24 // PIN 18 = BCM 24
const val PIN_LED = 22 // PIN 15 = BCM 22
var pressCount = 0
console {
title("<-- The Pi4J Project -->", "Minimal Example project")
pi4j {
digitalInput(PIN_BUTTON) {
id("button")
name("Press button")
pull(PullResistance.PULL_DOWN)
debounce(3000L)
piGpioProvider()
}.onLow {
pressCount++
+"Button was pressed for the ${pressCount}th time"
}
}
digitalOutput(PIN_LED) {
id("led")
name("LED Flasher")
shutdown(DigitalState.LOW)
initial(DigitalState.LOW)
piGpioProvider()
}.run {
while (pressCount < 5) {
+"LED ${state()}"
toggle()
sleep(500L / (pressCount + 1))
}
}
}
}
}
```