echo '' ;

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

  • Use
    -- 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)
    
    while true do
        tmr.delay(1000000)
        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
    

     


LED Fade using PWM

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

     


 

 


Next :

Previous :


 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading