You need to log in to create posts and topics.

375_BJT TRANSISTOR CURVE TRACER

Here is a project to simulate a curve tracer for bipolar BJT transistors, using the ILI9341 color TFT display and added hardware to shape the DC levels and corresponding signals to adapt the transistor under test to the MCU. To use it, first select the type of transistor to be tested: NPN or PNP. Then insert the transistor to be tested into the test connector marked CBE for the transistor pins (in our case, we have used an NPN as an example). Pressing the START button starts the test; the LED will blink, and the curves (in various colors) will begin to appear on the screen for different base currents. Modifying the gain and load resistance will display other curve shapes (by default, the most appropriate values ​​have been set to see the typical curves in their full range). When the test is finished, the LED will remain lit and ready to restart the test by pressing START again.

SOFTWARE.

The following shows the program for Arduino UNO, which requires two libraries for compilation: Adafruit_GFX.h and Adafruit_ILI9341.h

/ NPN/PNP BJT CURVE TRACER WITH THE TFT ILI9341. DEFRAN26A

#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
const uint8_t PWM_C=6, PWM_B=5, EMITTER=7, MODE=2, START=3;
const uint8_t STATUS_LED=4;
const uint8_t VB_TEST=A2, BASE_ADC=A3;  // MID-BASE
const uint8_t VC_TEST=A4, COLLECTOR_ADC=A5, EMITTER_ADC=A1; // MID-COLL, EMIT ANALOG
const float VREF_MV = 5000.0f;  // Set to the actual AVcc if needed.
const float RC_OHM = 680.0f, RB_OHM = 22000.0f;
const float BASE_FILTER_OHM = 1000.0f;
const uint8_t STEPS = 40;
const uint8_t CURVE_COUNT = 11;
uint8_t baseStepUa = 10;
uint8_t currentScaleMa = 6;
const uint16_t COLORS[] = {0xCE79, 0x64FF, 0x07FF, 0x2FF3, 0x47E6, 0xBFE0,0xFFE0, 0xFD20, 0xF986, 0xF81F, 0xB41F};
static_assert(CURVE_COUNT == sizeof(COLORS) / sizeof(COLORS[0]), "One color per curve is required");
const uint16_t BACKGROUND = 0x0842, PANEL = 0x1106, BORDER = 0x326C;
const uint16_t MAJOR_GRID = 0x21C8, MINOR_GRID = 0x1925, MUTED_TEXT = 0xA5B8;
const int X0 = 32, Y0 = 222, PLOT_WIDTH = 195, PLOT_HEIGHT = 162;
Adafruit_ILI9341 tft(10, 9, 8);  // CS, DC, RST. 13=CLK, 11=MOS
bool pnp;
bool isPnp();
void prepareMode();
void drawGrid();
void showStatus(const __FlashStringHelper *message);
bool sweep();

void setup()
{
  pinMode(MODE, INPUT_PULLUP);
  pinMode(START, INPUT_PULLUP);
  pinMode(STATUS_LED, OUTPUT);
  pinMode(PWM_C, OUTPUT);
  pinMode(PWM_B, OUTPUT);
  prepareMode();
  tft.begin(4000000);
  tft.setRotation(0);  // Portrait, 240 x 320.
  drawGrid();
  showStatus(F("Press START"));
}

void loop()
{
  if (isPnp() != pnp)
  {
    prepareMode();
    drawGrid();
    showStatus(F("Press START"));
  }
  if (digitalRead(START) != LOW) return;
  delay(25);
  if (digitalRead(START) != LOW) return;
  if (!sweep())
  {
    prepareMode();
    drawGrid();
    showStatus(F("Mode changed | Press START"));
  }
  while (digitalRead(START) == LOW && isPnp() == pnp) delay(5);
  delay(25);
}

bool isPnp()
{
  return digitalRead(MODE) == LOW;
}

void idle()
{
  analogWrite(PWM_B, pnp ? 255 : 0);
  analogWrite(PWM_C, pnp ? 255 : 0);
}

void prepareMode()
{
  digitalWrite(STATUS_LED, LOW);
  pinMode(EMITTER, INPUT);
  digitalWrite(EMITTER, LOW);
  do
  {
    pnp = isPnp();
    idle();
    delay(180);  // Let both filters settle with the emitter disconnected.
  } while (isPnp() != pnp);
  digitalWrite(EMITTER, pnp ? HIGH : LOW);
  pinMode(EMITTER, OUTPUT);
}

void pwm(uint8_t pin, int level)
{
  level = constrain(level, 0, 255);
  analogWrite(pin, pnp ? 255 - level : level);
}

float readMv(uint8_t pin)
{
  analogRead(pin);  // Discard the first reading after switching ADC channels.
  uint16_t sum = 0;
  for (uint8_t i = 0; i < 16; ++i) sum += analogRead(pin);
  return sum * (VREF_MV / (16.0f * 1023.0f));
}

void drawGrid()
{
  tft.fillScreen(BACKGROUND);
  constexpr int titleWidth = (sizeof("BJT CURVE TRACER") - 1) * 12;
  constexpr int titleX = (240 - titleWidth) / 2;
  tft.fillRect(titleX - 8, 7, titleWidth + 16, 24, 0x2355);
  tft.setTextSize(2);
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(titleX, 11); tft.print(F("BJT CURVE TRACER"));
  tft.drawFastHLine(0, 36, 240, pnp ? 0xF41D : 0x07FF);
  tft.setTextSize(1);
  tft.setTextColor(MUTED_TEXT);
  tft.setCursor(10, 45); tft.print(pnp ? F("|Ic| (mA)") : F("Ic (mA)"));
  tft.fillRect(X0, Y0 - PLOT_HEIGHT, PLOT_WIDTH + 1, PLOT_HEIGHT + 1, ILI9341_BLACK);
  for (uint8_t i = 1; i < 2 * currentScaleMa; i += 2)
    tft.drawFastHLine(X0, Y0 - PLOT_HEIGHT * i / (2 * currentScaleMa), PLOT_WIDTH + 1, MINOR_GRID);
  for (uint8_t i = 1; i < 10; i += 2)
    tft.drawFastVLine(X0 + PLOT_WIDTH * i / 10, Y0 - PLOT_HEIGHT, PLOT_HEIGHT + 1, MINOR_GRID);
  for (uint8_t i = 0; i <= currentScaleMa; ++i)
  {
    int y = Y0 - PLOT_HEIGHT * i / currentScaleMa;
    tft.drawFastHLine(X0, y, PLOT_WIDTH + 1, i ? MAJOR_GRID : MUTED_TEXT);
    tft.setCursor(18, y - 3);
    tft.print(i);
  }
  for (uint8_t i = 0; i <= 5; ++i)
  {
    int x = X0 + PLOT_WIDTH * i / 5;
    tft.drawFastVLine(x, Y0 - PLOT_HEIGHT, PLOT_HEIGHT + 1, i ? MAJOR_GRID : MUTED_TEXT);
    tft.setCursor(x - 2, Y0 + 5);
    tft.print(i);
  }
  tft.setCursor(172, 239); tft.print(pnp ? F("|Vce| (V)") : F("Vce (V)"));
  tft.fillRect(6, 251, 228, 40, PANEL);
  tft.drawFastHLine(6, 251, 228, BORDER);
  tft.setCursor(12, 255); tft.print(F("Base current Ib (uA)"));
  for (uint8_t i = 0; i < CURVE_COUNT; ++i)
  {
    int x = 12 + (i % 6) * 37;
    int y = 267 + (i / 6) * 11;
    tft.fillRect(x, y + 2, 9, 3, COLORS[i]);
    tft.setCursor(x + 12, y);
    tft.print(i * baseStepUa);
  }
}

void showStatus(const __FlashStringHelper *message)
{
  tft.fillRect(0, 297, 240, 23, BACKGROUND);
  tft.fillRoundRect(8, 298, 176, 21, 5, 0x032C);
  tft.fillRoundRect(190, 298, 42, 21, 5, pnp ? 0xF41D : 0x07FF);
  tft.setTextColor(BACKGROUND);
  tft.setCursor(202, 304); tft.print(pnp ? F("PNP") : F("NPN"));
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(14, 304);
  tft.print(message);
}

void showProgress(uint8_t curve)
{
  showStatus(F("Curve "));
  tft.print(curve + 1);
  tft.print('/');
  tft.print(CURVE_COUNT);
  tft.print(F("   Ib "));
  tft.print(curve * baseStepUa);
  tft.print(F(" uA"));
  tft.fillRect(14, 314, 164, 2, BORDER);
  tft.fillRect(14, 314, 164 * (curve + 1) / CURVE_COUNT, 2, COLORS[curve]);
}

bool waitForSettle(uint8_t ms)
{
  for (uint8_t t = 0; t < ms; t += 5)
  {
    if (isPnp() != pnp) return false;
    digitalWrite(STATUS_LED, millis() % 500 < 250 ? HIGH : LOW);
    delay(5);
  }
  return isPnp() == pnp;
}

int baseDrive(uint8_t targetUa)
{
  if (targetUa == 0) return 0;
  return int((700.0f + targetUa * 0.001f * (RB_OHM + BASE_FILTER_OHM)) *255 / VREF_MV + 0.5f);
}

bool adjustBase(uint8_t targetUa, int direction, int &baseLevel)
{
  for (uint8_t attempt = 0; targetUa && attempt < 3; ++attempt)
  {
    float ib = direction * (readMv(VB_TEST) - readMv(BASE_ADC)) * 1000.0f / RB_OHM;
    float error = targetUa - ib;
    if (error >= -1.5f && error <= 1.5f) break;
    float correction = error * (RB_OHM + BASE_FILTER_OHM) * 0.255f / VREF_MV;
    int newLevel = constrain(baseLevel + int(correction + (correction > 0 ? 0.5f : -0.5f)), 0, 255);
    if (newLevel == baseLevel) break;
    baseLevel = newLevel;
    pwm(PWM_B, baseLevel);
    if (!waitForSettle(65)) return false;
  }
  return true;
}

float readCollectorMa(int direction)
{
  return direction * (readMv(VC_TEST) - readMv(COLLECTOR_ADC)) / RC_OHM;
}

bool chooseRanges(int direction)
{
  const uint8_t probeUa = 10;
  int drive = baseDrive(probeUa);
  pwm(PWM_C, 255);
  pwm(PWM_B, drive);
  if (!waitForSettle(145) || !adjustBase(probeUa, direction, drive)) return false;
  float probeMa = readCollectorMa(direction);
  float measuredUa = direction * (readMv(VB_TEST) - readMv(BASE_ADC)) * 1000.0f / RB_OHM;
  baseStepUa = 10;  // Target about 3 mA at the top curve, leaving collector voltage headroom.
  if (probeMa > 0.05f && measuredUa > 2.0f)
    baseStepUa = constrain(int(3.0f * measuredUa / (probeMa * (CURVE_COUNT - 1))), 1, 10);
  const uint8_t topUa = (CURVE_COUNT - 1) * baseStepUa;
  drive = baseDrive(topUa);
  pwm(PWM_B, drive);
  if (!waitForSettle(110) || !adjustBase(topUa, direction, drive)) return false;
  float topMa = readCollectorMa(direction);
  currentScaleMa = constrain(int(topMa * 1.15f + 1.0f), 1, 6);
  idle();
  return true;
}

bool sweep()
{
  const int direction = pnp ? -1 : 1;
  showStatus(F("Setting ranges..."));
  if (!chooseRanges(direction)) return false;
  drawGrid();
  for (uint8_t curve = 0; curve < CURVE_COUNT; ++curve)
  {
    const uint8_t targetUa = curve * baseStepUa;
    int baseLevel = baseDrive(targetUa);
    pwm(PWM_B, baseLevel);
    pwm(PWM_C, 0);
    showProgress(curve);
    if (!waitForSettle(110)) return false;
    int previousX = -1, previousY = 0;
    for (uint8_t step = 0; step <= STEPS; ++step)
    {
      pwm(PWM_C, (uint16_t(step) * 255 + STEPS / 2) / STEPS);
      if (!waitForSettle(65)) return false;
      if (!adjustBase(targetUa, direction, baseLevel)) return false;
      float vc = readMv(COLLECTOR_ADC);
      float vce = direction * (vc - readMv(EMITTER_ADC));
      float ic = direction * (readMv(VC_TEST) - vc) / RC_OHM;
      if (vce < -15 || ic < -0.03f)  // Skip reverse-polarity points, allowing for ADC error near zero.
      {
        previousX = -1;
        continue;
      }
      int x = X0 + int(constrain(vce, 0.0f, 5000.0f) * PLOT_WIDTH / 5000.0f + 0.5f);
      int y = Y0 - int(constrain(ic, 0.0f, float(currentScaleMa)) * PLOT_HEIGHT / currentScaleMa + 0.5f);
      if (previousX >= 0) tft.drawLine(previousX, previousY, x, y, COLORS[curve]);
      else tft.drawPixel(x, y, COLORS[curve]);
      previousX = x;
      previousY = y;
    }
  }
  idle();
  digitalWrite(STATUS_LED, HIGH);
  showStatus(F("Done | START to repeat"));
  return true;
}

FILES PROVIDED:

The attached ZIP file includes: the schematic, the "ino" file, the "exe" file, and all subcircuits used in this project, all located in the "data" directory. Simulide version 1.2 or higher is required.

P. de Francisco

Uploaded files: