Skip to content

PWM: Further analogWrite broken after ledcDetachPin #7105

Closed
@habazut

Description

@habazut

Board

ESP32 Dev Module

Device Description

Random Devkit

Hardware Configuration

Any GPIO than can do PWM

Version

v2.0.4

IDE Name

Arduino IDE

Operating System

Whatever

Flash frequency

Whatever

PSRAM enabled

no

Upload speed

Whatever

Description

The following sequence of function calls

analogWrite(pin, 128);
ledcDetachPin(pin);
analogWrite(pin,128);

does only give PWM once and not after the second call to analogWrite.

Problem: Internal conditional on pin_to_channel array prevents second ledcAttachPin.

Solution:

$ diff -u .arduino15/packages/esp32/hardware/esp32/2.0.4/cores/esp32/esp32-hal-ledc.c{.orig,}
--- .arduino15/packages/esp32/hardware/esp32/2.0.4/cores/esp32/esp32-hal-ledc.c.orig	2022-07-06 11:47:49.000000000 +0200
+++ .arduino15/packages/esp32/hardware/esp32/2.0.4/cores/esp32/esp32-hal-ledc.c	2022-08-10 00:18:47.778755484 +0200
@@ -222,6 +222,8 @@
       pin_to_channel[pin] = cnt_channel--;
       ledcAttachPin(pin, cnt_channel);
       ledcSetup(cnt_channel, 1000, 8);
+    } else {
+      ledcAttachPin(pin, cnt_channel);
     }
     ledcWrite(pin_to_channel[pin] - 1, value);
   }

Apply inline patch and it works.

Regards,
Harald.

Sketch

See above, you can reproduce

Debug Message

See above, you can reproduce

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions