ESP8266 NodeMCU Interface LED

LED ON/OFF

Note : Assume the pin GPIO-5 is a LED (NodeMCU pin map is 1)

  • Set LED Mode
    • Set LED pin (gpio-5) as output using gpio.mode(pin, mode [, pullup])  function.
    • Example :  gpio.mode(1, gpio.OUTPUT)
  • Turn LED ON
    • Set LED pin (gpio-5) HIGH using gpio.write(pin, level)  function.
    • Example : gpio.write(1, gpio.HIGH)  or gpio.write(1, 1)
  • Turn LED  OFF
    • Set LED pin (gpio-5) Low using gpio.write(pin, level)  function.
    • Example : gpio.write(1, gpio.LOW) or gpio.write(1,0)

 


LED Blink Using Timer

  • Required NodeMCU Modules (Firmware) GPIO Module, Timer Module
  • Required Hardware ESP8266 with Programmer (or)  NodeMCU Dev Kit, LED- 3v3 or 5v,
  • Required Software Tools ESPlorer IDE Tool

ESP8266 with LED Connection Diagram

  • Will Add Soon

NodeMCU Lua code

-- http://www.ArunEworld.com/embedded/ESPressif/ESP8266/ESP82266_NodeMCU
--[[ Required NodeMCU Modules
 Timer Module
 GPIO Module
]]--

LED_Pin = 1 -- GPIO-5
LED_Blink_Time = 1000 -- ms
gpio.mode(LED_Pin, gpio.OUTPUT)
local function Blink()
 if ( 0 == gpio.read(LED_Pin)) then 
  gpio.write(LED_Pin, gpio.HIGH)
 elseif( 1 == gpio.read(LED_Pin)) then 
  gpio.write(LED_Pin, gpio.LOW)
 end 
end

tmr.alarm(0,LED_Blink_Time,1,function() Blink() end)

Follow steps

  • Connect the circuit as per the connection diagram
  • Save the above code as “AEW_LED-Blink.lua”
  • Open ESPlorer and upload the file “AEW_LED-Blink.lua”
  • Run the file  [ dofile(“AEW_LED-Blink.lua”) ]
  • Done.!(LED will Blink based on blink time)

LED Blink uisng While loop and Timer delay function


-- http://www.ArunEworld.com/embedded/ESPressif/ESP8266/ESP82266_NodeMCU <br>--[[ Required NodeMCU Modules Timer Module GPIO Module ]]--<br>LED_Pin = 1 -- GPIO-5<br>LED_Blink_Time = 1000 -- ms<br>gpio.mode(LED_Pin, gpio.OUTPUT)<br><br>while true do<br>    tmr.delay(1000000)<br>    if ( 0 == gpio.read(LED_Pin)) then<br>        gpio.write(LED_Pin, gpio.HIGH) <br>    elseif( 1 == gpio.read(LED_Pin)) then<br>        gpio.write(LED_Pin, gpio.LOW)<br>    end<br>end<br>

 



LED Fade using PWM

Use this code in ESP8266

pin = 1<br>------ PWM setup --------------<br>pwm.setup(pin, 999, 512)    -- pwm.setup(pin, clock, duty) | set pin index 1 as pwm output, frequency is 100Hz, duty cycle is half.<br><br>pwm.start(pin)          -- pwm.start(pin)<br>duty = 1000<br><br>for i=1000,0,-1 do<br> --print(i)<br> duty = i<br> pwm.setduty(pin, duty)         -- pwm.setduty(pin, duty)<br> tmr.delay(1000)<br>end<br>

 


Code Explanation – PWM Example in NodeMCU Lua

1. Select pin

pin = 1
  • Refers to GPIO5 (in NodeMCU pin mapping, pin index 1 = GPIO5).
  • This pin will be configured as a PWM output.

2. PWM Setup

pwm.setup(pin, 999, 512)
  • Initializes PWM on the given pin.
  • Syntax: pwm.setup(pin, clock, duty)
    • pin = pin index (1 → GPIO5).
    • clock = 999 → Frequency is about 1000/10 = ~100 Hz.
      • Formula: frequency = 1000000 / clock. So, 1 MHz / 999 ≈ 1001 Hz actually.
    • duty = 512 → Initial duty cycle is 512 out of 1023 (~50%).
      • i.e., output signal is ON for half the time, OFF for half.

3. Start PWM

pwm.start(pin)
  • Starts generating the PWM signal on GPIO5.

4. Duty variable

duty = 1000
  • Just initializes duty cycle variable with 1000 (close to max 1023).

5. For loop

for i=1000,0,-1 do
 --print(i)
 duty = i
 pwm.setduty(pin, duty)
 tmr.delay(1000)
end
  • Loop runs from 1000 down to 0 in steps of -1.
  • Inside the loop:
    1. duty = i → Updates duty cycle value.
    2. pwm.setduty(pin, duty) → Changes the PWM duty cycle.
      • Higher duty → LED brighter (if LED is connected).
      • Lower duty → LED dimmer.
    3. tmr.delay(1000) → Waits 1000 µs (1 ms) before next step.

What It Does

Loop ends when duty = 0 → LED OFF.

Starts with 100% brightness (duty ≈ 1000).

Gradually reduces duty cycle from 1000 → 0.

As duty decreases:

If an LED is connected, it will smoothly fade out (dimming effect).

Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off