Simple GPIO example

I have been mostly using openhab via MQTT commands to Arduino’s and ESP8266’s but ofcourse have been eying the mainly unused pins on my Raspberry Pi. In all honesty however, when I glanced over the various articles dealing with the use of GPIO pins, I got dizzy :slight_smile:
But there is no way but forward, so i decided to just give it a try. It turned out to be much easier than I thought.

First of all, install the GPIO binding in the PaperUI. After one has done that, there isnt really much else to do on that binding. There is no services/gpio.cfg although some of the documentation mentions that file.

Before I go further it is good to note that I used the openhabian distro and I presume a lot of things are already OK in that, but if you did a manual install, it might be good to check if the user ‘openhab’ is member of ‘gpio’ and if not, set that with

sudo adduser openhab gpio

That’s basically all one needs to do.
Let’s setup an ‘input’ which in openhab is a ‘Contact’ and an ‘output’, which in openhab is a Switch. In fact, those are the only states the binding allows.

There is no pressing need to define anything in your sitemap file if e.g. you add the items to a group in your itemsfile, but if you prefer a sitemap then add this to your sitemap file:

Switch item=GPIO_LAMP

Let’s pick some pins to use. It is easiest to use pins on the Raspberry header that are close to a ground pin.

Looking at the pin out of the raspi, we see that for instance GPIO pins 21,13,5, 7, 10, 27,22,24,25,12,16 and 26 are next to a ground pin. there are a few more, but those might have other functions so i stayed away from those.
I picked GPIO23 and GPIO24 as that one is also close to a 3V3 pin and when using that as input one can always decide later to make that have a HIGH or a LOW input.

The configuration string for the binding is defined as follows:

gpio="pin:PIN_NUMBER [debounce:DEBOUNCE_INTERVAL] [activelow:yes|no] [force:yes|no] [initialValue:high|low]"

for those who consider this as double dutch, it will become clear in the items definition:

Add this to your items file:

Switch GPIO_LAMP "Lamp" (LivingRoom) { gpio="pin:23 force:yes" }
Contact GPIO_BUTTON "Button [%s]" (LivingRoom) { gpio="pin:24 activelow:yes" }

The group ‘LivingRoom’ is not mandatory if you have defined your sitemap and of course you may use another group that suits you more.

The Switch that I set up is quite simple. In fact it already would be enough if it would have been:

Switch GPIO_LAMP "Lamp" {gpio="pin:23"}.

But with ‘force:yes’ we force the use of the pin even if it would have been in use by another process. Obviously that might not be the most elegant way to do it, but as i am pretty sure I am not using it for another process, I just wanted to make sure Openhab would have the pin available.

The current of a GPIO pin is far to small to drive any significant load as it can deliver some 60mA I think.
But the output can drive a small LED to test it… but use a 330 ohm series resistor, no matter what other websites tell you: you dont want to fry your GPIO pin. Eventually it would be more useful to drive a small relay module as long as it has a separate transistor to drive the relay (I know, I am cautious)

The configuration allows for some other parameters.
i already mentioned 'force:yes|no

activelow yes|no  this means that when the switch is off, there is no voltage on the pin
initialValue:high:low determines the state the pin will take during initialisation.
debounce:interval is used to set the debounce value for the switch in milisecs

If we look at the output, I have not added an activelow because I thought that was the default value (but correct me if I am wrong)
If we look at our input, I set that to activelow, because the way the button works is it applies voltage to trigger the pin and I added it because I was not sure about the default
I did not set a debounce for the contact, but you could state:

Contact GPIO_BUTTON "Button [%s]" (LivingRoom) { gpio="pin:24 debounce:10 activelow:yes" }

This all worked for me, but as said, I used the openhabian distro and that went fine


Thank for this, haven’t tried it this way yet. I do have another Pi in my network for music and I have Moode installed on it. The MPD binding controls simple commands for music and then I also have a python script that responds to MQTT which uses the GPIO pinson Pi to turn the amplifier ON/OFF with a relay.


Sounds great (no pun intended).
I do not know much about MPD, but will look into that a bit more in the future

This helped me a lot. Thank you.

This was exactly what I needed to get into the GPIO game. It is less than 2 hours later and I can now control my landscape lighting with openHAB.

Details: The lighting runs on 12V and the transformer/timer combo is indoors beside the electrical panel. I changed the mode on the transformer from TIMER to ON and am now using a 5V relay to toggle power to the lights (controlled by the raspberry pi that runs openHAB).

I have set the schedule by editing my .rules file which will also turn them on at 6:45PM: and off at 10:30PM

Next, I will be utilizing more GPIO to control additional relays for the pool equipment and to take temp/humidity readings and some other stuff TBD.

I am happy i could help. I saw using the gpio pins as a big hurdle as well, until I just went ahead and tried.

sounds like a nice setup. Plenty of pins left for u to play with

I’m going crazy here. I’m just doing a simple test and want to just use Python. Everything seems fine, but I can’t seem to get the GPIO pin to switch and beginning to wonder if there is any reason this won’t work out-the-box with OpenHabian.



if you are using Python, you may need to install a few libraries. nevertheless it shouldn’t be too hard.
You may want to have a look at my other Tutorial control gpio pins with Python/MQTT

Things to look out for: you need to install the RPi.GPIO library and you need to address the pins by their proper number (Board or BCM), but you might have done that.
Anyway, have a look at my other tutorial (linked above) and see if that helps you

Thank you. I’ve gone through the list of binaries in your example and they’re were already installed and at the latest version.

Interestingly the gpio readall command doesn’t work for me (RPi 3).

I must have got something wrong with the pin-out. The multimeter reads 0 volts between pin 6 (GND) and pin 8 (+).

I’m using GPIO.BOARD, so with the Micro USB (Power) port facing me and the NIC to my right, pin 6 is the third pin in from the right, on the bottom row.

At this point, I am only testing with a multimeter, but I am using the code provided here, “Practice circuit 1”.

I have a PoE hat installed and although it shouldn’t affect it, I’ll try with it powered from the USB in the morning.

in order to use the gpio readall command you need to have wiringPi installed.
not sure if I fully understand your description coz the NIC and USB are at the same side of the board so they would all be facing you or all be on your right, but lets say with the pinheaders on the top of the board would be physical pin 35, BCMpin 19, WiringPi pin25.

In that same position physical pin 6 would be third from the left on the top row… but that is a ground connection. Pin8 is GPIO14.It will only be ’ +’ if you switch it on

I would suggest the following:
install wiringPi (sudo apt-get install wiringPi)
If you want to keep using the program in the link, use physical pin 8 which is BCM pin 14.Make sure you know which pins you are using. From your description I understood you had your board possibly 180 degrees wrong, but maybe I just didn’t understand you correctly.

If you remain unsure about using the right pin, issue the gpio readall command. That should represent the pins on your board in the same way as if you have the board upright with the pin header on your right hand side. The gpio readall command also shows the state of the pin so if you (think you) use pin 8,check with gpio readall to see if that pin now indeed is an output and if it is switched high or low

Sorry, when I wrote my last post, I didn’t have the Pi in front of me. By USB port, I meant the micro USB power socket. Updated my last post.

The code linked just seemed like a good, clear “hello world” example to turn an LED on and off. I’m actually creating the circuit for adding some LEDs to a MoBo case and wanted to use the 3.3v GPIO from my RasberryPi for a test rig. My only Pi happens to have OpenHabian installed, I suppose I could configure another SD card with ordinary Raspbian…

You were right, I had the board 180 degrees around the wrong way. Probably not helped by the fact the PoE hat has no numbering, although I’m sure I checked out which was pin 1. Anyway, it now works. Thank you.

On a side note, sudo apt-get install wiringPi didn’t work. I checked my libraries were up-to-date. I had to install and compile from Git, using these instructions.

Thanks again. One happy camper.

great that you got it working.
bit surprised the apt-get install wiringPi didn’t work. Had no problems with it. Oh well, you got it now :slight_smile:

Hi @Kees_van_Gelder,

I started using GPIO’s to get switches connected to OH. It took me a bit but it works. I am now in the process on implementing MQTT, so appears I’m doing the reverse to you in that respect. Anyway, do you know how to setup a toggle function in OH using two GPIO pins and one switch item?

For clarification I have an RF remote which have separate On and Off buttons. At the moment I have an On and Off switch setup in OH and using .rules to toggle each switch for 500ms. I am finding it a pain to tell my google home commands. The switches are setup for example, Lounge_On and Lounge_Off. So if I want to tell G’home to turn on the lounge lamp I have to say “turn lounge on on” and to turn off "turn lounge off on. It would be nice if I could switch it the usual way.

My thought train was along the lines of MQTT where you have the line

String KitchenTopL "KitchenTopL [%s]" {mqtt="<[mqtt:buttons:state:*:10-54],>[mqtt:openhab/out:command:*:${command}]"}

Is there a way to place the two GPIO pins as the controlling state and the string do the switching work?

Any help for a push in the right direction would be appreciated if possible.

I have never tried that myself, but I believe it is possible to attach two outgoing and two incoming MQTT commands to a channel.
So as ON command you could actually send the ON command for the One and the OFF command for the other


Do I install this on the opnehabian Pi or the Remote Pi?
Sorry for such a stupid question, very new to this!

this is for the openhabian Pi.
If you want to adres GPIO pins on a remote Pi you can use MQTT like here

Hi. I want to read an analog input. My hardware is a beaglebone black. I could read it from linux but not from OH2. I tried to add an item like this:

Number AIN5 “Analog Input 5” { AIN5=“ANALOG_IN:5” }

but it didn’t work.

from linux I put this “cat /sys/bus/iio/devices/iio:device0/in_voltage5_raw” and I can see the value.

I have no problems with DI’s and DO’s using contact and switch with GPIO bindings.

Any idea? Someone can help me? Thanks

There is no specific binding for analog values afaik.
But what you can do is read the value of /sys/bus/iio/devices/iio:device0/in_voltage_raw and either MQTT that to openhab OR use the REST API to put it in openhab.
This is simple to do with Python.

If you dont know how, have a look at how the DS18B20 or DHTx transfers their values with a python program, it basically comes down to reading the file, parsing it to get just the one value you need and then use MQTT or REST.
REST API uses:
requests.put('<YOUR ITEM>/state',value) .

Maybe this tutorial can help you on your way Reading a DHT11/22 with 'overlay' and send result via REST API

1 Like

Thanks. I dont know anything about python but I will try it. Thanks again

its good to try, in order to learn. if you really don’t manage, call on me again

I’m trying to trigger a relay with this but the relay just stays on all the time. Any ideas what could be causing this?