2015年3月31日 星期二

NodeMCU固件-GPIO/ADC/PWM控制



參考連結: NodeMCU API指令

包括有下列Module:
node
file
wifi
wifi.sta
wifi.ap
timer
gpio
pwm
net
net server
net socket
i2c
adc
uart
1-wire
bit
spi
mqtt
mqtt client



NodeMCU 的 GPIO Pin 對應 ESP8266 GPIO Pin看連結說明.


esp12-3.jpg


NodeMCU 的 GPIO Module定義了12個GPIO Pin,對應如下:


IO indexESP8266 pinIO indexESP8266 pin
0 [*]GPIO167GPIO13 (Color Blue)
1GPIO58GPIO15 (Color Red)
2GPIO49GPIO3
3GPIO010GPIO1
4GPIO211GPIO9
5GPIO1412GPIO10
6GPIO12(Color Green)
** [] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported. *

GPIO16=0
GPIO5=1
GPIO4=2
GPIO0=3
GPIO2=4
GPIO14=5
GPIO12=6
GPIO13=7
GPIO15=8
GPIO3=9
GPIO1=10
GPIO9=11
GPIO10=12


//亮Red ,暗Green,Blue
gpio.mode(GPIO15, gpio.OUTPUT)  gpio.write(GPIO15,1) -- Red on
gpio.mode(GPIO12, gpio.OUTPUT)  gpio.write(GPIO12,0) -- Green off
gpio.mode(GPIO13, gpio.OUTPUT)  gpio.write(GPIO13,0) -- Blue off



關於GPIO Module說明

範例: (Pin index=0~12)

>gpio.mode(4, gpio.OUTPUT)
>gpio.write(4, gpio.LOW)   --點亮GPIO2 LED
>print(gpio.read(4))

0
>gpio.write(4,gpio.HIGH)  --關掉GPIO2 LED
>print(gpio.read(4))

1
>

關於ADC Module說明


測試板ADC是接光敏電阻,越暗,阻值大,電壓越大,所讀ADC數值就會越大

範例: 

>print(adc.read(0)) --暗

256
>print(adc.read(0)) --亮

0

讀Vdd33電壓
>print(adc.readvdd33()/1000) --read vdd voltage

2.944





關於PWM Module說明
Only 3 pins can be set to PWM mode at the most.

pin: 1~12, IO index
duty: 0~1023, pwm duty cycle, max 1023(10bit).
範例: 

    -- D6 is connected to green led (ESP8266 GPIO15)
    -- D7 is connected to blue led (ESP8266 GPIO12)
    -- D8 is connected to red led (ESSP8266 GPIO13)
    pwm.setup(6,500,512)
    pwm.setup(7,500,512)
    pwm.setup(8,500,512)
    pwm.start(6)
    pwm.start(7)
    pwm.start(8)
    function led(r,g,b)
      pwm.setduty(6,g)
      pwm.setduty(7,b)
      pwm.setduty(8,r)
    end
    led(512,0,0) --  set led to red
    led(0,0,512) -- set led to blue.






沒有留言:

張貼留言