Hi! Very new to this, apologies if the answers are somewhere on this forum, I tried searching already but couldn't find anything.
So I wanted to make 2 LEDs flash in alternate intervals - like a police/ambulance siren.
I used the code below, and everything turned out well.
However, my initial attempt at hooking it up (see first 'not working' figure below) ended up with both LEDs flashing on and off at the same time, with the PIN 13 diode flashing a lot less brighter.
The second circuit worked perfectly.
So I'd just like to figure out why the first circuit didn't work as the second did, thanks in advance for any replies
edit: (I also tried it with different pins, after reading that pin 13 is a bit different to the others; same results)
Are you sure those circuits are the right way round. The one you claim works is wrong, you should not share resistors with LEDs. Their is nothing wrong with the circuit you claim will not work.
What do you mean by different grounds? Do you mean different ground pins on the Arduino? If so then they are not different at all. All ground pins on the Arduino are connected together on the board.
The sharing resistor one is ok , if the leds are lit alternatively . As the leds can have slightly varying forward voltage , then it won’t work if you turn both on at the same time ( so good practice is to use seperate resistors as others said)
All those circuits , if wired as shown will work .
( they have to be connected the right way around ). Have a bit more of a play - if you don’t have the series resistor , you will probably kill the led
Grumpy_Mike:
The one you claim works is wrong, you should not share resistors with LEDs. Their is nothing wrong with the circuit you claim will not work.
What do you mean by different grounds? Do you mean different ground pins on the Arduino?
Oh, I didn't know that, thanks.
And yes, I meant 2 different ground pins. Thinking about that now that sounds stupid, lol.
hammy:
The sharing resistor one is ok , if the leds are lit alternatively . As the leds can have slightly varying forward voltage , then it won’t work if you turn both on at the same time ( so good practice is to use seperate resistors as others said)
All those circuits , if wired as shown will work .
( they have to be connected the right way around ). Have a bit more of a play - if you don’t have the series resistor , you will probably kill the led
Hmm, weird. Thanks, I'll try hooking it up again and will see what happens.
Annoyingly, it now works perfectly fine with my original setup. I've never been so angry at two lights before. Wonder what went wrong.
I'm still not quite sure what the deal is with the resistor-LED situation (i.e. why sharing is bad practice) - from what I gather it's because the LEDs likely have different power ratings? Is this correct/incorrect?
jeannly:
I'm still not quite sure what the deal is with the resistor-LED situation (i.e. why sharing is bad practice) - from what I gather it's because the LEDs likely have different power ratings? Is this correct/incorrect?
Put simply the LEDs may not behave identically so they may not share the current equally. So if you set the resistor for say 20mA thinking you're getting 10mA through each LED in fact one LED may have 15mA and the other only 5mA.
It's fine to use one resistor in this case. Normally that's not true, but here, only one led is lit at any time, so there is no problem of sharing current between them.
Now your next challenge. Do it again but with one Arduino pin! You will need two resistors this time...
It's fine to use one resistor in this case. Normally that's not true, but here, only one led is lit at any time, so there is no problem of sharing current between them.
True, but it is bad practice letting a beginner get away with something because he made a mistake but got lucky because the mistake was canceled out by the specific circumstances he was using.
The lesson of each LED requires its own resistor is one anyone can take away where as the complicated special circumstances are much harder to take any learning from.
In addition to what slipstick said, the brightness of the LEDs will depend on if you have one or two LEDs on. In fact if they are two different colours of LED it is possible that if your software turns both on at the same time only one of them will be on because the one with the lower forward voltage drop does not leave enough voltage to turn the other on. As the colours go through the spectrum from IR to UV the forward voltage increases. So a red LED will prevent a green LED from lighting if wired to share the same resistor.
slipstick:
Put simply the LEDs may not behave identically so they may not share the current equally. So if you set the resistor for say 20mA thinking you're getting 10mA through each LED in fact one LED may have 15mA and the other only 5mA.
Grumpy_Mike:
In addition to what slipstick said, the brightness of the LEDs will depend on if you have one or two LEDs on. In fact if they are two different colours of LED it is possible that if your software turns both on at the same time only one of them will be on because the one with the lower forward voltage drop does not leave enough voltage to turn the other on. As the colours go through the spectrum from IR to UV the forward voltage increases. So a red LED will prevent a green LED from lighting if wired to share the same resistor.
Thanks for the clarifications, you guys have been a great help
I reckon that might have been what went wrong then - I probably hooked everything up in series by accident, which explains the flashing at the same time + different brightness (I used two colours).
PaulRB:
Now your next challenge. Do it again but with one Arduino pin! You will need two resistors this time...
I've been thinking about how to go about this for the past hour or so now, to no avail. Is this possible with using just the same components that I used (+ extra resistor), and the Arduino? The only ways I could think of require extra components.
Yes, sourcing Vs sinking current is the interesting lesson. When an Arduino pin is set to OUTPUT & HIGH, it will supply (source) current to an external circuit such as an led, as you already know. When a pin is set to OUTPUT & LOW, it can receive (sink) current from an external circuit. You may be wondering where an external circuit would get this current from, after all, LEDs don't generate current. The answer is: from the 5V pin.
This is a variation of controlling a two color LED with two outputs and a common lead, so I guess a two color LED could be controlled this way except there is n way to turn the LED off - but a good exercise - had to think this one over a couple of times -
Not really the same. All the two colour leds I have seen have either been back-to-back, or common anode or common cathode. Back-to-back might be made to work with one Arduino pin, but even if it did work, a lot of current would be wasted. Common anode or common cathode could not be controlled with one Arduino pin. What you would really need is a anode-to-cathode, and I have never seen one of those.
Looks like I stand corrected - sorry - used some back to back and thinking a bit more used two pins - used some common something (3 lead) on a project over a year ago and was two pins since I needed to have the LED's off part of the time and different colors other times