ledcDetachPin(Pin) seems not working

Goal it to apply alternatively PWM to an output. Code below does not actually detach OutC. PWM stays on OutC an OutA stays always high.
Any clue, thanks.
//######################
void loop() {
potValue = analogRead(potPin);
Serial.println("Pot = " +String(potValue));
ledcDetachPin(OutA);
digitalWrite(OutA, HIGH); // on Pin 19
ledcAttachPin(OutC, 0); // Pin 22
ledcWrite(0, potValue); // (PWM_Ch, DutyCycle 0 ... 4095)
delay(5000);
ledcDetachPin(OutC);
digitalWrite(OutC, HIGH); // off Pin 22
ledcAttachPin(OutA, 0); // Pin 19
ledcWrite(0, potValue); // (PWM_Ch, DutyCycle 0 ... 4095)
// loop()
}

We ask that you post complete code. Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Where are the function definitions for the ledcDetachPin and ledcAttachPin functions?

What Arduino board?

ledcDetachPin(Pin)
Issue solved. Code below functioning as expected

// ################################################################
// ### Board select ESP32 Dev Module or Wemos D1 Mini ESP 32 OK ###
// ### Peltier H-Bridge Test V1.0                               ###
// ### © Michael Jacoby 20211207, GNU General Public License    ###
// ### 4 pin fan PWM 25 kHz, 5 V                                ###
// ################################################################
// Done --> test proper function with real circuit, OK
// ToDo --> tweak R & f for best PWM and minimal transient switching loss 
// ToDO --> add temperature measurement, compare to set temperature, apply PWM = f(dTemp)
// ToDo --> add PID law to obtain smoth control of set temperature if simple ctrl is not within ± 2°K
// Todo --> add OLED temperature & set temperature display

//  __________________________________ +12V    Inductive load needs 4 x Diodes across MOSFET's 
//       | |   |            |    | |
//       R R   |            |    R R            R = 4 x 1k // keep low to allow fast gate charging  
//       | |   S            S    | |            0 = 4 x 10k //base current limit
//       | /-G-O A'      B' O--G-\ |            A', B' z. B. 2 x 1/2 IRF 7319 PMOS, 
//  A -0--┤   D-- +Load- --D     ├--0- B       A, B NPN BC337
//       | \   D            D    / |            0 = 2 x IRL7913 //N-PMOS pair
//      /----G-0 D'      C' 0-G---\             C', D' z. B. 2 x 1/2 IRF 7319 NMOS
// D-0-┤   |   S           S   |   ├--0- C     C, D NPN BC337 
//      \  |   |            |   |  /
//      |  |   |            |   |  |
//==================================== GND

// A,B LOW  --> OFF 
// C,D HIGH --> OFF
// A'- C' +- --> C PWM, A HIGH, D HIGH, B LOW
// B'- D' +- --> D PWM, A LOW,  B HIGH, C HIGH
// ### change from --> to ###
// A'- C' to B'- D'--> A LOW, C HIGH, B HIGH, D PWM //in that order to 
// B'- D' tp A'- C'--> B LOW, D HIGH, A HIGH, C PWM //in that order

const uint8_t  outA = 25; 
const uint8_t  outB = 26;
const uint8_t  outC = 27;
const uint8_t  outD = 32;
const uint8_t  potPin = 33; 
const uint8_t  useChannel = 1; 
int potValue = 0;
//forward declaration TBD

//###########################################################################################
void setup() {
  Serial.begin(115200);
  pinMode(outA,OUTPUT);
  pinMode(outB,OUTPUT);
  pinMode(outC,OUTPUT);
  pinMode(outD,OUTPUT);
  allOff(); 
  ledcSetup(1, 4000, 12); // (channel, frequency, resolution 12 bit) // frequency TBD  
  // setup
}

//###########################################################################################
void loop() {
  // get_dTemp(); //readTemp, read_setTemp, calculate PWM = dTemp * TBD  
  potValue = analogRead(potPin);
  potValue = map(potValue, 0,4095, -3900, 3900);
    // A,B LOW  --> OFF 
    // C,D HIGH --> OFF
    // A'- C' +- --> C PWM, A HIGH, D HIGH, B LOW
    // B'- D' +- --> D PWM, A LOW,  B HIGH, C HIGH
    // ### change from --> to ###
    // A'- C' to B'- D'--> A LOW, C HIGH, B HIGH, D PWM //in that order to 
    // B'- D' tp A'- C'--> B LOW, D HIGH, A HIGH, C PWM //in that order
if (potValue < 0) { 
    // A+ -- C-
    ledcDetachPin(outD); 
    digitalWrite(outD, HIGH); // D OFF
    digitalWrite(outA, HIGH); // A ON    
    digitalWrite(outB, LOW);  // B OFF
    // (gpio, channel) //assigning same PWM_Ch for else loop --> OK
    ledcAttachPin(outC, useChannel);     
    // (PWM_Ch, DutyCycle 0 ... 4095) // negative PWM numbers --> NoGo
    ledcWrite(1, (potValue+3900)); 
    Serial.print("\nPWM C = ");  Serial.println(String(potValue/39*(-1)) +" %");
   }
  else { 
    // B+ -- D-
    ledcDetachPin(outC);
    digitalWrite(outC, HIGH); // C OFF
    digitalWrite(outA, LOW);  // A OFF
    digitalWrite(outB, HIGH); // B ON
    ledcAttachPin(outD, useChannel); 
    potValue = map(potValue, 0,3900, 3900, 0);      
    ledcWrite(1, (potValue));       // (PWM_Ch, DutyCycle 0 ... 4095)
    Serial.print("\nPWM D = ");  Serial.println(String(100-potValue/39) +" %");
  }
  // loop()
}

//###########################################################################################
void allOff() {
  //All transistors OFF
  digitalWrite(outA, LOW); // 25 A OFF
  digitalWrite(outB, LOW); // 26 B OFF
  digitalWrite(outC, HIGH); // 27 C OFF
  digitalWrite(outD, HIGH); // 32 D OFF
  Serial.println("\nAll OFF"); 
  //allOff()
}
//###########################################################################################

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.