Skip to content

Instantly share code, notes, and snippets.

@jhorsman
Created Jan 26, 2018
Embed
What would you like to do?
Blink for Node MCU; Blinking the builtin led on GPIO pin 2.
/**
* Blink for Node MCU
* also see https://arduino.stackexchange.com/questions/38477/does-the-node-mcu-v3-lolin-not-have-a-builtin-led
*
* Turns on an LED on for one second,
* then off for one second, repeatedly.
*/
#include "Arduino.h"
// On a NodeMCU board the built-in led is on GPIO pin 2
#define LED_BUILTIN 2
void setup()
{
Serial.begin(14400);
// initialize LED digital pin as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
Serial.println("Node MCU blink!");
// turn the LED on (the built-in led on a Node MCU board is active low)
digitalWrite(LED_BUILTIN, LOW);
// wait for a second
delay(1000);
// turn the LED off (the built-in led on a Node MCU board is active low)
digitalWrite(LED_BUILTIN, HIGH);
// wait for a second
delay(1000);
}
@AakashPandey

This comment has been minimized.

Copy link

@AakashPandey AakashPandey commented Apr 17, 2018

Do you know how to do this with lua ? I tried pin 2 & pin 0 but it doesn't seem to work.

@Bartvelp

This comment has been minimized.

Copy link

@Bartvelp Bartvelp commented May 27, 2018

@AakashPandey
I dont understand why but you should use GPIO 4 (?)
for turning it on:

gpio.mode(4,gpio.OUTPUT)
gpio.write(4, 0)

For turning it off

gpio.mode(4,gpio.OUTPUT)
gpio.write(4, 1)
@flywire

This comment has been minimized.

Copy link

@flywire flywire commented Jan 3, 2019

I dont [sic]understand why but you should use GPIO 4 (?)

It's confusing with esp8266 modules because pin numbers on board don't map to GPIO - Pin D4 is actually GPIO2.

Lua must be using board pin numbers.

@marcelomp3

This comment has been minimized.

Copy link

@marcelomp3 marcelomp3 commented Dec 15, 2020

@flywire check the GPIO code. https://nodemcu.readthedocs.io/en/release/modules/gpio/
there's a little difference between IO index and ESP8266 pins

also, see my code for example :

switch_light = true
mytimer = tmr.create()
gpio.mode(0, gpio.OUTPUT)
gpio.mode(4, gpio.OUTPUT)
mytimer:register(10000, 
                 tmr.ALARM_AUTO, 
                 function(mytimer)
                    if (switch_light) then
                        gpio.write(0, gpio.LOW)
                        gpio.write(4, gpio.HIGH)
                        switch_light = false;
                    else
                        gpio.write(0, gpio.HIGH)
                        gpio.write(4, gpio.LOW)
                        switch_light = true;
                    end
                 end)
mytimer:interval(5000)
mytimer:start()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment