Troubleshoot driving a multi-colour LED

mungoh
Posts: 2
Joined: Wed Feb 25, 2026 7:59 pm

Troubleshoot driving a multi-colour LED

Postby mungoh » Wed Feb 25, 2026 8:14 pm

Hi,
My very first post here: please be gentle.

I bought an "ESP32 Basic Starter Kit" labelled as "Hosyond" and am working through the example applications on this wee kit using this Linux box to host the Arduino IDE.
I am having problems on Example 6 which deals with driving a single RGB LED. The example source uses a couple of library functions which are now obsolete, so I changed the code and fixed that and crossed my fingers. It compiled and has somewhat ran, but although the web interface seems to work fine, the LED stubbornly stays off.
I then laced the code to check that calls to library functions were returning Success, and lo and behold the three calls in the code to the library function "ledcWrite" are all coming back as failing.
Presuming I am not in the right sub-forum, which forum ought I repeat this question in for help please?

Details omitted here to keep the issue small. Thanks in advance.

Mungo

Sprite
Espressif staff
Espressif staff
Posts: 10596
Joined: Thu Nov 26, 2015 4:08 am

Re: Troubleshoot driving a multi-colour LED

Postby Sprite » Thu Feb 26, 2026 12:30 am

Can you post your code as well as the errors it throws?

mungoh
Posts: 2
Joined: Wed Feb 25, 2026 7:59 pm

Re: Troubleshoot driving a multi-colour LED

Postby mungoh » Thu Feb 26, 2026 8:43 am

Code as below. I've redacted the password for obvious reasons here! :-)
Its the sample code from the Hosyond website but I have tinkered and used #ifdefs to remove the old obsolete function calls and add in replacements.
And I have beefed up other function calls so that the returned results are output:

Code: Select all

// Load Wi-Fi library
#include <WiFi.h>

// Replace with your network credentials
const char* ssid = "RedRidgesMesh";
const char* password = "<redacted for this forum post>;
// Set web server port number to 80
WiFiServer server(80);

// Decode HTTP GET value
String redString = "0";
String greenString = "0";
String blueString = "0";
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int pos4 = 0;

// Variable to store the HTTP req  uest
String header;

// Red, green, and blue pins for PWM control
const int redPin = 13;     // 13 corresponds to GPIO13
const int greenPin = 12;   // 12 corresponds to GPIO12
const int bluePin = 14;    // 14 corresponds to GPIO14

// Setting PWM frequency, channels and bit resolution
const int freq = 5000;
const int redChannel = 0;
const int greenChannel = 1;
const int blueChannel = 2;
// Bit resolution 2^8 = 256
const int resolution = 8;

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

void setup() {
  Serial.begin(115200);
  // configure LED PWM functionalitites
#if 0
  ledcSetup(redChannel, freq, resolution);
  ledcSetup(greenChannel, freq, resolution);
  ledcSetup(blueChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(redPin, redChannel);
  ledcAttachPin(greenPin, greenChannel);
  ledcAttachPin(bluePin, blueChannel);
#else
  // Reworked due to library changes. Mungo 24/Feb/2026

/*  if(ledcSetClockSource(LEDC_USE_RTC8M_CLK))
  {
    Serial.println("Setting ledc Clock Source worked");
  }
  else
  {
    Serial.println("Setting ledc Clock Source failed");
  }
*/
  if(ledcAttachChannel(redPin, freq, resolution, redChannel))
    {
      Serial.println("Red PIN successfully attached");
    }
    else
    {
      Serial.println("Red PIN failed to attach");
    }
  if(ledcAttachChannel(greenPin, freq, resolution, greenChannel))
      {
      Serial.println("Green PIN successfully attached");
    }
    else
    {
      Serial.println("Green PIN failed to attach");
    }
  if(ledcAttachChannel(bluePin, freq, resolution, blueChannel))
      {
      Serial.println("Blue PIN successfully attached");
    }
    else
    {
      Serial.println("Blue PIN failed to attach");
    }
#endif
  
  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    currentTime = millis();
    previousTime = currentTime;
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected() && currentTime - previousTime <= timeoutTime) {            // loop while the client's connected
      currentTime = millis();
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
                   
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            client.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\">");
            client.println("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js\"></script>");
            client.println("</head><body><div class=\"container\"><div class=\"row\"><h1>ESP Color Picker</h1></div>");
            client.println("<a class=\"btn btn-primary btn-lg\" href=\"#\" id=\"change_color\" role=\"button\">Change Color</a> ");
            client.println("<input class=\"jscolor {onFineChange:'update(this)'}\" id=\"rgb\"></div>");
            client.println("<script>function update(picker) {document.getElementById('rgb').innerHTML = Math.round(picker.rgb[0]) + ', ' +  Math.round(picker.rgb[1]) + ', ' + Math.round(picker.rgb[2]);");
            client.println("document.getElementById(\"change_color\").href=\"?r\" + Math.round(picker.rgb[0]) + \"g\" +  Math.round(picker.rgb[1]) + \"b\" + Math.round(picker.rgb[2]) + \"&\";}</script></body></html>");
            // The HTTP response ends with another blank line
            client.println();

            // Request sample: /?r201g32b255&
            // Red = 201 | Green = 32 | Blue = 255
            if(header.indexOf("GET /?r") >= 0) {
              pos1 = header.indexOf('r');
              pos2 = header.indexOf('g');
              pos3 = header.indexOf('b');
              pos4 = header.indexOf('&');
              redString = header.substring(pos1+1, pos2);
              greenString = header.substring(pos2+1, pos3);
              blueString = header.substring(pos3+1, pos4);
              /*Serial.println(redString.toInt());
              Serial.println(greenString.toInt());
              Serial.println(blueString.toInt());*/
#if 1
              Serial.println("red=" + redString + ", green=" + greenString + ", blue =" + blueString); // For debugging: show what was sent.
#endif
              if(ledcWrite(redChannel, redString.toInt()))
              {
                Serial.println("Red write was successful");
              }
              else
              {
                Serial.println("Red write was UNsuccessful");
              }
              if(ledcWrite(greenChannel, greenString.toInt()))
                            {
                Serial.println("Green write was successful");
              }
              else
              {
                Serial.println("Green write was UNsuccessful");
              }
              if(ledcWrite(blueChannel, blueString.toInt()))
                            {
                Serial.println("Blue write was successful");
              }
              else
              {
                Serial.println("Blue write was UNsuccessful");
              }
            }
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

Omitting some previous stuff, here is what I believe is the root of the problem.
08:32:50.663 -> Red PIN successfully attached
08:32:50.663 -> Green PIN successfully attached
08:32:50.663 -> Blue PIN successfully attached
08:32:50.663 -> Connecting to RedRidgesMesh
...
08:33:08.194 -> red=255, green=250, blue =169
08:33:08.194 -> Red write was UNsuccessful
08:33:08.194 -> Green write was UNsuccessful
08:33:08.194 -> Blue write was UNsuccessful
08:33:08.194 -> Client disconnected.
08:33:08.194 ->
Hope I have included sufficient info in the above - let me know if I am short.

Sincerely, I am grateful for any help. If there is some online resource I could go to solve my own problems myself, I would love to know about it. It's quite daunting with this technology: there are so many variants and the documentation I have so far seen does not address this subject to a level that I would like (I like to complain - I'm a retired Software Engineer with a grudge! :-) )

Thanks in advance

Mungo

Who is online

Users browsing this forum: Google [Bot] and 19 guests