Created
January 26, 2018 18:09
-
-
Save jhorsman/6a93191ba31a48cf0cea75acd4c20cea to your computer and use it in GitHub Desktop.
Blink for Node MCU; Blinking the builtin led on GPIO pin 2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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
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)
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.
@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
Do you know how to do this with lua ? I tried pin 2 & pin 0 but it doesn't seem to work.