Electronic – arduino – LED dims when MOSFET switching at a higher speed

arduinoledmosfet

I want to drive LEDs using an Arduino Due and a MOSFET. Below is the circuit I am using, taken from another post: Driving LED strip from microcontroller

circuit schematic

I replaced the transistor in the picture with a IRLB8721PbF. Datasheet:https://cdn-shop.adafruit.com/datasheets/irlb8721pbf.pdf

The LED_STRIP is replaced with two LH351B white LEDs connected in series. Datasheet:http://www.samsung.com/global/business/business-images/led/file/product/lighting/201504/Data_Sheet_LH351B_Rev.4.3d.pdf

The forward voltage of the LH351B is approximately 3V, so I am using 6V as my power supply. Basically I want to keep turning the LEDs on and off. I noticed that when I increased the switching frequency, for example 100kHZ, the LEDs were dimmer than what it was when switching at a slower frequency like 1kHz. What could be causing the decrease in brightness?

I believe LED should be able to switch at the nano seconds range? And the MOSFET I am using has rise/fall time at nano seconds as well. Does it have to do with the microcontroller? Below is the code I am using, just a simple digitalWrite using port manipulation:

    PIOD->PIO_SODR = 1<<8; //HIGH on pin 12
    //digitalWrite(12, HIGH);
    delayMicroseconds(10);


    PIOD->PIO_CODR = 1<<8; //LOW on pin 12
    //digitalWrite(12,LOW);
    delayMicroseconds(10);
    }

–EDITED–

full code:

    #include <SPI.h>
    #include <SD.h>
    File dataFile;

    void setup() {
      pinMode(12, OUTPUT);
      Serial.begin(210000);

      if (!SD.begin(10)) {
        Serial.println("Card failed, or not present");
        return;
      }
      Serial.println("card initialized.");
    }

    void loop() {
      int myData_read;
      dataFile = SD.open("DATALOG.dat");
      myData_read = dataFile.read();
      //Serial.println(myData_read);
      for(int i=0; i < 8; i++){
         if((myData_read & (1<<i)) >> i){

            PIOD->PIO_SODR = 1<<8; //HIGH on pin 12
            //digitalWrite(12, HIGH);

            delayMicroseconds(5);
          } else {

            PIOD->PIO_CODR = 1<<8; //LOW on pin 12
            //digitalWrite(12,LOW);
            delayMicroseconds(5);
            }
      }
    dataFile.close();
    } 

Best Answer

You haven't shown us enough of your code, but presumably this fragment is embedded in a loop of some sort, perhaps even an Arduino-specific loop() function.

What this means is that your "off" time is not equal to your "on" time. The "on" time is simply the 10 µs delay, but the "off" time is the 10 µs delay plus the loop overhead — which might include the overhead of exiting the loop() function and then re-entering again.

As you decrease the nominal delay value, that loop overhead becomes a greater fraction of the total period, skewing the duty cycle and therefore the LED brightness.

This will be obvious if you simply look at the output pin with an oscilloscope.


Looking at the "full code" in your edit, there are many ways it could be optimized. I would try something like this:

#include <SPI.h>
#include <SD.h>
File dataFile;
int myData_read;

void setup() {
  pinMode(12, OUTPUT);
  Serial.begin(210000);

  if (!SD.begin(10)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
  dataFile = SD.open("DATALOG.dat");
  myData_read = dataFile.read();
  //Serial.println(myData_read);
  dataFile.close();
}

void loop() {
  for (int i=0; i < 8; i++) {
    if (myData_read & (1<<i)) {
      PIOD->PIO_SODR = 1<<8; //HIGH on pin 12
      //digitalWrite(12, HIGH);
    } else {
      PIOD->PIO_CODR = 1<<8; //LOW on pin 12
      //digitalWrite(12,LOW);
    }
    delayMicroseconds(5);
  }
}

There's no reason to read the file over and over again in the loop, and the>> i in theif statement is completely superfluous.