#241 Yikes! Only ONE GPIO pin but TWO🔴🟢LEDs - Now What? Easy fix!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back yes now this week we've got a bit of a dilemma on my latest projects i've only got one pin one gpio pin it's um a gpio pin it's input output pin and it's the last one and i've got to drive this dual led with it these although there are two pins on this led it's actually two leds in one yes indeed i've got a dual led red and green in this one little tiny led so basically one's connected one way and one's connected the other way so if you put positive on one lead and negative on the other it'll go green and if you swap this power around it will go red ideal for sort of a status indicator isn't it with just two leads except i've only got one pin because there are there's a conventional way of wiring this up and then there's the well i guess the ralph bacon way of wiring up yeah i know are you intrigued come back come back yes we'll go through the circuit which is extremely simple and find out how i did it i want to give a big shout out to jlcpcb and there's a few things i want to bring to your attention first of all as you can see there's industrial 3d printing let's have a closer look at that shall we 3d printing is very easy as it shows you here add your files to their website make a click on which products it is you want it to be made from and then just follow the directions on screen really really easy you can make shapes like these as well without owning a 3d printer yourself you really need to give it a try and to learn more about their 3d printing capability watch the video with the charming naomi who will walk you through exactly how their system works additive manufacturing is just another word for 3d printing now here's the thing that will make you smile purple pcbs yes i've ordered one of these and i've used it one of my projects as you can see here it looks very nice and now they've got multiple colors so yeah choose the color that suits your project the best and don't forget the free assembly for your pcb and it's two dollars for one to four pieces regardless of the color of the board you can't go wrong with that why not have a look at them now now conventionally if you've got a dual led or bicolor led you'd use two pins on the arduino whatever microcontroller you're using in the same way i'm showing in this diagram here so you've got arbitrarily picked 9 and 10 here so when 9 is high so you've set it as an output pin set it to high digital right high set this one as an output pin but set it to low it means current can flow from pin 9 through the top led because obviously current can only go that way through an led down here through the current limiting resistor and to ground via this other pin and conversely when this pin goes high and this one goes low then current goes the opposite way around but lights up the other led great fantastic what could possibly be simpler well there are a couple of flying ointments here not least the fact that you're using two valuable pins to light up you know a silly little led all right maybe not quite so silly the one on my smart heater controller down here is anything but silly it really does tell me what's going on but i wouldn't want to to use up two pins for it but there's another issue if you've noticed the current limiting resistor down here which i've arbitrarily assigned 220 ohms it's safe doesn't work very well because in this bi-color led red and green you will find that the red appears fine at 220 but the green appears dim it doesn't want a 220 it really wants a 180. so what do you do put in a 200 and you sort of overdrive the red a bit make it too bright and the green still isn't bright enough how are you going to put two resistors in here one for that one and one for that one yeah and even if the um the leds themselves needed exactly the same current which they don't your eye says that one color is brighter to you than the other now the textbooks say green is brighter to us than the red whether that's true or not for everybody i don't know but anyway the point is you really want to be able to tweak the individual current going to those leds don't you hmm so the two pin gpio solution is a sort of a a rough and ready halfway house isn't it but i've only got one gpio anyway so how am i going to do it now the code that we're going to be using just to test this led out is as simple as you might think we've defined our led here on gpio9 which happens to be pwm capable in the setup we simply say that led pin is an output pin and then we set it alternatively low and then high with a one second gap in between i might set this to 5000 as we go through the program just to slow things down a bit but that's all it's doing so current either flows into the arduino when it's low or out of the arduino when it's high that's that's all there is to it with a single pin simples so here we have a working flashing led going from red to green which shows both both leds are being switched on and off and you think that's it great done dusted well that was a short video what's ah yes we do have a little bit of a problem here now actually it's half a problem because of this circuit we're running from um you know a wall wart as as this one's running here okay if it's running from wall water and you don't intend running it from batteries ever and let's face it you wouldn't run this project with this board from batteries ever but say your project ran just from a wall wart you can just leave it like this and pretend that there isn't any problem okay but uh there is an underlying um potential problem if you're running some batteries so this is the conventional solution when you've got a single gpio pin a buy or dual color led and you want about to turn each one of those on and off independently all right so you've got vcc at the top you've got ground down the bottom and your gpio pin here so what happens is if you set the arduino gpio pin high it's an output pin remember you set it high as a current can flow from it up to about well if you're really going to push it 40 milliamps but 20 is a max and for an led you don't need anywhere near that current flows out from the gpio pin through this diode because it cannot go through this one it's blocked so it goes from here through here out to here and goes right where's my nearest route to ground well it's down here through the 220 ohm resistor and bang this led is now lit brilliant if however you set this pin low then current from vcc will flow through this 180 ohm resistor into here can't go this way because it's blocked it goes this way now and then it's sunk by your arduino to ground internally and therefore this led lights up brilliant i mean there's two important advantages of this solution one is that you can choose two individual resistors for the red and green so get the brightness or the relative brightness about the same and of course you're only using one pin brilliant so is that it is that the end of the video have we have we got to the end already no because there's a drawback to this particular solution which is recognized so everybody who's done this sort of circuit before knows that there's a potential drawback if you're running on a wall wart it probably doesn't matter but look where the vcc is at the top here that's connected to this resistor 180 ohms yeah fine okay you can get about i don't know 12 14 milliamps through that comes down here sees another resistor okay 220 ohms and it sees ground so even with nothing happening here and no led lit current is still going to flow from vcc through these two resistors to ground now it's not a huge amount of current it's a few milliamps but current is nonetheless flowing through here if you're running off a wall wart you probably don't care it's not worth the time and effort to try and sort this out because what's another few milliamps amongst the many hundreds you're probably using from that power supply but if you're running on battery it's a very different thing altogether isn't it because whilst this part of the circuit is powered up you're going to continually drain the battery with a few milliamps and that's a no no so how can we stop the current flowing anywhere except through the correct led whether it's from here that way or from vcc that way without any current sort of flowing down this leg is there a solution so how do we stop the power leaking to ground that way and ensuring it only goes through the led yes there's a solution to the problem of current flowing from vcc through two resistors to ground and the solution is to put a small mosfet here and a small mosfet here shock horror you're thinking surely not ralph have you gone mad you're going to put two mosfets into this circuit just to stop a few milliamps flowing well a few milliamps and a battery powered project is absolutely required i mean you need to save that so you're thinking not a mosfet surely they're all big and clunky and stop right there stop right there we are talking about something like an si4599 i've used it many times before i've got a whole draw of them over there because they are so useful they have an extremely low on resistance rds on they can carry huge amounts of carrots but like six amps and they are just very very cheap less than a dollar i'll show you in a minute and they are tiny tiny tiny tiny little things so tiny that you have to actually use a tiny little breakout board when you're playing with them on a breadboard or something like that i'll show you in a minute so although we've drawn in two transistors like this it's child's play to use believe me so just bear with me while i describe the circuit the p channel will allow current to flow from left to right this resistor which is connecting the gate of this p channel this way to vcc ensures it stays off if you take this resistor off here you'll find that current starts leaking almost immediately and if you touch the gate with your finger you'll find that current flows an awful lot more it's that sensitive so we need the 100k just to turn it off and no current flows this way it just brings the voltage on the gate high so no current flows the converse happens down here with the n-channel mosfet here we're bringing the gate to ground normally for a 10k resistor the exact values aren't that important but you know of that ilk it just makes sure that this transistor is also switched off great so under normal circumstances without anything happening on the arduino both transistors are off now when the arduino says right i'm going to light up one of these leds it goes right i'm going to set this pin as an output pin and i'm going to set it high so what happens well the current wants to flow down here through this diode because obviously it can't go through this one because it's blocked it goes through here to here it goes where's my nearest route to ground well there's a resistor to oh i'm blocked or is it because this is now high this is also turned on this transistor so there is a route from here through this transistor down to ground great so what happens is current flows exactly as you might expect in this way brilliant it's on but nothing is happening up this end because this remember is still off when this is high this stays high it's already been brought high from this vcc so nothing happens here the current is blocked so when the arduino says right i want to turn the other led on now and i'll bring this low so current can flow into the arduino the vcc goes to this transistor here because is this switched on or not well this is low now so it's brought this leg of the gate here low so yes it is switched on so current will flow through here through the transistor through here through this led and into the arduino voila and nothing can flow down here because this transistor is most definitely off because its gate is not high it's low remember from here so that's it you've now blocked all current paths with that little itsy bitsy si four five nine nine or equivalent and it's child's play to use i think i think it's about time we saw some demos on the breadboard so this is the final circuit then with that mosfet or pair of mosfets in fact all plugged in as you can see red and green yeah i know it's difficult to see actually it's easier looking at the reflection isn't it red and green pulsing on and off every five seconds and um zero current wasted when we're turning the green led on ie the stuff that's being powered from over here let's put the multimeter on there and double check that right so the red leds on and we're taking well just just short of 14 milliamps now the green leds on and zero current is flowing through that resistor for the green led which proves the fact that the mosfet of course is blocking all that current as we'd expected but there we are so that's 100 great now for a battery pad project the led or whatever it is you're driving instead of an led it's only on when it should be on and we're not wasting power through that double resistor to ground great okay this is it right i was just having a look to see how much these were and as you can see from lcsc they're 83 cents a piece but you probably want to buy at least five i would have thought and that little breakout board that i showed you i got closer to home now that's 167 each i've got about half a dozen myself just for playing about with on breadboards i mean they're cheap enough now i have used the a04606 as a drop in replacement works just as well and is even cheaper would you believe okay i think i think we've explored this and we've seen that for a few pennies we can add in this si4599 or any dual mosfet or two individual mosfets if you really want of your choice to make sure that the current flowing through this led is only flowing when you want it to be flowing and not wasted uh in a battery-powered project if you're powering it from a wall wart it doesn't matter so much but even then you might be inclined to think no i want it working properly properly so you might introduce that as well and for the few pennies this one costs it's probably well worth it yeah especially if you can solder that little tiny chip into your circuit and believe me it's it's just not that difficult okay i think we're done and dusted here hope you've learned something i wish i'd done this now in my smart heater control rather than having two individual leds with you know basically three wires going in there but there we are we live and learn and the quiz answer was that stop right there was a hit for the holly's not the spice girls which funnily enough i was thinking of as well there we go okay cool i think we're done so remember comments down below if you've got any questions or comments generally perhaps you wouldn't have done it this way you'd do it another way like using two gpio pins but remember the premise for this was that i only had a single gpio open um and if you liked it don't forget if you get a good thumbs up because youtube like that and it helps my channel and i guess i'll be seeing you in the next video i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching
Info
Channel: Ralph S Bacon
Views: 13,436
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: Yq7KdIC6Xu4
Channel Id: undefined
Length: 17min 26sec (1046 seconds)
Published: Fri Apr 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.