点亮LED
from machine import Pin #导入Pin模块
pin16=Pin(16,Pin.OUT) #将GPIO16口设置为输出口
pin16.value(1) #输出高电平
from machine import Pin
pin16=Pin(16,Pin.OUT)
pin16.value(1)
pin18=Pin(18,Pin.OUT)
pin18.value(1)
pin23=Pin(23,Pin.OUT)
pin23.value(1)
pin2=Pin(2,Pin.OUT)
pin2.value(1)
输出高电平找到对应GPIO然后通过on( )或这value(1),输出低电平让灯灭调用off( )或者value(0).
Pin(xx,Pin.OUT)
Pin.OUT定义起引脚为输出引脚,打开时会输出电压点亮LED.
流水灯效果
#!/usr/bin/env python3
#-*-coding:utf-8-*-
#说明:板载LED流水灯实验
#---------------------
from machine import Pin
from utime import sleep_ms
#定义LED管脚,可在扩展板底座找到对应GP口编号
led=[16,17,18,19,23,5,2,22]
#将对应的GPIO口都设置为输出模式
leds=[Pin(led[i],Pin.OUT) for i in range (0,8)]#pin=Pin(led[],Pin.OUT)
#循环语句
while True:
for n in range(0,8): #依次点亮
leds[n].value(1)
sleep_ms(50)
for n in range(0,8): #依次熄灭
leds[n].value(0)
sleep_ms(50)
点亮三色灯
红绿蓝1秒交替闪烁
#
#
# 说明:WS2812B三色LED灯实验交替闪烁 红绿蓝1秒交替闪烁
from machine import Pin #
from utime import sleep_ms #
import machine,neopixel #neopixel三色彩灯控制库
numpix=4 #WS2812B 灯数量
p=15 #GP15 彩灯对应的GPIO口15
strip=neopixel.NeoPixel(machine.Pin(p),numpix)
while True:
#熄灭
strip[0]=(0,0,0)
strip.write()
strip[1]=(0,0,0)
strip.write()
strip[2]=(0,0,0)
strip.write()
strip[3]=(0,0,0)
strip.write()
sleep_ms(1000)
#红色
strip[0]=(30,0,0)
strip.write()
strip[1]=(30,0,0)
strip.write()
strip[2]=(30,0,0)
strip.write()
strip[3]=(30,0,0)
strip.write()
sleep_ms(1000)
#绿色
strip[0]=(0,30,0)
strip.write()
strip[1]=(0,30,0)
strip.write()
strip[2]=(0,30,0)
strip.write()
strip[3]=(0,30,0)
strip.write()
sleep_ms(1000)
#蓝色
strip[0]=(0,0,30)
strip.write()
strip[1]=(0,0,30)
strip.write()
strip[2]=(0,0,30)
strip.write()
strip[3]=(0,0,30)
strip.write()
sleep_ms(1000)
炫彩灯带
#
#
# 说明:三色LED炫彩灯带
from machine import Pin #
from utime import sleep_ms #
import machine,neopixel #neopixel三色彩灯控制库
numpix=4 #WS2812B 灯数量
p=15 #GP15 彩灯对应的GPIO口15
strip=neopixel.NeoPixel(machine.Pin(p),numpix)
while True:
r=0
g=0
b=0
while r<255: #红灯顺序点亮
for n in range(0,4):
strip[n]=(r,0,0)
strip.write()
r=r+10
sleep_ms(200)
while g<255: # 绿灯顺序点亮
for n in range(0,4):
strip[n]=(0,g,0)
strip.write()
g=g+10
sleep_ms(200)
while b<255: #蓝灯顺序点亮
for n in range(0,4):
strip[n]=(0,0,b)
strip.write()
b=b+10
sleep_ms(200)