Saturday, 15 August 2015 16:53

Lua_ShiftOut: A 74HC595 Shift Register Library for Lua

Lua ShiftOut A 74HC595 Shift Register Library for Lua

I want to built a wifi car without using micro contoller, due to the limited GPIO availability on the wireless router, a 74HC595 Shift Register is needed to expand its outputs.

This is a 74HC595 Shift Register library for Lua running on wireless router flashed with OpenWRT, it may run in any linux-based embedded system. It can be used to shift out data from either the most (i.e. the leftmost) or least (rightmost) significant bit.

GPIO configuration

The default GPIO configuration is configured to follow the GL-iNet wireless router pinout (i.e. GPIO 20, 19, 18, 22 & 21), however it is very easily re-configure the layout to suit yourself.

Below is the pin connections between GL-iNet wireless router and 74HC595 Shift Register. Click here if you want to know more about GPIO.

Router GPIO 20 - 74HC595 Pin 12 (Latch)
Router GPIO 19 - 74HC595 Pin 14 (Data)
Router GPIO 18 - 74HC595 Pin 11 (Clock)
Router GPIO 22 - 74HC595 Pin 13 (Output Enable)

Pin connections between GL iNet wireless router and 74HC595 Shift Register 

Note: You can always disconnect OE (74HC595 Pin13) from GPIO21 and connect it  to groud  if you want to use only 3 signals to control the Shift Register.

Code Samplers

With only two lines of code, you can shift any values to the Shift Register. Example below send a value 6 (00000110) to the Shift Register starting from least significant bit (LSBFIRST).

  1. require "shiftOut"
  2. update_ShiftRegister(6, LSBFIRST) --alternative: update_ShiftRegister(6)

Example below will send the value starting from most significant bit (MSBFIRST) to the Shift Register:

  1. require "shiftOut"
  2. update_ShiftRegister(255, MSBFIRST) --turn on all outputs


Example using binary number

  1. require "shiftOut"
  2. update_ShiftRegister_Binary("10100010",LSBFIRST)


List of default values

You can overwrite the following settings in order to use with different router.

LatchPin = 20 (LatchPin connected to GPIO20)
DataPin = 19  (DataPin connected to GPIO19)
ClockPin = 18 (ClockPin connected to GPIO18)
OutputEnablePin = 22 (Future use for PWM control)
Number_of_bit = 8 (8=single shift register, 16=two shift register, etc)


Example below will send a 16bits value to the Shift Register, 321 will output 0000000101000001

  1. require "shiftOut"
  2. Number_of_bit = 16
  3. update_ShiftRegister(321)


Here is an example to re-configure pin configuration to match with your router. You can use other pins, like those connected to the LEDs. 

  1. require "gpio" --require if you want to re-configure GPIO
  2. require "shiftOut"
  3. LatchPin = 7
  4. DataPin = 29
  5. ClockPin = 26
  6. OutputEnablePin = 22
  7. pinMode(LatchPin, OUTPUT)
  8. pinMode(DataPin, OUTPUT)
  9. pinMode(ClockPin, OUTPUT)
  10. pinMode(OutputEnablePin, OUTPUT)
  11. update_ShiftRegister(6)


Step by step

I'm using a GL-iNet wireless router for this project, it is preloaded with the OpenWRT firmware. You must upgrade your router to OpenWRT firmware if your router does not already have OpenWRT. Below is a detailed guide to use the Lua_ShiftOut library.


  • A router (eg. TL-WR1043ND, TL-MR3020, GL.iNet) flashed with OpenWRT, I suggested to use GL.iNet, it is preloaded with OpenWRT.
  • SSH client software such as PuTTy
  • FTP/SCP client software such as WinSCP

Source Code

Find the Router IP address

Now, you must know your router IP address and its login user name, most of the OpenWRT firmware router is configured to with user name root. For my GL.iNet wireless router, the default IP address and user name is and root respectively.

If you don't know your router IP address, I would suggested to use Soft Perfect Network Scanner to detect your router IP address.

SoftPerfect Network Scanner

Uploading Files and Folders to Router

I'm using WinSCP to copy files/folders from computer to router. You need to download the source code (Lua_GPIO & Lua_ShiftOut libraries) and copy them to OpenWRT search path (eg. /usr/lib/lua) as shown in figure below.


Enable SSH

Before enabling SSH, you must set a password for your router.

  1. Open up a browser such as Internet Explorer.
  2. Navigate to your router IP (eg.
  3. Login to the router and change the password,  SSH is enabled automatically.


Below is an example (filename: shiftOut_example.lua) to show you how to control the LED from router, it is already included in your download.

  • Upload the file (shiftOut_example) to your router. I copy it to router /tmp directory. 
  • SSH to router
    PuTTy Configuration


  • Login and change the file access permission to 755. Finally, run the example

    login as: root
    root_192.168.8.1's password:

    BusyBox v1.22.1 (2014-10-08 16:34:50 HKT) built-in shell (ash)
    Enter 'help' for a list of built-in commands.

    _______ ________ __
    | |.-----.-----.-----.| | | |.----.| |_
    | - || _ | -__| || | | || _|| _|
    |_______|| __|_____|__|__||________||__| |____|
    |__| W I R E L E S S F R E E D O M
    BARRIER BREAKER (14.07, r42853)
    * 1/2 oz Galliano Pour all ingredients into
    * 4 oz cold Coffee an irish coffee mug filled
    * 1 1/2 oz Dark Rum with crushed ice. Stir.
    * 2 tsp. Creme de Cacao
    root@GL-iNet:~# chmod 755 /tmp/shiftOut_example
    root@GL-iNet:~# /tmp/shiftOut_example.lua

Using the browser

Save the following file to /www/cgi-bin and give it a name (eg. control)

Now, you can control it  from internet.

  • Turn on first LED
  • Turn on all LED
Read 12446 times Last modified on Sunday, 28 February 2016 18:39
Back to Top