ESP32 NodeMCU Basic Interface Examples
Here we can see ESP32 NodeMCU Basic Interface Examples
LED glow using Button Input Method in While loop
gpio.config( { gpio=23, dir=gpio.OUT }, { gpio=0, dir=gpio.IN})
while(true)
do
if (gpio.read(0)==0) then gpio.write(23, 1)
else gpio.write(23, 0)
end
end
LED glow using Button Interrupt
gpio.trig(0, gpio.INTR_UP_DOWN,pin_interrupt_1) –GPIO-0 as INPUT
function pin_interrupt_1(level)
if (0 == gpio.read(0)) then
gpio.write(23, 1)
else
gpio.write(23, 0)
end
end
LED Blink using Timer
gpio.config( { gpio=23, dir=gpio.OUT })
ArunEworld_timer = tmr.create()
ArunEworld_timer:register(1000, tmr.ALARM_AUTO, function()
if (gpio.read(23)==1) then gpio.write(23, 0)
elseif (gpio.read(23)==0) then gpio.write(23, 1)
end
end)
ArunEworld_timer:start()
GPIO Configuration
gpio.config({ gpio=23, dir=gpio.OUT })
gpio=23→ This selects GPIO 23 on the ESP32/NodeMCU.dir=gpio.OUT→ Sets the pin as an output pin.- Effect: You can now control GPIO 23 programmatically by writing HIGH (1) or LOW (0) values.
Creating a Timer
ArunEworld_timer = tmr.create()
tmr.create()→ Creates a new software timer object.ArunEworld_timer→ Stores this timer, so it can be started, stopped, or reused.
Registering the Timer Callback
ArunEworld_timer:register(1000, tmr.ALARM_AUTO, function() if (gpio.read(23)==1) then gpio.write(23, 0) elseif (gpio.read(23)==0) then gpio.write(23, 1) end end)
1000→ Timer interval in milliseconds (1000 ms = 1 second).tmr.ALARM_AUTO→ Timer repeats automatically after each interval.function() ... end→ The callback function executed every 1 second.
Inside the callback:
gpio.read(23)→ Reads the current state of GPIO 23.if 1 then gpio.write(23,0)→ If the pin is HIGH, set it LOW.elseif 0 then gpio.write(23,1)→ If the pin is LOW, set it HIGH.
Effect: This toggles GPIO 23 every second. If you connect an LED to GPIO 23, it will blink with a 1-second interval.
Starting the Timer
ArunEworld_timer:start()
Activates the timer, so the callback function begins executing repeatedly every 1 second. ESP32 NodeMCU Basic Interface Examples.