pinMode()
Configures the specified pin to behave either as an input or an output
- Syntax:
pinMode(pin, mode)
- Example1: Set GPIO 20 as input)
pinMode(20, INPUT)
- Example2: Set GPIO 20 as output)
pinMode(20, OUTPUT)
digitalWrite()
Write a HIGH or a LOW value to a pin.
- Syntax:
digitalWrite(pin, value) - Example1: Set GPIO 20 to low
digitalWrite(20, LOW) - Example2: Set GPIO 20 to high
digitalWrite(20, HIGH)
digitalRead()
Reads the value from a specified pin, either HIGH or LOW.
- Syntax:
digitalRead(pin) - Example: Read value from GPIO 20, return high or low
digitalRead(20)
delay()
Pauses the program for the amount of time (in seconds)
- Syntax:
delay(second) - Example: Delay 0.5 second
delay(0.5)
pin_direction_status()
Chcek the direction of a give pin
- Syntax:
pin_direction_status(pin) - Example: Check the direction of pin20, return o if it is declared as output, return i if it is declared as input, otherwise return nothing ("")
pin_direction_status(20)
Default Configuration
This is the pin layout for GL.iNet wireless router, you can change its value to match with your hardware configuration.
GPIO = {20, 19, 18, 22, 21}
Example
Example below is a Lua script running on OpenWRT which turn on GPIO 20, it then turn off after 0.5 second
#!/usr/bin/lua require "gpio" --import lua_gpio library IO_pin=20 -- or IO_pin=GPIO[1] pinMode(IO_pin,OUTPUT) -- configure IO_pin as output digitalWrite(IO_pin, HIGH) -- turn it ON print(digitalRead(IO_pin)) -- print the output delay(0.5) -- wait for 0.5 second digitalWrite(IO_pin, LOW) -- turn it OFF print(digitalRead(IO_pin)) -- print the output
Another Example
#!/usr/bin/lua require "gpio" ----- set GPIO as output for i=1, 5 do pinMode(GPIO[i],OUTPUT) end ----- set GPIO to HIGH for i=1, 5 do digitalWrite(GPIO[i],HIGH) delay(0.2) end ----- set GPIO to LOW for i=1, 5 do digitalWrite(GPIO[i],LOW) delay(0.2) end
Download
Lua_GPIO source code is available here.
Related articles
Lua_ShiftOut: A 74HC595 Shift Register Library for Lua