zoukankan      html  css  js  c++  java
  • esp32使用ledcWrite实现呼吸灯

    ESP32 没有Arduino输出 PWM 的 analogWrite(pin, value) 方法,取而代之的 ESP32 有一个 LEDC ,设计是用来控制 LED 。

    ESP32 的 LEDC 总共有16个路通道(0 ~ 15),分为高低速两组,高速通道(0 ~ 7)由80MHz时钟驱动,低速通道(8 ~ 15)由 1MHz 时钟驱动。

    #include <Arduino.h>
    
    int freq = 2000;    // 频率
    int channel = 0;    // 通道
    int resolution = 8;   // 分辨率
    
    const int led = 18;
    void setup()
    {
    
      ledcSetup(channel, freq, resolution); // 设置通道
      ledcAttachPin(led, channel);  // 将通道与对应的引脚连接
    }
    
    void loop()
    {
      // 逐渐变亮
      for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle = dutyCycle + 5)
      {
        ledcWrite(channel, dutyCycle);  // 输出PWM
        delay(20);
      }
    
      // 逐渐变暗
      for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle = dutyCycle - 5)
      {
        ledcWrite(channel, dutyCycle);  // 输出PWM
        delay(20);
      }
    }

    参考:https://blog.csdn.net/weixin_43474408/article/details/90743082

  • 相关阅读:
    linux 常用命令
    git 常见命令
    合并两个有序链表---python
    Code Contract for .NET
    Kruskal最小生成树算法
    逻辑-哲学
    停机问题
    逆向工程
    .net framework
    python 类库
  • 原文地址:https://www.cnblogs.com/codeit/p/15538384.html
Copyright © 2011-2022 走看看