1
\$\begingroup\$

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();
    } 
\$\endgroup\$
12
  • 2
    \$\begingroup\$ That's pretty small to drive a mosfet gate capacitance \$\endgroup\$
    – Trevor_G
    Dec 23, 2017 at 1:38
  • 2
    \$\begingroup\$ Your IRLML2502 FET has a Gate-to-Source capacitance of 740 pF (typical), from its datasheet. I haven't the energy at this time of night to look up the I/O pin current and work out how quickly it can charge/discharge this capacitance through a 27R resistor and therefore how slow your edges are. (Personal guess: not very slow, doubt they're the cause but you can work it out by looking up the current and transposing Q = CV = it.) Also, how's your wiring for carrying 100 kHz, is it three foot long of spindly wire? And can your LED average a current? Stick 10 uF across the LED, see if it changes. \$\endgroup\$
    – TonyM
    Dec 23, 2017 at 1:46
  • 3
    \$\begingroup\$ No way an LED can switch at nano seconds... for the same reason a standard diode can not do it... most LEDs start topping out around a few kHz or 10s of kHz. Higher speed capable ones will have specs on junction capacitance etc that can help you estimate the peak switching frequencies. \$\endgroup\$
    – MadHatter
    Dec 23, 2017 at 2:55
  • 2
    \$\begingroup\$ How about taking scope traces of the transistor drain, instead of speculating about code or drive strength/capacitance? What model of an oscilloscope you are using during code debug? \$\endgroup\$ Dec 23, 2017 at 5:10
  • 2
    \$\begingroup\$ @TonyM, my extensive experience of dealing with developers of firmware indicates that anyone developing any interface with hardware and not using a scope is wasting everyone's time. A two-channel PC scope (+logic analyzer) is not a big expense nowadays, but it will save a lot of time and nerves. \$\endgroup\$ Dec 23, 2017 at 17:52

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Or, otherwise, try putting the code snippet from the question in a for(;;){} block and see if the behavior is different? \$\endgroup\$
    – vicatcu
    Dec 23, 2017 at 2:03
  • \$\begingroup\$ @Dave Tweed♦ I think you pointed out a serious flaw in my design. The full code (edited above) involves reading a stored binary data on a SD card, and toggle the LED to send 1(on) and 0(off). Every line of code takes some time to execute, causes more difference in the "on" and "off" time. Any suggestion on how to improve the code? (Perhaps I should ask this in a separate post) \$\endgroup\$
    – Wei T
    Dec 23, 2017 at 2:58
  • \$\begingroup\$ @vicatcu Yes the behavior is different! The LEDs are bright again. Avoid going into the loop() function solves the unequal LED on/off time. \$\endgroup\$
    – Wei T
    Dec 23, 2017 at 3:01
  • 1
    \$\begingroup\$ If you need really precise timing, you should drive the LED directly from one of the hardware timers. This will also entail the use of interrupts to make sure that the software updates the timer settings as soon as possible when required. \$\endgroup\$
    – Dave Tweed
    Dec 23, 2017 at 3:07
0
\$\begingroup\$

LED cannot be toggled at such high frequency. It will not get enough voltage in that time interval to fully turn on and hence it remains dim or even off.

\$\endgroup\$
6
  • 4
    \$\begingroup\$ This is simply incorrect. LEDs are not slow devices. \$\endgroup\$
    – Dave Tweed
    Dec 23, 2017 at 1:43
  • 1
    \$\begingroup\$ Real world -- How about switching time due to series resistor, combined with junction capacitances of diode ? Check -> google.co.in/url?sa=t&source=web&rct=j&url=http://… \$\endgroup\$
    – Mitu Raj
    Dec 23, 2017 at 2:34
  • 1
    \$\begingroup\$ Give a square wave input to a Resistor - LED Circuit and gradually increase the input frequency and observe the changes. \$\endgroup\$
    – Mitu Raj
    Dec 23, 2017 at 3:09
  • 1
    \$\begingroup\$ Already discussed and accepted here ---> electronics.stackexchange.com/questions/79373/… \$\endgroup\$
    – Mitu Raj
    Dec 23, 2017 at 3:19
  • 1
    \$\begingroup\$ @MITURAJ Thanks, it gave me some idea on how to improve the current design \$\endgroup\$
    – Wei T
    Dec 23, 2017 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.