Kotlin DSL for Pi4J V2
Find a file
Muhammad Hashim 5e395e72f8
Some checks failed
Build pi4j-kotlin / build-and-test (push) Failing after 10s
Update environment variables for Maven Central publishing in release workflow
Signed-off-by: Muhammad Hashim <msg@mhashim6.me>
2026-02-22 22:27:24 +03:00
.github/workflows Update environment variables for Maven Central publishing in release workflow 2026-02-22 22:27:24 +03:00
.run default test config 2022-12-28 21:30:47 +02:00
example Updated pi4j to v4.0.0 with Java25. New artefact wrapping jSerialComm 2026-02-20 22:27:35 +03:00
gradle/wrapper Update Java version to 25 and Gradle to 9.1.0 in build and release workflows 2026-02-21 00:20:23 +03:00
lib Refactor Gradle build scripts for Maven Central publishing and GPG signing 2026-02-22 22:24:27 +03:00
lib-serial Refactor Gradle build scripts for Maven Central publishing and GPG signing 2026-02-22 22:24:27 +03:00
.gitignore ignore .idea 2022-09-11 13:00:26 +02:00
build.gradle.kts Refactor Gradle build scripts for Maven Central publishing and GPG signing 2026-02-22 22:24:27 +03:00
gradle.properties restore project level gradle.properties 2022-09-14 19:19:31 +02:00
gradlew Update Java version to 25 and Gradle to 9.1.0 in build and release workflows 2026-02-21 00:20:23 +03:00
gradlew.bat Update Java version to 25 and Gradle to 9.1.0 in build and release workflows 2026-02-21 00:20:23 +03:00
LICENSE.txt License 2022-09-11 13:23:53 +02:00
README.md Fix punctuation in README.md description 2026-02-22 10:51:06 +01:00
settings.gradle.kts Updated pi4j to v4.0.0 with Java25. New artefact wrapping jSerialComm 2026-02-20 22:27:35 +03:00

Pi4J - Kotlin

Kotlin Interface & DSL for Pi4J.

For Pi4J V1 Kotlin Bindings, check Pi4K (no longer supported).

Awesome Kotlin Badge Maven Central License GitHub Actions build state

Documentation

Full documentation can be found on the website

Modules

Module Artifact Description
lib pi4j-ktx Core Kotlin DSL for Pi4J (digital/analog GPIO, PWM, I2C)
lib-serial pi4j-ktx-serial Serial communication DSL wrapping jSerialComm

Example

This is a minimal working example, make sure to check it out for full introduction and comments.

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)
    }.onLow {
      pressCount++
      +"Button was pressed for the ${pressCount}th time"
    }

    digitalOutput(PIN_LED) {
      id("led")
      name("LED Flasher")
      shutdown(DigitalState.LOW)
      initial(DigitalState.LOW)
    }.run {
      while (pressCount < 5) {
        +"LED ${state()}"
        toggle()
        sleep(500L / (pressCount + 1))
      }
    }
  }
}

Serial

Serial communication is provided by the pi4j-ktx-serial module, which wraps jSerialComm (independent of Pi4J core since serial was removed in Pi4J 4.0.0).

Gradle setup:

dependencies {
  implementation("com.pi4j:pi4j-ktx-serial:4.0.0")
  implementation("com.fazecast:jSerialComm:2.11.0")
}

Usage:

import com.pi4j.ktx.io.serial.serial
import com.pi4j.ktx.io.serial.open

serial("/dev/ttyS0") {
  baudRate = 115200
  dataBits = 8
  // stopBits, parity, flowControl also available
}.open {
  // `this` is a jSerialComm SerialPort
  // port is automatically closed when the block exits
  outputStream.write("Hello\n".toByteArray())
  val response = inputStream.bufferedReader().readLine()
}