LED power switching power transistor - code hangs?

Hi. I'm pretty sure this is a hardware issue and I haven't been able to solve it. Many thnks.

I am trying to control 8 leds in series (5v) through a hardware switch to illuminate a light box. Power supply is a 5 volt, 1.5 amp 7805 replacement switchmode regulator mounted on a shield powered by the 12v Vin.

It works like this.
Hardware switch ON - leds ON - camera takes a defined number of shots - leds OFF - next sequence.

The camera part and other code is working well.

I have tested the LED switching code using a single led (pin 12 and GND) to test the sequence - led ON, camera shoots 1, 2, 3, ... as defined, led OFF. The code jumps to the next sequence no problems.

However, when I switch all 8 leds ON through a power transistor my code doesn't sequence... zero nothing at all and the LEDs remain on endlessly, until the hardware switch is off.

The problem seems to be associated with switching through the power transistor. It is wired correctly, because it can be switched ON and OFF by the hardware switch, setting pin12 HIGH then LOW.

(The base is connected to the microcontrollers output. The high-current load (i.e. the leds) is attached to its power source, and then to the collector of the transistor. The emitter of the transistor is connected to ground).
Reference
http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads

I have posted the relevant code as minimally as possible to avoid distraction. Note: the camera sequence works well and is a bit irrelevant to the LED power switching IMHO.

#define pin2 2 // hardware switch - 3 positions - different sequences
#define pin3 3 // hardware switch
#define pin12 12 // power transistor - base

void pause() {

digitalWrite(pin12, LOW);

}

void setup() {

pinmode(pin2, INPUT);
pinmode(pin3, INPUT);
pinmode(pin12, OUTPUT);

}

void loop() {

if (pin2 == LOW && pin3 == LOW) {
digitalWrite(pin12, HIGH); // LEDS light up - power transistor BASE

// camera shoot - when finished

digitalWrite(pin12, LOW); // leds should turn off here - but they don't

// next camera sequence - then all stop - no problems here

// hardware switch - pause everything
if (pin2 == HIGH && pin3 == LOW) {
pause();

}
}

The link you provided shows a MOSFET transistor, which is basically voltage controlled and driving the Gate directly from an arduino pin is acceptable.

The terms you use are for an NPN transistor, and needs resistor between arduino and base to prevent the output from overcurrenting.
Current driven into the base-emitter junction controls the current thru the collectoe-emitter junction.
You need a 150-180 ohm resistor to protect the arduino output pin.

Regardless: do the switches change state after this:

// camera shoot - when finished

digitalWrite(pin12, LOW); // leds should turn off here - but they don't

so that the next if () isn't just skipped and goes right to the top of loop to turn the LEDs back on?

// next camera sequence - then all stop - no problems here

// hardware switch - pause everything
if (pin2 == HIGH && pin3 == LOW) {
pause();
}

The solution might be as simple as turning on the internal pullups to make sure switches see a good high:

pinmode(pin2, INPUT);
digitalWrite (pin2, HIGH); // enable internal pullup
pinmode(pin3, INPUT);
digitalWrite (pin3, HIGH); // enable internal pullup

Thanks.

Regardless: do the switches change state after this:

Code:

// camera shoot - when finished

digitalWrite(pin12, LOW); // leds should turn off here - but they don't

so that the next if () isn't just skipped and goes right to the top of loop to turn the LEDs back on?

The pin remains HIGH.

The solution might be as simple as turning on the internal pullups to make sure switches see a good high:

pinmode(pin2, INPUT); digitalWrite (pin2, HIGH); // enable internal pullup pinmode(pin3, INPUT); digitalWrite (pin3, HIGH); // enable internal pullup

I am doing this too.

While the camera code works fine testing with a single led as described. There is no action whatsoever once the LEDs light up. The hardware switch to pause turns LESs off as mentioned.

You have reading the state of the pins in there somewhere too?

Perhaps changing to:

if (digitlRead(pin2) == LOW && digitalRead(pin3) == LOW)
&
if (digitalRead(pin2) == HIGH && digitalRead(pin3) == LOW)

It impossible

to control 8 leds in series (5v)

Each led has a forward drop voltage around 2-3 V.
Split them in parallel with 5 resistors, or use higher voltage source , up to 15 - better 20 V.

2 strings of 4 in parallel from the 12V source would be fine.
Still, having LEDs in series with transistor to turn them should not cause the code to hang.

If there is a resistor, it's not clear, so could be anything - :frowning:

However, when I switch all 8 leds ON through a power transistor my code doesn't sequence... zero nothing at all and the LEDs remain on endlessly, until the hardware switch is off.

You need to show us your schematic, that is what you actually made not some thing you based your circuit on. That is a real schematic, not a physical layout diagram.

I suspect you have not got the right circuit.

Thanks. I will get back soon - have some ideas for the code. It can be simpler. Meantime, will try the circuit suggestions. I want max brightness - Cree 30000mcd LEDs.

OK. I relocated the LED switching out of the main loop and set up a separate void loop for that part of the code - works fine now.

I have a 12v supply that is currently powering the Arduino board and a separate 5v power supply for the LEDs.

The LEDs should be wired in parallel, powered directly from the Vin 12v supply instead of a separate 5v supply for maximum brightness?

Referring to this site http://led.linear1.org/led.wiz for circuit calculation - 12v 3v forward voltage 10 mA forward current 8 LEDs. I've guessed the forward current. As suggested, a pair of 4 LED - 2 x 1 ohm resistors.

The LEDs don't care what voltage as much as they care what current.
I would power the arduino from 5V (direct to the VCC or 5V pin, jumper to Vin), and power 4 pairs of (2 LEDs in series with resistor) from the 12V with NPN transistor such as BC547, 2N2222

+12V-LED-LED-Resistor-collector, emitter to Gnd.
Arduino to 180 ohm resistor to base of transistor.
(12V- (2x3V)-0.7V)/.02 = 270 ohm resistor per series pair. Just under 80 mA capable source needed.

As suggested, a pair of 4 LED - 2 x 1 ohm resistors.

Something wrong there, those resistors are way too small.

Thanks Cross Roads and Grumpy Mike.

I had the same thought about the resistance and ran he wizard again - I get the same values. I'll use cross roads calculation.

I'll use 12v for the LEDs as suggested. I run a stepper and camera off the same shield at 5v. I can tap 12v off the Vin pin.

I used the data sheet this time. This makes more sense.

Solution 1: 2 x 4 array uses 8 LEDs exactly

+12V

R = 220 ohms

R = 220 ohms

R = 220 ohms

R = 220 ohms

The wizard says: In solution 1: each 220 ohm resistor dissipates 198 mW the wizard says the color code for 220 is red red brown the wizard thinks 1/2W resistors are needed for your application together, all resistors dissipate 792 mW together, the diodes dissipate 768 mW total power dissipated by the array is 1560 mW the array draws current of 120 mA from the source.