2019-4-30 13:59:12 [显示全部楼层]
8873浏览
查看: 8873|回复: 1

[ESP32系列教程] 用ESP32+MicroPython+Thonny点亮WS2812灯带

[复制链接]
本帖最后由 gada888 于 2019-4-30 14:31 编辑

尽管python的编程软件很多,anaconda,pycharm or python ide.个人认为最好用的是Thonny。虽然做AI项目时候可能需要用pyCharm这类的去编程。但低级别单片机是上的py编程一定选Thonny了。这次就是用Thonny环境来给ESP32单片机和RGB灯带硬件编程。
用ESP32+MicroPython+Thonny点亮WS2812灯带图5
这是Thonny主界面了。
用ESP32+MicroPython+Thonny点亮WS2812灯带图7
Thonny下载地址
用ESP32+MicroPython+Thonny点亮WS2812灯带图2
选定端口
用ESP32+MicroPython+Thonny点亮WS2812灯带图6
选MicroPython主控
用ESP32+MicroPython+Thonny点亮WS2812灯带图3
先输入一段程序,测试下ESP32上的LED的显示

import machine, neopixel
import time

n = 14
# strip control gpio
p = 14

np = neopixel.NeoPixel(machine.Pin(p), n)

# set single pixel (1st pixel = index [0]) to red color
np[0] = (255, 0, 0)
np.write()
time.sleep(1)

# set strip color
def set_color(r, g, b):
  for i in range(n):
    np = (r, g, b)
    np.write()

set_color(0, 0, 255)
time.sleep(1)

# fade in/out
def fade_in_out(color, wait):
  for i in range(0, 4 * 256, 8):
    for j in range(n):
      if (i // 256) % 2 == 0:
        val = i & 0xff
      else:
        val = 255 - (i & 0xff)
        if color == 'red':
          np[j] = (val, 0, 0)
        elif color == 'green':
          np[j] = (0, val, 0)
        elif color == 'blue':
          np[j] = (0, 0, val)
        elif color == 'purple':
          np[j] = (val, 0, val)
        elif color == 'yellow':
          np[j] = (val, val, 0)
        elif color == 'teal':
          np[j] = (0, val, val)
        elif color == 'white':
          np[j] = (val, val, val)
      np.write()
    time.sleep_ms(wait)

#fade_in_out('red', 0)
fade_in_out('green', 10)
#fade_in_out('blue', 25)
#fade_in_out('purple', 10)
fade_in_out('yellow', 10)
fade_in_out('teal', 10)
#fade_in_out('white', 10)
time.sleep(1)

# bounce
def bounce(r, g, b, wait):
  for i in range(4 * n):
    for j in range(n):
      np[j] = (r, g, b)
    if (i // n) % 2 == 0:
      np[i % n] = (0, 0, 0)
    else:
      np[n - 1 - (i % n)] = (0, 0, 0)
    np.write()
    time.sleep_ms(wait)
      
bounce(255, 0, 125, 50)
time.sleep(1)

# cycle
def cycle(r, g, b, wait):
  for i in range(4 * n):
    for j in range(n):
      np[j] = (0, 0, 0)
    np[i % n] = (r, g, b)
    np.write()
    time.sleep_ms(wait)

cycle(0, 255, 0, 50)
time.sleep(1)

# function to go through all colors
def wheel(pos):
  # Input a value 0 to 255 to get a color value.
  # The colours are a transition r - g - b - back to r.
  if pos < 0 or pos > 255:
    return (0, 0, 0)
  if pos < 85:
    return (255 - pos * 3, pos * 3, 0)
  if pos < 170:
    pos -= 85
    return (0, 255 - pos * 3, pos * 3)
  pos -= 170
  return (pos * 3, 0, 255 - pos * 3)

# rainbow
def rainbow_cycle(wait):
  for j in range(255):
    for i in range(n):
      rc_index = (i * 256 // n) + j
      np = wheel(rc_index & 255)
    np.write()
    time.sleep_ms(wait)

rainbow_cycle(10)
rainbow_cycle(5)
time.sleep(1)

# turn off all pixels
def clear():
  for i in range(n):
    np = (0, 0, 0)
    np.write()

clear()


测试ws2812b彩灯带。
用ESP32+MicroPython+Thonny点亮WS2812灯带图1
用cat /main.py命令测试主程序
用ESP32+MicroPython+Thonny点亮WS2812灯带图4
用%lsdevice命令测试ESP32里有什么程序,
用ESP32+MicroPython+Thonny点亮WS2812灯带图9
点击三角运行按钮,可以运行程序,但并没有烧录到ESP32里。
用ESP32+MicroPython+Thonny点亮WS2812灯带图8
要在device里运行点击激活如上功能。上传程序。
最后因为Thonny并没有能从ESP32里删除文件的功能。很多人用上传空文件的方法达到删除的目的。
用ESP32+MicroPython+Thonny点亮WS2812灯带图10
用ESP32+MicroPython+Thonny点亮WS2812灯带图11
main code.jpg

陨落的泪痕  学徒

发表于 2023-9-3 00:34:58

# 这个Demo,最新的MicroPython跑不起来的更正代码: from machine import Pin from neopixel import NeoPixel  p=25 n=64 #初始化 p = Pin(p, Pin.OUT) np = NeoPixel(p, n)  # set single pixel (1st pixel = index [0]) to red color np[0] = (255, 0, 0) np.write() time.sleep(1)  # set strip color def set_color(r, g, b):   for i in range(n):     np[i] = (r, g, b)     np.write()  set_color(0, 0, 255) time.sleep(1)  # fade in/out def fade_in_out(color, wait):   for i in range(0, 4 * 256, 8):     for j in range(n):       if (i // 256) % 2 == 0:         val = i & 0xff       else:         val = 255 - (i & 0xff)         if color == 'red':           np[j] = (val, 0, 0)         elif color == 'green':           np[j] = (0, val, 0)         elif color == 'blue':           np[j] = (0, 0, val)         elif color == 'purple':           np[j] = (val, 0, val)         elif color == 'yellow':           np[j] = (val, val, 0)         elif color == 'teal':           np[j] = (0, val, val)         elif color == 'white':           np[j] = (val, val, val)       np.write()     time.sleep_ms(wait)  #fade_in_out('red', 0) fade_in_out('green', 10) #fade_in_out('blue', 25) #fade_in_out('purple', 10) fade_in_out('yellow', 10) fade_in_out('teal', 10) #fade_in_out('white', 10) time.sleep(1)  # bounce def bounce(r, g, b, wait):   for i in range(4 * n):     for j in range(n):       np[j] = (r, g, b)     if (i // n) % 2 == 0:       np[i % n] = (0, 0, 0)     else:       np[n - 1 - (i % n)] = (0, 0, 0)     np.write()     time.sleep_ms(wait)        bounce(255, 0, 125, 50) time.sleep(1)  # cycle def cycle(r, g, b, wait):   for i in range(4 * n):     for j in range(n):       np[j] = (0, 0, 0)     np[i % n] = (r, g, b)     np.write()     time.sleep_ms(wait)  cycle(0, 255, 0, 50) time.sleep(1)  # function to go through all colors def wheel(pos):   # Input a value 0 to 255 to get a color value.   # The colours are a transition r - g - b - back to r.   if pos < 0 or pos > 255:     return (0, 0, 0)   if pos < 85:     return (255 - pos * 3, pos * 3, 0)   if pos < 170:     pos -= 85     return (0, 255 - pos * 3, pos * 3)   pos -= 170   return (pos * 3, 0, 255 - pos * 3)  # rainbow def rainbow_cycle(wait):   for j in range(255):     for i in range(n):       rc_index = (i * 256 // n) + j       np[i] = wheel(rc_index & 255)     np.write()     time.sleep_ms(wait)  rainbow_cycle(10) rainbow_cycle(5) time.sleep(1)  # turn off all pixels def clear():   for i in range(n):     np[i] = (0, 0, 0)     np.write()  clear()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail