GPIO
NodeMcu pin (D#): | ESP8266 chip (GPIO#) | Notes |
---|---|---|
0 | 16 | free - see note. |
1 | 5 | free |
2 | 4 | free |
3 | 0 | free after booting (caution)- pull-up |
4 | 2 | connects to led - pull-up |
5 | 14 | free |
6 | 12 | free |
7 | 13 | free |
8 | 15 | free after booting - pull-down. |
From the documentation:
"On every boot/reset/wakeup, GPIO15 [8] MUST keep LOW, GPIO2 [4] MUST keep HIGH. GPIO0 [3] HIGH ->RUN MODE, LOW -> FLASH MODE. When you need to use the sleep mode,GPIO16 and RST should be connected, and GPIO16 will output LOW to reset the system at the time of wakeup."
So:
Pin 0 - is used to return from for deep sleep, so if you are using this mode, don't use it for anything else.
Pin 3 - if pulled low during reset, sends ESP8266 into flash programming mode. Pull-up.
Pin 4 - it's connected to the on-board led, and it is pulled up. Also remember that the led blinks during boot, so this pin receives signals.
Pin 8 - has pull-down as it must be low on boot.
gpio.mode()
Initialize pin to GPIO mode, set the pin in/out direction, and optional internal weak pull-up.
Sets a given pin as OUTPUT or INPUT
gpio.mode(7,gpio.OUTPUT)
gpio.mode(7,gpio.INPUT)
gpio.write()
Set digital GPIO pin value.
gpio.mode(7,gpio.OUTPUT)
gpio.write(7,gpio.HIGH)
gpio.read()
Read digital GPIO pin value. Returns 0 for low, 1 for high.
Blink with timer interrupts
"Blink" is the "hello world" of the embedded world. A blink program was introduced at the "First steps chapter", however, that program used a delay function that stopped all processing while waiting.
The following code uses a timer interrupt (code from adafruit):
-- Pin definition
pin = 2
status = gpio.LOW
duration = 500 -- 0.5 second duration for timer
-- Initialising pin
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, status)
-- Create an interval
tmr.alarm(0, duration, 1, function ()
if status == gpio.LOW then
status = gpio.HIGH
else
status = gpio.LOW
end
gpio.write(pin, status)
end)
To do
gpio.serout() Serialize output based on a sequence of delay-times in µs.
gpio.trig() Establish or clear a callback function to run on interrupt for a pin.