24h購物| | PChome| 登入
2023-05-03 00:05:44| 人氣8| 回應0 | 上一篇 | 下一篇
推薦 0 收藏 0 轉貼0 訂閱站台

Arduino ESP32 PWM輸出 讓LED漸亮漸暗


影片演示



文章出處:網頁設計,網站架設 ,網路行銷,網頁優化,SEO - NetYea 網頁設計

用ESP32 PWM實現LED慢慢亮起。

程式的部份首要分成三個:1.設定頻道LEDchannel、2.附加到PIN腳、3.決意輸出大小。

1.設定頻道LEDchannel屬性

ledcSetup(LEDChannel, freq, resolution);
//LEDChannel設定為0,不同輸出要設定到分歧頻道,例如RGB LED就要開三個頻道分別治理R、G、B
//freq輸出頻率,建議值5000 Hz
//resolution代表輸出解析度,例如8代表0-255,10代表0-1023

2.附加到PIN腳

ledcAttachPin(ledPin, LEDChannel);
//ledPin代表腳位,看你把設備接在哪一個腳位上面
//LEDchannel代表步驟1所宣告的LEDchannel,也就是說把設定好的LEDchannel屬性附加到某個腳位上

3.決定輸出巨細。

ledcWrite(LEDChannel, dutyCycle);
//將LEDchannel輸出dutyCycle的值。

規範程式將使接在Pin16的LED逐步亮起並熄滅,規範複製於 https://randomnerdtutorials.com/esp32-pwm-arduino-ide/

1.jpg

  1. // the number of the LED pin
  2. const int ledPin = 16;  // 16 corresponds to GPIO16
  3.  
  4. // setting PWM properties
  5. const int freq = 5000;
  6. const int ledChannel = 0;
  7. const int resolution = 8;
  8.  
  9. void setup(){
  10.   // configure LED PWM functionalitites
  11.   ledcSetup(ledChannel, freq, resolution);
  12.   
  13.   // attach the channel to the GPIO to be controlled
  14.   ledcAttachPin(ledPin, ledChannel);
  15. }
  16.  
  17. void loop(){
  18.   // increase the LED brightness
  19.   for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){   
  20.     // changing the LED brightness with PWM
  21.     ledcWrite(ledChannel, dutyCycle);
  22.     delay(15);
  23.   }
  24.  
  25.   // decrease the LED brightness
  26.   for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
  27.     // changing the LED brightness with PWM
  28.     ledcWrite(ledChannel, dutyCycle);   
  29.     delay(15);
  30.   }
  31. }
複製代碼


以下內文出自:

台長: jillipn3
人氣(8) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 搞笑趣味(笑話、趣事、kuso) | 個人分類: NetYea |
此分類下一篇:網頁設計 Opencart 在Cpanel主機上點上傳不會泛
此分類上一篇:網頁設計 利用 pytube4 下載影片

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文