ESP32 Source code on the bottom of this article!
See the original complete project tutorial here: Android RGB LED Arduino Controller
In September 2014, I created this project with Arduino Mini Pro Mini and Android APP loved by so many readers! There are more than 100,000 App downloads by now! Since then, quite of few readers asked me how to change the sketch to be used on ESP32 with built-in Bluetooth. Here it is!! 😀
Be aware that LED PWM pins used on ESP32 were completely different, since Arduino Mini Pro and ESP32 have different architectures.
For ESP32, I used following pins:
- Pin 13 = Red
- Pin 12 = Green
- Pin 14 = Blue
- Channel definition for PWM
- R channel = 0
- G channel = 1
- B channel = 2
- Setup PWM frequency: 5000
- Setup PWM resolution: 8 (8bit = 255 colors)
- ledcSetup( channel, frequency, resolution)
- ledcAttachPin(Pin, channel)
- ledcWrite( channel, value for resolution(0~255));
END
Chinese version: 中文版 Android 手機控制 RGB LED (修改成ESP32版本)
ESP32 開源碼在本文的最後方!
請參考原本的教學步驟: Android RGB LED Arduino Controller
2014 年九月, 我用Android 手機加上Arduino Mini Pro Mini 及 HC-05 藍芽模組開發了這個專案, 它深受廣大讀者喜愛. Android 手機 APP 目前已有超過 100,000 次應用下載! 從那以後,常常有讀者問我如何更改內置藍牙的ESP32上使用。哈, 今天終於寫好了~ 😀
因為 Arduino Mini Pro 和 ESP32 具有不同的架構, 所以 ESP32 上使用的 LED PWM 腳位是完全不同的.
在 ESP32上, 我用了這些腳位:
- Pin 13 = 紅 Red
- Pin 12 = 綠 Green
- Pin 14 = 藍 Blue
因為我試過了...😂
- Channel definition for PWM
- R channel = 0
- G channel = 1
- B channel = 2
- Setup PWM frequency: 5000
- Setup PWM resolution: 8 (8bit = 255 colors)
- ledcSetup( channel, frequency, resolution)
- ledcAttachPin(Pin, channel)
- ledcWrite( channel, value for resolution(0~255));
ESP32 原始碼
請注意, R, G, B 腳位已變更為 13, 12, 14
Source code for ESP32 with built-in Bluetooth
Notice: Different R, G, B PWM pins were used for ESP32!
#include <BluetoothSerial.h> #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif #include <Wire.h> //Include libraries: SoftwareSerial & Wire BluetoothSerial BT; //RGB LED Pins, ESP32 does not allow to use pin 6, 7, 8, 9, 10, 11, //Select pin wisely or ESP32 will boot crash const int PIN_RED = 13; const int PIN_GREEN = 12; const int PIN_BLUE = 14; const int PWM_R_ledChannel = 0; const int PWM_G_ledChannel = 1; const int PWM_B_ledChannel = 2; //Setting LED PWM const int PWM_freq = 5000; const int PWM_resolution = 8; String RGB = ""; //store RGB code from BT String RGB_Previous = "255.255.255)"; //preserve previous RGB color for LED switch on/off, default White String ON = "ON"; //Check if ON command is received String OFF = "OFF"; //Check if OFF command is received boolean RGB_Completed = false; void setup() { Serial.begin(115200); //ESP32 LED definition ledcSetup(PWM_R_ledChannel, PWM_freq, PWM_resolution); ledcAttachPin(PIN_RED, PWM_R_ledChannel); ledcSetup(PWM_G_ledChannel, PWM_freq, PWM_resolution); ledcAttachPin(PIN_GREEN, PWM_G_ledChannel); ledcSetup(PWM_B_ledChannel, PWM_freq, PWM_resolution); ledcAttachPin(PIN_BLUE, PWM_B_ledChannel); BT.begin("Stonez56_ESP32S_BT"); RGB.reserve(30); Serial.print("BT is turned on!"); } void loop() { // put your main code here, to run repeatedly: //Read each character from Serial Port(Bluetooth) while (BT.available()) { char ReadChar = (char)BT.read(); // Right parentheses ) indicates complet of the string if (ReadChar == ')') { RGB_Completed = true; } else { RGB += ReadChar; } } //When a command code is received completely with ')' ending character if (RGB_Completed) { //Print out debug info at Serial output window Serial.print("RGB:"); Serial.print(RGB); Serial.print(" PreRGB:"); Serial.println(RGB_Previous); if (RGB == ON) { digitalWrite(13, HIGH); RGB = RGB_Previous; //We only receive 'ON', so get previous RGB color back to turn LED on Light_RGB_LED(); } else if (RGB == OFF) { digitalWrite(13, LOW); RGB = "0.0.0)"; //Send OFF string to turn light off Light_RGB_LED(); } else { //Turn the color according the color code from Bluetooth Serial Port Light_RGB_LED(); RGB_Previous = RGB; } //Reset RGB String RGB = ""; RGB_Completed = false; } //end if of check if RGB completed } // end of loop void Light_RGB_LED() { int SP1 = RGB.indexOf('.'); int SP2 = RGB.indexOf('.', SP1 + 1); int SP3 = RGB.indexOf('.', SP2 + 1); String R = RGB.substring(0, SP1); String G = RGB.substring(SP1 + 1, SP2); String B = RGB.substring(SP2 + 1, SP3); //Print out debug info at Serial output window Serial.print("R int ="); Serial.println(constrain(R.toInt(), 0, 255)); Serial.print("G int ="); Serial.println(constrain(G.toInt(), 0, 255)); Serial.print("B int ="); Serial.println(constrain(B.toInt(), 0, 255)); //Light up the LED with color code //**2014-09-21 //Because these RGB LED are common anode (Common positive) //So we need to take 255 to minus R,G,B value to get correct RGB color code // analogWrite(PIN_RED, (255 - R.toInt())); // analogWrite(PIN_GREEN, (255 - G.toInt())); // analogWrite(PIN_BLUE, (255 - B.toInt())); //**2021-07-10 For ESP32, analogWrite was not implemented in ESP32 //So, use ledcWrite instead ledcWrite(PWM_R_ledChannel, (255 - R.toInt())); ledcWrite(PWM_G_ledChannel, (255 - G.toInt())); ledcWrite(PWM_B_ledChannel, (255 - B.toInt())); }
No comments:
Post a Comment