Liking cljdoc? Tell your friends :D

Build Status


A basic library for reading, writing and watching GPIO signals on a Raspberry Pi, in a Clojure REPL-friendly way. Now, also targets ClojureScript.


Add the following to your project.clj

[clj-gpio 0.2.0]

Fire up a REPL, and require gpio.core.

GPIO Read/Write

We can open a basic read/write gpio port as follows (let's say we have an LED conncted to GPIO 17):

user=> (require '[gpio.core :refer :all] :reload)
user=> (def port (open-port 17))

To read the value of the port, we can do the following:

user=> (read-value port)

To set values on the port, The port needs to be configured for out mode:

user=> (set-direction! port :out)

This also works with 'out and "out". A value can be written to the port as follows:

user=> (write-value! port :high)

With our LED connected to gpio 17, we should see it turned on. We can also read back the value and see that (= :high @port).

We can also toggle the state, for convenience:

user=> (toggle! port)

which will flip the state from :low to :high or vice versa.

GPIO Listening.

We can also pull events off of a gpio port by using open-channel-port. In addition to setting directions, values etc, we set the edge change that we'll listen for, and we can create a core.async channel from which can receive values.

For example (if we have a push button on GPIO 18):

user=> (def ch-port (open-channel-port 18))
user=> (set-direction! ch-port :in)
user=> (set-edge! ch-port :both) ; or :falling, :rising, and :none to disable 

We'll also set the bit to :high when the button pressed:

user=> (set-active-low! ch-port true) 

Let's turn on the LED we defined in the Read/Write example above when our button is pressed:

user=> (def ch (create-edge-channel ch-port))
user=>  (require '[clojure.core.async :as a :refer [go <!]])
user=> (go (loop []
         (when-let [value (<! ch)]
            (write-value! port value)
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@1197ad0>

When we're finished with the channel, we call:

 user=> (a/close! ch)

And clean up our ports:

user=> (close! port)
user=> (close! ch-port)


First compile the java sources:

lein javac

then fire up your REPL and require gpio.core as usual.

Note, the edge channel will only operate on the Raspberry PI platform.


Copyright © 2014 Peter Schwarz

Distributed under the Eclipse Public License, the same as Clojure.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close