Expert‎ > ‎

LUA with pinguino

LUA is a great script language and it is used on a lot of embedded systems.
I tried to build script on a computer to communicate with Pinguino, and use the same script on an embedded system ( FONERA 2.0 with OpenWrt ).
This script communicate with Pinguino with the USB port and can drive outputs.
This is a very simple example and i will improve it as soon as possible, but it works, both on the computer and on the FONERA without modifications.
The goal in the future is to build a USB terminal with LUA, instead of using Python. Why not python ? because threading in libusb are buggy and LUA don't use thread but use coroutines.

What do you need on the computer ?

All this tutorial was tested on a linux computer with UBUNTU 9.10.

First, LUA 5.1 should be installed. Launch it in a terminal, then type print("LUA is ready !!") and hit Enter.
The result should be:
LUA is ready !!
Congratulations, LUA is installed !!

Now to communicate with USB, you need a libusb binding for LUA.
Download it here:
To compile this package you need the liblua5.1-0-dev package of LUA and libusb-1.0-0-dev.
Open the Makefile in the lualibusb folder and change the first 2 lines to point to the good folders, the lines for me were:

LIBUSB_SRC = /usr/include/libusb-1.0 #change with your libusb sources folder
LUA_SRC = /usr/include/lua5.1    #change with your lua sources folder

Then compile this libusb binding.

Now you should find a file in the folder. Rename this file to and copy it in

If this folder doesn't exist, create it.

Now your libusb binding is installed. Try it:
Lua 5.1.4  Copyright (C) 1994-2008, PUC-Rio

Everythig is OK, now you can try LUA with your Pinguino.

Try it !!

I wrote this very simple program for the Pinguino side:

// test USB LUA
// Jean-Pierre MANDON 2010

int i;
uchar caractere,caractere1;

void setup()
for (i=0;i<8;i++) 

void loop()
if (USB.available())
    if (caractere=='W') if (USB.available())

This program is supposed to reverse the state of output 0 to 7 when it receives a Wnumber string on USB.
example: W1 reverse the state of the pin 1

This is the computer side program ( or FONERA ) i wrote with LUA:

--- interfacing LUA with Pinguino
--- jean-pierre mandon 2010


local buses=libusb.get_busses()    --- locate USB buses on the computer

function find_pinguino()
    for dirname, bus in pairs(buses) do
        local devices=libusb.get_devices(bus)
        for filename, device in pairs(devices) do
            local descriptor=libusb.device_descriptor(device)
            if ((descriptor.idVendor==0x04D8) and (descriptor.idProduct==0xFEAA)) then
                if     ((libusb.set_configuration(handle,3))     and
                    (libusb.claim_interface(handle,0))) then
                    return handle
                else return nil

local pinguino
local keyboard

if (pinguino~=nil) then                             --- try to find a Pinguino
    print('Pinguino initialised with success !!')
    else print('Pinguino initialisation failed !!')
while (character~="x") do
    print("Pin state menu")
    print("0: reverse state of pin 0")
    print("1: reverse state of pin 1")
    print("2: reverse state of pin 2")
    print("3: reverse state of pin 3")
    print("4: reverse state of pin 4")
    print("5: reverse state of pin 5")
    print("6: reverse state of pin 6")
    print("7: reverse state of pin 7")
    print("x: exit")
    io.write("enter your choice: ")
    if ((character>="0") and (character<="7")) then
libusb.close(pinguino)                                --- close Pinguino