Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

ESP32 LED PWM fade not working?

Not sure if this belongs more in an electronics SE, but I'll give this a try. I'm trying to use an ESP32-C3 and an NPN transistor (2N5551) to PWM drive an LED (values below). I'm trying a simple fade, however the LED is not fading, but rather going in quite discreet steps.

I'm using analogWrite (snipped below), but I'm fairly sure the code is correct. It could very well be an issue with my soldering, but thought I'd ask here in case I'm missing something obvious?

I measured the LED at 100mA with a 2.7V drop, so I put it in series with a 22ohm resistor. Diagram below:

schematic

simulate this circuit – Schematic created using CircuitLab

 #include <Arduino.h>
 unsigned long fadeStartTime;
 void setup() {
 Serial.begin(115200);
 while(!Serial); // Wait for serial to be available
 Serial.println("Starting");
 setCpuFrequencyMhz(80);
 LOG_INFO(MODULE_NAME, "Finished setup.");
 }
 void loop() {
 unsigned long progress = millis() - fadeStartTime;
 long brightness;
 if (progress <= 3000) brightness = map(progress, 0, 3000, 0, 255);
 else if (progress <= 6000) brightness = map(6000 - progress, 0, 3000, 0, 255);
 else fadeStartTime = millis(); // restart fade again
 analogWrite(STEMS_DATA_PIN, brightness * brightness / 255);
 }

Answer*

Draft saved
Draft discarded
Cancel
5
  • Interesting observation of esp32 analogWrite() behaviour. In that configuration you need a series base resistor on the transistor (say 680 ohms to 1k). With the previous emitter follower configuration a base resistor was unnecessary. Also, if 22 ohms for the led current limiting resistor was correct in the previous configuration, it will surely be too low in the current configuration where 5v instead of about 2.7v is available. It depends on the led's forward voltage and maximum current. Commented Apr 21 at 0:10
  • @6v6gt I mean, this is my only reasonable guess as why it would work with the delay honestly. As for the resistor, values for the led are in the diagram, the 22ohms was calculated for the original 5v (with a 0.3v drop from the transistor). I also did add a base resistor of 150ohm, kind of winged it as not too expert and all the calculators online seemed to give different results. Edited the diagram to add it. Commented Apr 21 at 0:22
  • That's indeed an "interesting" behavior! On AVR, you can analogWrite() as often as you want, and that doesn't break the PWM behavior. Commented Apr 21 at 9:22
  • Out of curiosity, I have just tried the OP's original code (with the minimal necessary changes) both on a Uno and an Espressif ESP32_C3 devkitC-02 (Arduino ESP32 Core 3.0.4) and cannot duplicate the problem. The ESP32 analogWrite() appears to have undergone a number of changes. I've looked briefly at the ESP32 analogWrite issue history on github (there are currently 56 entries) so I could imagine that the OP has got an unlucky combination of board and core release level. Having said that, hammering away at analogWrite() at loop speed does not seem a good idea. Commented Apr 21 at 11:18
  • @6v6gt I think it might've also been some weird combination of other problems too. I didn't mention it, but my led was a high current (higher than a small TH led) and was wired with a 30cm thin awg30 wire. Some capacitance / inductance / whatever big electronic word might've played a part ahaha Commented Apr 21 at 11:33

AltStyle によって変換されたページ (->オリジナル) /