TShopping

 找回密碼
 註冊
搜索
查看: 3659|回復: 0
打印 上一主題 下一主題

[教學] Arduino ESP32 PWM輸出 讓LED漸亮漸暗

[複製鏈接]
跳轉到指定樓層
1#
發表於 2020-5-12 22:31:07 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
用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. // the number of the LED pin
  2. const int ledPin = 16;  // 16 corresponds to GPIO16

  3. // setting PWM properties
  4. const int freq = 5000;
  5. const int ledChannel = 0;
  6. const int resolution = 8;

  7. void setup(){
  8.   // configure LED PWM functionalitites
  9.   ledcSetup(ledChannel, freq, resolution);
  10.   
  11.   // attach the channel to the GPIO to be controlled
  12.   ledcAttachPin(ledPin, ledChannel);
  13. }

  14. void loop(){
  15.   // increase the LED brightness
  16.   for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){   
  17.     // changing the LED brightness with PWM
  18.     ledcWrite(ledChannel, dutyCycle);
  19.     delay(15);
  20.   }

  21.   // decrease the LED brightness
  22.   for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
  23.     // changing the LED brightness with PWM
  24.     ledcWrite(ledChannel, dutyCycle);   
  25.     delay(15);
  26.   }
  27. }
複製代碼

影片演示



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

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-23 21:11 , Processed in 0.074650 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表