Skip to content

Instantly share code, notes, and snippets.

@jhorsman
Created January 26, 2018 18:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhorsman/6a93191ba31a48cf0cea75acd4c20cea to your computer and use it in GitHub Desktop.
Save jhorsman/6a93191ba31a48cf0cea75acd4c20cea to your computer and use it in GitHub Desktop.
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);
}
@flywire
Copy link

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.

@mmilk23
Copy link

mmilk23 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