Fetching contributors…
Cannot retrieve contributors at this time
115 lines (84 sloc) 3.52 KB


This package provides a Lua module to control the GPIO on a Raspberry Pi.

The main functionality is provided by the RPi.GPIO Python Module of Ben Croston. The Lua binding follows the Python module as closely as possible.

Note; asynchroneous callbacks require darksidesync. So if you provide either add_event_detect() or add_event_callback() with a callback function you'll get a error mentioning 'darksidesync' if you did not require that module before.

Basic usage (more advanced example below):

local GPIO=require "GPIO"


GPIO.setup(18, GPIO.IN)
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, GPIO.LOW)


Additional modules

Some additional modules are included for specific hardware.

  • GPIO.lcd-hd44780 Module for hd44780 compatible LCDs (4bit mode), migrated to Lua from Adafruit. See test_lcd.lua for an example.


If you're familiar with installing packages and modules you can quickly build the module by cd-ing into rpi-gpio/lua and run make. A file will be created, which can be used in your Lua scripts.

Here's a quick list of commands for inexperienced users, to get you going with Lua on your Raspberry Pi (on Raspbian);

sudo apt-get update                     -- update your package cache
sudo apt-get install lua5.1             -- the Lua 5.1 interpreter and libs
sudo apt-get install liblua5.1-0-dev    -- development files, required by LuaRocks to build modules
sudo apt-get install luarocks           -- package manager for Lua modules
sudo apt-get install openssl            -- required for luasec (below)
sudo apt-get install libssl-dev         -- development files for openssl, required for luasec (below)
sudo luarocks install luarocks          -- LuaRocks will update itself to the latest version
sudo luarocks install luasec            -- required for LuaRocks to support https downloads

Installing the module through LuaRocks;

sudo luarocks install rpi-gpio
sudo luarocks install darksidesync      -- optional; only if you want to use callbacks
sudo luarocks install copas             -- optional; handy for callbacks, see 'test6_callbacks.lua'

If you feel like living dangerously, you can install the latest development code through;

sudo luarocks install rpi-gpio --server=



local GPIO=require "GPIO"

--BOARD Pin Layout
pins={11, 12, 13, 15, 16, 18, 22}

print ("Version: "..GPIO.VERSION..", Pi Rev: "..GPIO.RPI_REVISION)


for k,v in pairs(pins) do
  GPIO.setup(v, GPIO.OUT)

for k,v in pairs(pins) do 
  GPIO.output(v, 1)

os.execute("sleep 1")

for k,v in pairs(pins) do 
  GPIO.output(v, (k+1)%2) 

os.execute("sleep 1")

for k,v in pairs(pins) do 
  GPIO.output(v, k%2) 

os.execute("sleep 1")

p = GPIO.newPWM(12, 100)
os.execute("sleep 5")