PIC BJT LED - WTF - Page 1
Forum section: electronics / general

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 15, 2020, 05:19:28 pm
content: I want to display an 8bit number in LEDs on a PIC 12F683 which has only five output GPIO pins using simple “multiplexing”. Circuit attached.The theory is that the PIC displays alternating nibbles with four pins (GP0, GP1, GP2, GP4) and a fifth pin (GP5) to take all the bases of the trannies either high (lower nibble) or low (upper nibble).This circuit works in as expected except that the upper nibble displays properly but the lower nibble shows a “shadow” of the upper nibble at about half the intensity effectively displaying the number.The more bits that are on in the upper nibble the less the effect is seen; when three are on, there is no shadow. When I mimic the output of the PIC manually (i.e. connected to the rails) it works with no shadow.It seems to boil down to the fact that when GP5 is high it only reaches 3.9V (with 12mA flowing) and not the expected (by me!) 5V.It doesn’t seem to be programme related but the prog is below. I want to try and do this with jelly bean BJTs just for the fun of it.May I ask for advice please.Code:#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)#pragma config BOREN = ON // Brown Out Detect (BOR enabled)#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode is enabled)#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)void main(void){TRISIO = 0; //TRISIO to outputCMCON0 = 0b00000111; //These two turn-off analogue functions which can mess with RMWANSEL = 0; //processes (Read-Modify-Write).int count = 0;int flash = 50;while(count < 256) //GP3 is input only therefore shift bit3 to bit 4{while(flash > 0){GPIO = (count & 0x7) | ((count << 1) & 0x10);GPIObits.GP5 = 0; //LSBs__delay_ms(5);GPIO = ((count >> 4) & 0x7) | ((count >> 3) & 0x10);GPIObits.GP5 = 1; //MSBs__delay_ms(5);flash--;}flash = 50;count++;}}

author: james_s
author_rank: Super Contributor
timestamp: October 15, 2020, 06:51:54 pm
content: That seems like a very strange way of doing it and much more complicated than needed. Why don't you just use two transistors to control two separate banks driven directly from the four remaining IO pins? Or depending on how much brightness you need, you may get away without any transistors at all, just using the IO pins. There is a technique called Charlieplexing that you can use to get away with even fewer IO pins.

author: cv007
author_rank: Frequent Contributor
timestamp: October 15, 2020, 06:55:23 pm
content: >but the lower nibble shows a “shadow” of the upper nibble at about half the intensity effectively displaying the number.You are putting the pins to the new state before you switch the transistors, so you are briefly seeing the other nibble.1. all leds off2. switch transistor3. set led pins

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 16, 2020, 11:19:28 am
content: Thanks both.The problem is that I have a "module" that contains the LEDs and their eight resistors and I can't split the grounds.I tried the programme change but the shadow remains.

author: spudboy488
author_rank: Regular Contributor
timestamp: October 16, 2020, 12:00:38 pm
content: Maybe add a "blanking" step between? Display 4, all off, display the next 4, all off?

author: Dabbot
author_rank: Regular Contributor
timestamp: October 16, 2020, 12:04:16 pm
content: Quote from: PerranOak on October 16, 2020, 11:19:28 amWould you be averse to using a 74HC595 to drive the LED module?Edit: My bad! I missed where you said:Quote from: PerranOak on October 15, 2020, 05:19:28 pm

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 16, 2020, 02:04:43 pm
content: spudboy488 - tried that, no joy. Thank you.Dabbot - I'm rapidly going off the idea of using BJTs and, as it happens, I do have a 74HC595 but I've never used it. I'll download the datasheet and have a look, cheers.

author: Peabody
author_rank: Super Contributor
timestamp: October 16, 2020, 02:09:52 pm
content: I'm not sure it's possible to get rid of the ghosting, but the best shot would be to make D5 a floating input between nibbles so both nibbles would be off for a time. But I suspect that as D5 changes state, there may be times when both nibbles are on.The other thing is that for the lower nibble you are using NPN transistors as high-side switches, and that makes the emitter of the NPN one diode drop below the 5V rail. That may account for the brightness difference. The solution for that is to move the lower nibble LEDs and their resistors up to be in series with the collectors, and connect the 2N2222 emitters to ground, so the NPNs become low-side switches. But from what you say about the common ground in your module, that may not be possible.On the other hand, for the lower nibble, the base current goes through the LEDs, whereas that's not the case with the upper nibble. So that would tend to make the lower nibble brighter.Anyway, I think the common ground situation on the LEDs is going to limit what you can achieve unless you add something outside to gate things. And actually, something like a 595 is going to take up less room than eight transistors and eight resistors. The 595 would also move almost all the current draw outside the PIC. And finally, with the 595 you wouldn't even need to multiplex.

author: Peabody
author_rank: Super Contributor
timestamp: October 16, 2020, 05:29:24 pm
content: Reflecting further on ghosting, when GP4 is at 5V, then Q1 will begin to conduct when GP5 is higher above ground than the combination of the BE voltage drop of Q1 plus the Vf of the LED. Similarly, Q5 will conduct when GP5 is lower below 5V than the EB voltage drop of Q5. So there is a middle ground between roughly 2.6V and 4.4V when both bits will be lit. As the current increases and the output voltage of GP5 drops, that middle ground will shrink, which would explain why the ghosting is reduced as more bits are lit up.If the PIC were perfect, it would transition between low and high instantaneously, but it isn't perfect, and there is all kinds of inductance and capacitance in the circuit, so the transition takes some time.To eliminate the ghosting, it seems you would have to insert a diode, or perhaps even two diodes, into the base circuit of each PNP transistor to make it turn on only at a lower voltage.If you have one at hand, Dabbot's suggestion of an HC595 seems like the way to go. You need a data pin, a clock pin, and a latch pin at a minimum, and either an output enable or clear pin would be nice to have, but not absolutely essential. And you could drive your LED module directly from the HC595 - no transistors or resistors needed. (I'm assuming the 680R LED resistors are inside the LED module.)

author: james_s
author_rank: Super Contributor
timestamp: October 16, 2020, 08:13:54 pm
content: Add some long delays to slow things waaaaay down so you can see what's actually happening. That ought to narrow down precisely where the issue is taking place.

author: viperidae
author_rank: Frequent Contributor
timestamp: October 16, 2020, 10:30:55 pm
content: 12mA is easy too much to expect to draw from a gpio without a decent voltage drop.The datasheet graph only goes to 4mA, with 4.5V out. With 3x the current, don't be surprised to see 1.5V below your 5v supply

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 17, 2020, 02:54:18 pm
content: Peabody: thank you very much for the analyses. I can see that I hadn’t thought through properly on my quick and dirty solution! I don’t use BJTs much and have hundreds of them sitting around so I fancied using them for something. However, I ignored all the voltage drops and largish current flows.james_s: great idea. It confirms Peabody’s point, I guess, as both were on at the same time even when very slow.viperidae: ah yes I see. It’s all the current that the BJTs let through I suppose.So, this was a learning exercise and I did learn a lot. However I still need the functionality. The 595 is a project for the next step as I’ve never used one and it seems a bit of a cheat ... though I have no problem with that! I wanted to do it with simple “logic” as I thought I had with the BJTs. That got me thinking about actual logic. So, I cobbled (again!) a solution as attached. They’re not really jelly bean components but are fairly ordinary. Anyway, it worked! No ghosting, nothing.Now to try and figure out how to use the 595!

author: nali
author_rank: Frequent Contributor
timestamp: October 17, 2020, 03:02:49 pm
content: Unless this is just a learning exercise, wouldn't it be simpler easier & cheaper just to use a bigger PIC with more IO, rather than messing around with loads of glue logic?

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 17, 2020, 03:40:59 pm
content: Indeed it is a learning thing. The original purpose was to see what registers were actually set at as opposed to what I thought they were set at. I often found that the two were different.The LED module is fine for "normal" PICs but I wanted to see if I could to it for one with very limited pins.

author: ledtester
author_rank: Super Contributor
timestamp: October 17, 2020, 10:40:23 pm
content: In place of the LM393 you could also use a single NPN transistor configured as an inverter, e.g.:https://www.petervis.com/Education/logic-gates/transistor-logic-not-gate-inverter.htmlhttps://youtu.be/V8WIDmS_x9c

author: Buriedcode
author_rank: Super Contributor
timestamp: October 17, 2020, 11:07:47 pm
content: transistor, or LM393, either way you'll need a pullup as the 393 is open collector

author: PCB.Wiz
author_rank: Super Contributor
timestamp: October 18, 2020, 05:07:12 am
content: Quote from: PerranOak on October 16, 2020, 02:04:43 pmDid you try MOSFETS ?P-FETS would remove parasitic current paths, and low threshold N-FETS would be similar to NPN transistors, in all cases, you can eliminate the base resistors, saving 8 parts.

author: Ian.M
author_rank: Super Contributor
timestamp: October 18, 2020, 05:58:16 am
content: It *CAN* be done with BJTs, with one PNP transistor and a 10K base resistor per LED + 680R resistor, with one GPIO driving two PNP emitters. However you need a NPN with a 10K base resistor and a 1K collector pullup to provide an inverted multiplexing signal to the base resistors of half the PNP transistors, the others being driven direct from the multiplexing GPIO.I agree with PCB.Wiz that P-MOSFETs would simplify it, though you still need an inverter to drive half of the P-MOSFET gates, which can be implemented using single gate logic, a N-MOSFET with a pullupon its drain, or any other way that is convenient.However, if this is for the purpose of visualizing register content on low pin count MCUs with no (available) debugger support, a one pin bitbanged simple serial protocol is preferable. Either program a PIC with sufficient pins to decode it and drive the LEDs, or use Roman Black'sShift1 system(non-latched version) which uses a 74HC595 (or a chain of them) +a RC delay as a hardware decoder.Note: Its possible to get latched eight bit data from a modification of his latched seven bit circuit - disconnect RT from the input signal and connect it to Qh' of the last '595 in the chain, then start by clocking through enough '0' bits as fast as possible to get Qh' low, followed by a '1' bit that will eventually set Qh' high and cause the '595 to latch the data after the RT,CT delay, followed by the data bits. After the last data bit, idle (or do something else) till the data is latched.

author: snarkysparky
author_rank: Frequent Contributor
timestamp: October 18, 2020, 01:03:32 pm
content: Check out TBD62783They are P channel high side drivers with level translation built in and logic level drive.

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 23, 2020, 10:06:28 am
content: ledtester: I tried that and it worked fine – saves wasting an op amp!Buriedcode: you’re quite right, of course. I left off the pull-up and wondered why it didn’t work. Then I remembered being “told off” here for not putting them in: I put one in and it worked perfectly.PCB.Wiz: I don’t have enough MOSFETs to do it. They’re quite pricey so I only get them as needed. I asked on another thread about “jelly bean” MOSFETs but there seems to be only one, the 2N7000 and that is an N-channel so no Ps.Ian.M: Thank you very much for this analysis though some was over my head. I’m now thinking that the big problem I have is really the “LED module” and I’d be better off starting from scratch!snarkysparky: Thank you. I looked at the datasheet but I couldn’t really understand what was happening.

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 23, 2020, 04:50:08 pm
content: OK so I redesigned it (attached) without the "LED module".It works pretty well but there was still shadowing. So, I upped the value of R10 until the shadowing went away. This occurred at 150k.However, when more than one of the the LEDs attached to Q1 are on they are not as bright as the others on Q2.The problem is that, I guess, because the voltage on the base of Q1 only gets to 1V (because of the high value of R10) it is not "on" enough to allow sufficient current through. I was so close!Have I understood things properly? Any advice please?Cheers.

author: Ian.M
author_rank: Super Contributor
timestamp: October 24, 2020, 12:21:14 am
content: Not attachedWe cant comment on what you haven't shown us!

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 24, 2020, 09:45:31 am
content: Argh! Sorry. Attached. Many thanks.

author: Ian.M
author_rank: Super Contributor
timestamp: October 24, 2020, 10:15:15 am
content: Hmmm....When Q2 is off, the only source of base current for Q1 is via Q2's LEDs. That's guaranteed ghosting! To fix it either use a separate transistor to invert GP5 to drive Q1 base (preferred option), or use a 2K2 pullup to drive Q1 base, with a Schottky diode, cathode to Q2 collector to turn it off.

author: Dabbot
author_rank: Regular Contributor
timestamp: October 24, 2020, 12:23:25 pm
content: Make R10 a 10K, then connect another 1K between +5 and Q2 collector.

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 26, 2020, 05:10:47 pm
content: Thank you both. The extra tranny worked and the Q2 collector pull-up worked. Brilliant.