Monday, 28 September 2015 20:41

Lua_2W_Motor: 2 wheel motor library for Lua running on wireless router

2 wheel motor library for Lua running on wireless route

This is a motor library for Lua running on wireless router which flashed with OpenWrt firmware. It allows a wireless router control the motors without using micro-controller. This library works on any dual-full-bridge driver such as L298.

Each motor require two GPIOs for controlling, therefore 2 motors will use up 4 GPIOs. Most of the wireless router may not have enough GPIO to control the motors, but you can use other pin on the router such as the LED pin, this require to unload the leds_gpio module.


Source code and download

  • Download the source code ( to your computer.
  • Extract the zip file to your computer.
  • Upload all files to router \usr\lib\lua directory, I'm using WinSCP to upload files from computer to wireless router as shown in figure below.
    Upload files using WinSCP


  • Right click the two files and change its permissions to 0755 as shown in figure below.
    Change file permissions


GPIO Configuration

The default configuration is configured to follow the GL-iNet wireless router pinout (i.e. GPIO 20, 19, 18, 22 & 21), you may change the following value to suit yourself.

Motor[1].pinA = 21
Motor[1].pinB = 22
Motor[2].pinA = 18
Motor[2].pinB = 19

Using the library

Lua comes with the default installation and support web service. Following steps show you how to test the Lua_motor library under the Lua command line interpreter.

  • SSH to wireless router.
  • Enter router user name and password.
  • Type lua to bring up the Lua command line interpreter as shown in figure below. Each command enter into the Lua interpreter is executed immediately.

    login as: root
    [email protected]@'s password:

    BusyBox v1.22.1 (2014-09-20 22:01:35 CEST) 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, r42625)
    * 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
    [email protected]:~# lua
    Lua 5.1.5 Copyright (C) 1994-2012, PUC-Rio (double int32)

Controlling a Motor

Lua_2W_Motor library must loaded before you can controlling a motor.
Syntax: motor_run(motorNumber, direction)
Where motorNumber can be 1 or 2, direction must be either FORWARD, BACKWARD or STOP

  • Example 1: Motor 1 forward
    >require "2w_motor" --import the Lua_2W_Motor library
    >motor_run(motor, FORWARD) --or motor_run(motor, 8)
  • Example 2: Motor 2 backward
    >motor_run(motor, BACKWARD) --or motor_run(motor, 2)
  • Example 3: Motor 1 stop
    >motor_run(motor, STOP) --or motor_run(motor, 5)

Controlling a car

Syntax: car_run(direction)
Where direction can be FORWARD, BACKWARD, LEFT, RIGHT & STOP

  • Example 4: Car forward
    >require "2d_motor" --import the Lua_2W_Motor library
    >car_run(FORWARD) --or car_run(8)
  • Example 5: Car stop
    >car_run(STOP) --or car_run(5)
  • Example 6: Car left
    >car_run(LEFT) --or car_run(4)

Controlling a car from web browser

Create a new file with the following codes and save it as \www\cgi-bin\car. Remember to change its file permission to 0755.

require "gpio"
require "2w_motor"

local str= os.getenv("QUERY_STRING") -- query string from URL
local cmd= string.sub(str,1,1) -- first characters
value=tonumber(parameter) -- convert from string to integer

Now you can use the following url to control the car
Where NUMBER is ranged from 1 to 9

  •  Example 1: Car forward
  • Example 2: Car Stop
Read 11159 times Last modified on Sunday, 28 February 2016 18:30


  • Comment Link Millardanola Monday, 30 December 2019 14:06 posted by Millardanola

    free lesbian chat rooms fucks on camera teen chat chat hot teen webcams local teen chat room freecams.casaspace teen chat room com spycam beautiful japan teen naked girls on cam free
    camera inside penis campbell nude teen chat room free chat chat adult finder spy cam nude anal sex web cams sexy asian webcam free no pill no condom creampie cams

  • Comment Link BarbaraStest Friday, 15 November 2019 15:52 posted by BarbaraStest

    Всем привет
    Рекомендуют врачи
    препараты для лечения рассеяного склероза

    лечение гепатита +с софосбувиром велпатасвир рибавирин отзывы

  • Comment Link Stevambums Friday, 15 November 2019 14:55 posted by Stevambums

    Ambien Pharmacys Online What Does Lasix Do For Horses viagra online prescription Viagra Erfahrungen Kaufen

  • Comment Link Stevambums Thursday, 14 November 2019 21:24 posted by Stevambums

    Propecia Antes Y Despues Viagra Comentarios Online Lasix Cheap cialis generic Online Apotheke Viagra Kamagra Info Cialis Y La Ceguera

  • Comment Link nebo Friday, 28 September 2018 20:16 posted by nebo

    Hi friend...
    Sorry for my english...
    I want to run your project on Raspberry with Openwrt.
    I have a question
    Compiles the finished image with your files. Are any additional modules required for selection at compilation whether all required are already in the package you provided ??

Leave a comment

Back to Top