PDA

View Full Version : Transistor Affecting Timings?



RossWaddell
- 17th April 2012, 23:50
I need to set precise on/off timings for a blinking LED and I was surprised to see that when I switched to a 2N2222 transistor (with a 10k resistor between the GPIO.0 output and Base) in order to drive 4 LEDs that the overall on/off rate changed (it appears longer). I'm using a 20Mhz oscillator to get the best resolution possible, so am I doing something wrong? If I want a precise duty cycle is the only way by trial and error?



DEFINE OSC 20

@ DEVICE pic12F629, HS_OSC
@ DEVICE pic12F629, WDT_ON
@ DEVICE pic12F629, PWRT_ON
@ DEVICE pic12F629, MCLR_OFF
@ DEVICE pic12F629, BOD_ON
@ DEVICE pic12F629, PROTECT_OFF
@ DEVICE pic12F629, CPD_OFF

CMCON = 7 'Turn off comparators
TRISIO.0 = 0 'Make GPIO.0 pin output

LED_0 VAR GPIO.0 ' Alias GPIO.0 to LED
DUTY VAR BYTE
CYCLE VAR BYTE
STEP_CNTR var byte

LGHTS_ON_MS CON 1410 '36 frames given the oscilattor accuracy
LGHTS_OFF_MS CON 550 '12 frames given the oscilattor accuracy

Cycle = 1
STEP_CNTR = 2

lblLoop:
' Fade in
For Duty = 0 TO 255 step STEP_CNTR
PWM LED_0, Duty, Cycle
Next

' Stay on LGHTS_ON_MS
High LED_0
Pause LGHTS_ON_MS

' Fade out
For Duty = 255 TO 0 STEP -STEP_CNTR
PWM LED_0, Duty, Cycle
Next

' Stay off for LGHTS_OFF_MS
Pause LGHTS_OFF_MS

GoTo lblLoop

End


Also, I'm using PWM to fade in/out the LED to give the appearance of incandescents - does that even work with a 2N2222 transistor?

Ramius
- 18th April 2012, 00:54
I can only assist with the transistor itself which is good to 250 MHZ so at 20 MHZ is well within its frequency limits. If timimg is a concern you may wish to go with the faster 2N2222A. You might also wish to go with a 4.7K, and then consider creating a darlington configuration (collectorof transistor 1 to collector of transistor 2, emitter of transistor 1 to the base of transistor 2, base of transistor 1 to the resistor to the PIC and the emitter of transistor 2 to your LED's). My first guess is that your are not turning the transistor on hard enough. Best, Ed

RossWaddell
- 18th April 2012, 01:28
I think I'm already using a 2N2222A - the writing on the casing says, "MPS2222A". I'll try with a 4.7k first and then look into the Darlington configuration (I assume you're talking about two (2) 2N2222A's, right?)

RossWaddell
- 18th April 2012, 01:57
Neither replacing the 10k resistor with a 4.7k nor a darlington pair (your connections didn't work for me; this (http://www.kpsec.freeuk.com/trancirc.htm) did, though) helped - after about 1.5 minutes they are out of sync.

Dave
- 18th April 2012, 11:44
Out of SYNC?.....

Ramius
- 18th April 2012, 12:46
Not sure what "out of sync" means either. I doubt the transistors are causing much of a problem and the original idea/suggestion was to ensure that the transistors were driven hard enough to light the LED's. A schmatic/drawing would help if possible. Just an idea, maybe using "HPWM" to set your frequency might help? Perhaps even a combination of "HPWM" and "pause"? Yes I was talking about using 2 transistors. My guess is that you are placing your LED's on the emitter side of the transistors and not the collector?

Acetronics2
- 18th April 2012, 13:28
Hi, Ramius

I see 25 pf as emitter capacitance ... ( see datasheet - Philips p.5 )

Also have a look to the used components for the switching time tests ...

Your comments ???

Now ... your delays are quite long ... could you tell what make you call them " false " ???


But looking at your program closer ... The false delays are obvious - but for some µs only @ each loop ...

do not shoot your transistor, but your program lines ... :rolleyes:

Alain

RossWaddell
- 19th April 2012, 02:00
Alain - are you posing your questions to me or to Ramius? This thread was started by me so I don't know what you mean by 'false'. The PAUSEs (approx. 1.5 secs while LED is high, 0.5 secs while low) are the whole point of the circuit - I'm using a PIC to create blinking lights which match an existing model.

Here's the schematic:

http://i188.photobucket.com/albums/z301/RossAWaddell/TOS%20E%20Lighting%20Project/12f629_circuit.jpg

Both PICs are programmed with the same code. For about a minute or so after connecting the power, they both blink their LEDs at the same time but slowly get out of sync (one starts flashing before the other) after about 1.5 minutes. Since I'm trying to create EXACT on/off timings, it looks like if I use a transistor (which I need to do as this circuit will light 4 white LEDs) then I have to do this by trial and error rather than relying on the values in the PAUSE command.

RossWaddell
- 19th April 2012, 04:01
Here's a video which shows how the two LEDs start off in sync (first 10s) and then gradually go out of sync (last 10s - clip has been edited for length)

http://i188.photobucket.com/albums/z301/RossAWaddell/TOS%20E%20Lighting%20Project/th_Circuit.jpg (http://s188.photobucket.com/albums/z301/RossAWaddell/TOS%20E%20Lighting%20Project/?action=view&current=Circuit.mp4)

HenrikOlsson
- 19th April 2012, 06:18
Those look like ceramic resonators. (?) The ceramic resonators aren't that accurate and I bet that the actual frequency differes ever so slightly between the two so your timing drifts. Capacitance in the breadboard may also have an effect on tha actual frequency.

If you really need to keep them in sync then I'd suggest driving both PICs from one oscillator or sync them thru one of the spare I/O's but I suspect that's not physically possible (?). Next suggestion is to use crystals instead of resonators, moving on from there there's almost no end to how much you want to spend to be accurate - temperature compensated?, oven controlled?, rubidium? - you name it.

/Henrik.

EDIT: I looked at a couple of resonators and they had a frequency tollerance of +/-0.5% typically. If you're unlucky and get one at +0.5 and the other at -0.5 (very unlikely but still) the difference between the two is 1%. Your 1000ms delay becomes 995ms on one and 1005ms on the other.

Gusse
- 19th April 2012, 07:39
I agree with Henrik, that syncing PIC's is the easiest way to proceed.
If you use internal 4MHz OSC in master PIC and set GP4 pin to provide Fosc/4 out, then your system will be in sync. Rest of the PIC's will use master PIC clock as reference.

Two totally independent systems are always out of sync, already from the beginning. Difference might be small at the beginning, but....

Charlie
- 19th April 2012, 11:02
Here's a video which shows how the two LEDs start off in sync (first 10s) and then gradually go out of sync

It should be easy to prove to yourself it has nothing to do with the transistor - make the second circuit exactly like the first and observe how EXACTLY the same loss of sync happens.
Having 2 unrelated free running oscillators stay in sync is not a reasonable expectation. (Oscillators 101)

Dave
- 19th April 2012, 11:44
It can be EASY to check the OUT OF SYNC problem by having ONLY 1 of the PIC's with a resonator running and use the OSC output from the one running the resonator connected to the OSC input of one with out a resonator. There by both PIC's will be running at the EXACT same frequency. Unless there is some difference in the code running on each PIC they should not fall out of sync.

Acetronics2
- 19th April 2012, 14:21
Hi,

We also could raise scheme 1 ( unbuffered led ) will produce little heat inside the pic chip... then somewhat disturbing the oscillator ...

.6v loss ( typ. ) @ 10 mA = 6mW ... @135°C / Watt = 1°C !

Alain

rsocor01
- 19th April 2012, 14:57
RossWaddell,

Why don't you use a 16F or 18F PIC series with an internal oscillator. Also, you can drive the 4 LEDs from 4 different ports without transistors. Or with Vcc=5V you can even drive two LEDs per port without a resistor (the PIC has an internal resistance).

You can also try the same timming experiment that you did above with a 16F or 18F PIC, using the internal oscillator, to see what happens.

Robert

RossWaddell
- 19th April 2012, 17:28
I also thought last night about removing the transistor on the 2nd PIC to test the timings and lo and behold - it also goes out-of-sync after a minute or so. I would agree that the most likely explanation lies with the tolerance of the resonators.

To clear up a few misconceptions: I'm not trying to link the two PICS/LEDs. I had used the first schematic to test out the timings (I videotaped the blinking to match the frame rates to my source movie) and was disappointed to see it different when hooked up to the transistor (which turns out to be not relevant). I hadn't accounted for the tolerance of the resonator so either I need to use these specific components when finalizing the circuit or provide a way to tweak the timings during runtime (reading a pot, for example).

Some final questions (I hope):

* Is there a better resonator/oscillator I should use which would be more accurate (higher tolerances)? I was using 20Mhz because I thought the 12f629's built-in 4Mhz wasn't sufficiently accurate. Henrik - you mention this in your post so if you know an oscillator component that would be better please let me know.

* Gusse - I've thought about using the clock output of one PIC to be the input on other PICs but it never seems to work for me. Are saying I can dispense with the 20Mzh external resonator/oscillator, remove the define and just connect GP4 to CLKIN on the other PIC?

RossWaddell
- 19th April 2012, 17:33
Robert - the 12f629 has an internal 4 Mhz oscillator calibrated to 1%. I'd prefer something more accurate hence the external oscillator.

RossWaddell
- 19th April 2012, 17:50
Maybe a 20 Mhz crystal oscillator with capacitors is the way to go?

"Crystals provide accurate timing to within 50 parts per million (PPM)"

If that's true, then that's better than a resonator with 0.5 % accuracy.

rsocor01
- 19th April 2012, 21:19
Maybe a 20 Mhz crystal oscillator with capacitors is the way to go?

"Crystals provide accurate timing to within 50 parts per million (PPM)"

If that's true, then that's better than a resonator with 0.5 % accuracy.

Actually it is a 0.005% accuracy!!!!! Yes, the external oscillator might be the way to go.

Ramius
- 20th April 2012, 01:50
If you are dealing with video and not HD video then you are dealing 29.94 frames per second, a rather low frequency. Two different crystals will never sync as each is producing a sine wave or modified square wave and the start of the pulse (leading edge) will change. You might be better off using the CCP1 and CCP2 outputs and the "HPWM" command if you are working with two different frequencies. Also, the LED in your drawing should be from the emmitter to ground, You can leave R-1 from +5v to the collector. The reasoning is that in an NPN transistor there is current flow from the base to the emmitter (notice which way the arrow points). Hope this helps. Ed

Charlie
- 20th April 2012, 02:43
Maybe a 20 Mhz crystal oscillator with capacitors is the way to go?

"Crystals provide accurate timing to within 50 parts per million (PPM)"

If that's true, then that's better than a resonator with 0.5 % accuracy.
To repeat my earlier post, you will NEVER get them to stay locked in phase using 2 different oscillators - this really is oscillators 101. You are simply wasting your time trying, it is not fundamentally possible. One needs a reference from the other, it's just that simple. There are many ways to do this, including the examples already given.

Gusse
- 20th April 2012, 08:14
I've thought about using the clock output of one PIC to be the input on other PICs but it never seems to work for me. Are saying I can dispense with the 20Mzh external resonator/oscillator, remove the define and just connect GP4 to CLKIN on the other PIC?
What is your realistic accuracy requirement and where does it come from?

If LED blinking was just a trial circuit for your end application, then OK. But if your application will be LED blinking device, then I don't get why internal 4MHz OSC with +-1% tolearance would not be enough. +/-1% means max. 5ns/cycle deviation between two corner oscillators (every 50s, 1s time difference). OK, it is quite much, but everything is relative...

20MHz +/-50ppm means +/-1kHz deviation => an error of 26 minutes per year. Is this OK?
With VCTCXO you can have less than +/-1ppm stability.

If you want to use GP4 as an output, then you need to use internal 4MHz OSC, GP4 is multiplexed to OSC2 so XTAL cannot be used.
With external clock source you can use PIC in EC mode.

RossWaddell
- 20th April 2012, 14:49
Charlie - I'm not trying to lock the two PICs in phase. Please see my earlier post where I tried to clear that up:



To clear up a few misconceptions: I'm not trying to link the two PICS/LEDs. I had used the first schematic to test out the timings (I videotaped the blinking to match the frame rates to my source movie) and was disappointed to see it different when hooked up to the transistor (which turns out to be not relevant). I hadn't accounted for the tolerance of the resonator so either I need to use these specific components when finalizing the circuit or provide a way to tweak the timings during runtime (reading a pot, for example).

Charlie
- 20th April 2012, 19:01
Charlie - I'm not trying to lock the two PICs in phase. Please see my earlier post where I tried to clear that up:
And yet you are using a phase difference to decide the frequency is wrong? Anyway, I'm not sure how to say it any differently. If you want both circuits to be EXACTLY the same, then they need to be locked to each other. If they are not locked to each other, they will NEVER be exactly the same.This is because even hand picked components, and PICs created on the same die will have tiny differences.
Match everything to 0.0000000000000000000000000000000001 percent, (feel free to add more 0's) and they will still eventually drift apart.

languer
- 20th April 2012, 21:28
Is your request then that you want to have the on-time and off-time always the same? Meaning, you want 1_second to be 1_second on Design#1 and also 1_second on Design#2.

Then you must get your reference clock as accurate as you need it (which is what has been mentioned before). This has nothing to do with locking oscillators or anything like that. You want watch#1 and watch#2 to always read the same, correct?

So your task is to determine what does "always read the same" means. If you need them to always be the same , with not tweaks whatsoever, forever and ever then you need to phase lock them - but this is not realistic for watches and doesn't look like it is for your application either. You then need to determine how much drift you can tolerate. Drift will happen, and you have to determine how to deal with it. Watch manufacturers (sorry I keep using watches, but it is an easy timekeeping tool) allow you to "tweak" (adjust) the time for that reason.

You can use a crystal for your reference and pay attention to the drift requirements. The higher the number the more it will drift. You could also look at an RTC to serve you as the "time-keeper". You still may need a means to "adjust/tweak" if you want them to start right-on.

Jumper
- 21st April 2012, 04:55
Hi,

If your goal is to have a LED flash at exactly timed intervals always and forever you could look into connecting your PIC to the AC lines in your house. Most countries have 50Hz (some has 60) and if you can detect the zero crossing you will have a 100 Hz reference signal that you can use for timing purposes.

Then you can run the PIC on any kind of oscillator or x-tal since you only need to get the pic running on that clock source. For the LED flashing part you connect the 100Hz reference signal into a counter-input pin. If your counter is 8bit you then load it with with the value 155 and it will overflow 100 counts later to get the LED flash 1 sec on .. and 1 sec off. If you want it to be 0.1 sec on and 0.1 sec off you load 245 into the counter register.

When you get the counter to overflow your interrupt service routine should reload the counter with the desired value (155 or 245) and toggle the LED. This way the LED is always toggled at really stable intervals and will not drift with time. If you have several similar circuits they will always keep in phase (flash at the same time) as long as they are started at the same time.

The down side is: Main AC voltage is a potential killer and it will fry your circuit or you if you dont connect in a proper way. With a good circuit and enclosed in a plastic box it is usually ok but you need to understand the risks. With an OPTO coupler the risks are lower since most of your circuit will not be high voltage but if the only thing you want to do is flash a led then running the things from main AC should really be feasable. If you on the other hand want to connect a USB to your expensive PC or similar it is less of a great idea!

The up side is that you get a really stable reference that will not drift with time or temperature and is indepedent of any clockspeed you use for running the PIC.

The really good thing is that it is usually easy to find short circuits and bad designs, you only need to look for glowing parts and follow the smoke signals.

Or you can get a off-the-shelf 220V to 5V AC power supply...