From 9ee978a989843132b00a4bb799a85a9f8215640c Mon Sep 17 00:00:00 2001 From: Allan Date: Sun, 1 Oct 2017 21:41:45 -0400 Subject: [PATCH 1/3] Create ledcWrite_demo_ESP32_RGB.ino adding the public domain example ledcWrite_demo_ESP32.ino to this repo. Added RGB to the name for people searching, added some comments, and renames things to make a bit more sense. --- .../ledcWrite_demo_ESP32_RGB.ino | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino diff --git a/libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino b/libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino new file mode 100644 index 00000000000..1407c44f033 --- /dev/null +++ b/libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino @@ -0,0 +1,129 @@ +/* + ledcWrite_demo_ESP32_RGB.ino + Runs through the full 255 color spectrum for an rgb led + Demonstrate ledcWrite functionality for driving leds with PWM on ESP32 + + This example code is in the public domain. + + Some basic modifications were made by vseven, mostly commenting. + */ + +// Set up the rgb led names +uint8_t ledR = A4; +uint8_t ledG = A5; +uint8_t ledB = A18; + +uint8_t ledArray[3] = {1, 2, 3}; // three led channels + +const boolean invert = true; // set true if common anode, false if common cathode + +uint8_t color = 0; // a value from 0 to 255 representing the hue +uint32_t R, G, B; // the Red Green and Blue color components +uint8_t brightness = 255; // 255 is maximum brightness, but can be changed + +// the setup routine runs once when you press reset: +void setup() +{ + Serial.begin(115200); + delay(10); + + ledcAttachPin(ledR, 1); // assign RGB led pins to channels + ledcAttachPin(ledG, 2); + ledcAttachPin(ledB, 3); + + // Initialize channels + // channels 0-15, resolution 1-16 bits, freq limits depend on resolution + // ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits); + ledcSetup(1, 12000, 8); // 12 kHz PWM, 8-bit resolution + ledcSetup(2, 12000, 8); + ledcSetup(3, 12000, 8); +} + +// void loop runs over and over again +void loop() +{ + Serial.println("Send all LEDs a 255 and wait 2 seconds."); + // If your RGB LED turns off instead of on here you should check if the LED is common anode or cathode + ledcWrite(1, 255); + ledcWrite(2, 255); + ledcWrite(3, 255); + delay(2000); + Serial.println("Send all LEDs a 0 and wait 2 seconds."); + ledcWrite(1, 0); + ledcWrite(2, 0); + ledcWrite(3, 0); + delay(2000); + + Serial.println("Starting color fade loop."); + + for (color = 0; color < 255; color++) { // Slew through the color spectrum + + hueToRGB(color, brightness); // call function to convert hue to RGB + + // write the RGB values to the pins + ledcWrite(1, R); // write red component to channel 1, etc. + ledcWrite(2, G); + ledcWrite(3, B); + + delay(100); // full cycle of rgb over 256 colors takes 26 seconds + } + +} + +// Courtesy http://www.instructables.com/id/How-to-Use-an-RGB-LED/?ALLSTEPS +// function to convert a color to its Red, Green, and Blue components. + +void hueToRGB(uint8_t hue, uint8_t brightness) +{ + uint16_t scaledHue = (hue * 6); + uint8_t segment = scaledHue / 256; // segment 0 to 5 around the + // color wheel + uint16_t segmentOffset = + scaledHue - (segment * 256); // position within the segment + + uint8_t complement = 0; + uint16_t prev = (brightness * ( 255 - segmentOffset)) / 256; + uint16_t next = (brightness * segmentOffset) / 256; + + if(invert) + { + brightness = 255 - brightness; + complement = 255; + prev = 255 - prev; + next = 255 - next; + } + + switch(segment ) { + case 0: // red + R = brightness; + G = next; + B = complement; + break; + case 1: // yellow + R = prev; + G = brightness; + B = complement; + break; + case 2: // green + R = complement; + G = brightness; + B = next; + break; + case 3: // cyan + R = complement; + G = prev; + B = brightness; + break; + case 4: // blue + R = next; + G = complement; + B = brightness; + break; + case 5: // magenta + default: + R = brightness; + G = complement; + B = prev; + break; + } +} From a38717ec24438be5440f016c96d50542ff56c4ba Mon Sep 17 00:00:00 2001 From: Allan Date: Mon, 9 Oct 2017 10:13:53 -0400 Subject: [PATCH 2/3] Update ledcWrite_demo_ESP32_RGB.ino renamed to ledcWrite_RGB.ino and added a couple more comments based on https://github.com/espressif/arduino-esp32/issues/689 --- .../ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino b/libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino index 1407c44f033..633d1c0896d 100644 --- a/libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino +++ b/libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino @@ -1,5 +1,5 @@ /* - ledcWrite_demo_ESP32_RGB.ino + ledcWrite_RGB.ino Runs through the full 255 color spectrum for an rgb led Demonstrate ledcWrite functionality for driving leds with PWM on ESP32 @@ -19,7 +19,7 @@ const boolean invert = true; // set true if common anode, false if common cathod uint8_t color = 0; // a value from 0 to 255 representing the hue uint32_t R, G, B; // the Red Green and Blue color components -uint8_t brightness = 255; // 255 is maximum brightness, but can be changed +uint8_t brightness = 255; // 255 is maximum brightness, but can be changed. Might need 256 for common anode to fully turn off. // the setup routine runs once when you press reset: void setup() @@ -43,7 +43,8 @@ void setup() void loop() { Serial.println("Send all LEDs a 255 and wait 2 seconds."); - // If your RGB LED turns off instead of on here you should check if the LED is common anode or cathode + // If your RGB LED turns off instead of on here you should check if the LED is common anode or cathode. + // If it doesn't fully turn off and is common anode try using 256. ledcWrite(1, 255); ledcWrite(2, 255); ledcWrite(3, 255); From 76316836e87bbe2c6f94476b9971d81ad2dc2598 Mon Sep 17 00:00:00 2001 From: Allan Date: Mon, 9 Oct 2017 10:43:40 -0400 Subject: [PATCH 3/3] Rename libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino to libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino Renamed. --- .../ledcWrite_RGB.ino} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename libraries/ESP32/examples/AnalogOut/{ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino => ledcWrite_RGB/ledcWrite_RGB.ino} (100%) diff --git a/libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino b/libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino similarity index 100% rename from libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino rename to libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino