Using darlington TIP127 with ESP32

Hi. Im making a simple project where I control an rgb 12v led strip with some TIP127 PNP transistors. The problem is that, when using PWM, the esp can't turn off completely the transistors, but when doing digitalWrite, it can. Why does this happens?

  • Let's see a schematic of your proposed circuit.

  • Let's see your complete sketch.


The battery represents the 12v input and the small LED is the rgb strip
And the code:

#include "BluetoothSerial.h"
#include <string> 
#include <sstream>
#include<cmath>
const int ledPin = 15;
const int ledPin2 = 2;
const int ledPin3 = 4;
const int freq = 5000;
const int r = 15;
const int g = 2;
const int b = 8;
bool st = 0;
int rf = 0;
int gf = 0;
int bf = 0;
int br = 0;
float gf2 = 0;
float rf2 = 0;
float bf2 = 0;
bool invert = 0;
const int resolution = 8;
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
String sis = "";
BluetoothSerial SerialBT;

void setup() {
  ledcSetup(r, freq, resolution);
  ledcSetup(g, freq, resolution);
  ledcSetup(b, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(ledPin, r);
  ledcAttachPin(ledPin2, g);
  ledcAttachPin(ledPin3, b);
  st = 1;
  leds(st,"X",1);
  delay(2000);
  Serial.begin(115200);
  SerialBT.begin("ESP32"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  st = 0;
  leds(st,"X",1);
}

void loop() {
  if (SerialBT.available()) {
    char rc;
    rc = SerialBT.read();
    String sus = String(rc);
    Serial.print(sus);
    leds(0,sus,1);
  }
  delay(20);
}
void leds(bool start,String si, bool urs){
  if(start){
    if(urs){
     ledcWrite(r, 0);
    ledcWrite(g, 0);
    ledcWrite(b, 0);
    delay(500); 
    ledcWrite(r, 255);
    ledcWrite(g, 255);
    ledcWrite(b, 255);
    }
    else{
    ledcWrite(r, 255);
    ledcWrite(g, 255);
    ledcWrite(b, 255);
    delay(500);
    ledcWrite(r, 0);
    ledcWrite(g, 0);
    ledcWrite(b, 0);
    br = 1;
    }
  }
  else{
    float mul = 0;
    float mul2 = 0;
    if(si == "v"){
      rf = 255;
      gf = 0;
      bf = 0;
    }
    if(si == "w"){
     rf = 0;
      gf = 255;
      bf = 0;
    }
    if(si == "x"){
      rf = 0;
      gf = 0;
      bf = 255;
    }
    if(si == "y"){
      rf = 255;
      gf = 255;
      bf = 255;
    }
    if(si == "e"){rf = 247; gf = 105; bf = 10;}
    if(si == "f"){rf = 31; gf = 240; bf = 81;}
    if(si == "g"){rf = 31; gf = 85; bf = 240;}
    if(si == "i"){rf = 240; gf = 167; bf = 31;}
    if(si == "j"){rf = 31; gf = 172; bf = 240;}
    if(si == "k"){rf = 165; gf = 23; bf = 116;}
    if(si == "m"){rf = 255; gf = 175; bf = 1;}
    if(si == "n"){rf = 8; gf = 188; bf = 206;}
    if(si == "o"){rf = 232; gf = 12; bf = 133;}
    if(si == "q"){rf = 234; gf = 255; bf = 15;}
    if(si == "r"){rf = 9; gf = 107; bf = 180;}
    if(si == "s"){rf = 255; gf = 3; bf = 167;}
    bool on = 1;
    if(si == "a"){on = 1;}
    if(si == "b"){on = 0;}
    if(si == "d"){if(br<=1){ledcWrite(r, 255);
    ledcWrite(g, 255);
    ledcWrite(b, 255);
    delay(500);
    ledcWrite(r, 0);
    ledcWrite(g, 0);
    ledcWrite(b, 0);}
    else{
      br = br - 1;
    }}
    if(si == "c"){if(br>=5){ledcWrite(r, 255);
    ledcWrite(g, 255);
    ledcWrite(b, 255);
    delay(500);
    ledcWrite(r, 0);
    ledcWrite(g, 0);
    ledcWrite(b, 0);}
    else{
      br++;
    }}
    if(br == 1){
      mul = 1;
    }
    else{
      mul = pow(0.8,br);
    }
    if(on){
      rf2 = rf * mul;
      gf2 = gf * mul;
      bf2 = bf * mul;
      if(urs){
        ledcWrite(r, map(rf2,255,0,0,255));
    ledcWrite(g, map(gf2,255,0,0,255));
    ledcWrite(b, map(bf2,255,0,0,255));
      }
      else{
        ledcWrite(r, rf2);
    ledcWrite(g, gf2);
    ledcWrite(b, bf2);
      }
    }
    else{
      if(urs){
      ledcWrite(r, 255);
    ledcWrite(g, 255);
    ledcWrite(b, 255);  
      }
      else{
        ledcWrite(r, 0);
    ledcWrite(g, 0);
    ledcWrite(b, 0);
      }
    }
    Serial.println(br);
    Serial.println(mul);
  }
}
  • That’s not a schematic.

A BJT transistor requires a base resistor.


A PNP transistor is used as a high side switch, you are using it as a low side switch.


If that is common anode LED, you need a low side NPN transistor switch.


A logic level MOSFET would be a better driver.

1 Like

All of the above, and

I don't see any decoupling on the voltage regulator.

A 9volt battery is ok for a smoke alarm, but not for an ESP32 that draws 100mA with 500mA peaks.
Leo..

What MOSFET would you recommend?

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