Help with simple PICAXE circuit driving LED's using Transistors

jproehl

New Member
Hi all:

I've been struggling with a rather simple project using a PICAXE08M2 to try to randomly light and un-light some LED containing cars on a small HO train layout. The cars need 12v (or at least 9v) to light their LED's, so I thought that using transistors with the base connected to an output pin that the PICAXE could pseudo-randomly switch between high to low to light and un-light them. The cars would be on a 12v circuit that passes through the collector-emitter on a transistor.

I'm a bit new to this electronics stuff, so I might have something mis-wired, but I've attached a circuit diagram and my code trying to drive it. The diagram contains 2 manifestations of the circuit, just look at the left or right half (to the left/right of the 5v and 12v input blocks) that runs either cars #1 and #2 or cars #3 and #4. When I run the code in the simulator, the two pins (C.2 and C.4) switch semi-randomly between high and low as the random variable in the code dictates .. so I believe the code is correct. It my physical implementation that is suspect.

But when I take the chip and put it in my circuit board and look at the voltages relative to ground, C.2 stays high all the time (at essentially the same voltage as the 5v supply) and that car's lights remain lit all the time, while C.4 bounces all over the place (but never seems to drop below 0.7v) and that cars lights stay off. I've tried both having a 10kOhm resistor as a pull down resistor on the two output pins (thinking that maybe the PICAXE sends it low but might not drive it down) and not having anything else connected to those pins (i.e. in this case the PICAXE pin is only connected to the base of the transistor. I've also tied the grounds from the two supplies with the dotted green jumper from the top to bottom grounding rails. I have not bothered putting and diodes across the Collector-emitter of the transistor as the LED lighted cars should have very, very low currents (the wires are like gauge 26 or smaller).

I plan to ultimately use a photo-resistor as an override on the random circuit to turn on all lights if the room goes dark. So you'll see remnants of that code in there, but it's been commented out while I test things.

Anyone see what I'm doing wrong? Thanks a bunch!

Jeff
 

Attachments

premelec

Senior Member
Hi - I haven't looked at your program but looked at your wiring: You have PICAXE pins wired directly into a BJT base - the maximum voltage of Vbe is about .8 volts and will draw way too much current from the PICAXE - you could put a resistor - say 330 to 1000 ohms - from PICAXE to base of transistor and depending on the lamp current that should work... [BTW that's for ALL the PICAXE to transistor base connections]... Take a look at Manual 3 which is all about such things...
 

inglewoodpete

Senior Member
Four points.
  1. Your circuit board graphic does not show the download circuit/s. PICAXE chips MUST have the SerIn pin pulled low for the chip to run its code. That is what the two "download circuit" resistors do for each chip.
  2. Digital outputs toggle between a relatively low impedance Low and High. You do not need pull up or pull down resistors on outputs.
  3. You do not show current limiting resistors for the LEDs. Am I assuming that the "12v" or "9v" have current limiting built in?
  4. As mentioned by premelec above, replace your blue links with resistors in the range 330 to 4.7k ohms. Note that, without those resistors, it is possible that either the PICAXEs or transistors could have been damaged. With a bit of luck, they have survived.
 

westaust55

Moderator
Further to the information provided by Premelec and IWP, here is how to determine the value of resistor to the transistor bases.

For the 2N3904 transistor, the datasheet indicates that the gain when in saturation, that is when switched on ‘hard’ to limit voltdrop from the collector to emitter is about 10.

Therefore as an example if the LEDs in a car draws 20 mA total (= Ic) then the transistor base current must be Ib = Ic / Hfe(sat) = 20 / 10 = 2 mA
VBE(sat) will be at least 0.75 and maybe around 0.85 V. The PICAXE outputs will be about 5V – 0.3V = 4.7 volts. Thus the voltage across a base resistor is 4.7 = 0.85 = 3.85V worst case.

So the base resistor from PICAXE outputs to the transistor bases would need to be:
V / I = 3.85 / (2 *0.001) = 1925 Ohms. Thus a resistor with a value of 1.8 kOhm or to be sure 1.5 kOhm should suffice for an LED current of 20 mA per car/transistor.
 

jproehl

New Member
Hi - I haven't looked at your program but looked at your wiring: You have PICAXE pins wired directly into a BJT base - the maximum voltage of Vbe is about .8 volts and will draw way too much current from the PICAXE - you could put a resistor - say 330 to 1000 ohms - from PICAXE to base of transistor and depending on the lamp current that should work... [BTW that's for ALL the PICAXE to transistor base connections]... Take a look at Manual 3 which is all about such things...
Okay, thanks! I'm very new to this and really appreciate you help - it's a lot to learn!!
 

jproehl

New Member
Four points.
  1. Your circuit board graphic does not show the download circuit/s. PICAXE chips MUST have the SerIn pin pulled low for the chip to run its code. That is what the two "download circuit" resistors do for each chip.
  2. Digital outputs toggle between a relatively low impedance Low and High. You do not need pull up or pull down resistors on outputs.
  3. You do not show current limiting resistors for the LEDs. Am I assuming that the "12v" or "9v" have current limiting built in?
  4. As mentioned by premelec above, replace your blue links with resistors in the range 330 to 4.7k ohms. Note that, without those resistors, it is possible that either the PICAXEs or transistors could have been damaged. With a bit of luck, they have survived.
Okay, thanks.

1) I probably should have picked it up before, but I didn't realize that about the SerIn pin - very good to know. I've had success with chips running without doing this, so I must have just been lucky to have the SerIn pin naturally Low. But I do think that this could explain some other issues I've had in other situations - I'll add it to my lessons learned.
2) Thanks, I just wasn't sure. It didn't seem to make any difference.
3) The LED's have current limiting resistors built into them, I believe they are 330 Ohm resistors.
4) I'll put in the additional resistors on my leads to the base of the transistors as Premelec and you have suggested.

Thanks a bunch! Jeff
 

jproehl

New Member
Further to the information provided by Premelec and IWP, here is how to determine the value of resistor to the transistor bases.

For the 2N3904 transistor, the datasheet indicates that the gain when in saturation, that is when switched on ‘hard’ to limit voltdrop from the collector to emitter is about 10.

Therefore as an example if the LEDs in a car draws 20 mA total (= Ic) then the transistor base current must be Ib = Ic / Hfe(sat) = 20 / 10 = 2 mA
VBE(sat) will be at least 0.75 and maybe around 0.85 V. The PICAXE outputs will be about 5V – 0.3V = 4.7 volts. Thus the voltage across a base resistor is 4.7 = 0.85 = 3.85V worst case.

So the base resistor from PICAXE outputs to the transistor bases would need to be:
V / I = 3.85 / (2 *0.001) = 1925 Ohms. Thus a resistor with a value of 1.8 kOhm or to be sure 1.5 kOhm should suffice for an LED current of 20 mA per car/transistor.

Great, thanks! I can really use this ... it helps me to have specific application of the equations to an example that I'm working on. I read through some other info on computing needed resistance, but it was in enough of a different situation that I couldn't figure how to apply it to my case. Thanks!
 

jproehl

New Member
Thanks everyone. I made the recommended changes to the circuit and when I implement one side of it on a bread board it seems to work great! Since my board isn't to be used as a software download board, I've combined the 22k and the 10k resistors used on download boards into one 32k resistor.

However, I now have some weird behavior when I implement the entire circuit and include the photo-resistors to allow for the dark override of the random part of the code.

What I see, with a lighted room, is that ALL 4 cars turn off and on together!! They do seem to have random timing, but the cars do not operate semi-independently as expected. I did modify the time delays embedded in the code I downloaded into each chip to try to separate the lighting of cars in time. I don't see why they would be tied together, but being a novice there may be some interaction that I don't understand.

- Could having two chips running off the same 5v line introduce coupling between the chips in my circuit?
- Could the lighting/extinguishing of one car cause a voltage drop on either the 5v or 12v lines that would cause all the other cars to light/extinguish immediately thereafter?
- Should I add a capacitor to the 12v line to eliminate any voltage drops?
- Should I double the 100nF capacitor on the 5v line because I'm running 2 chips off the same supply?

So, if anyone has the time/interest, I've posted my slightly revised code and the revised circuit for you to look at again. I would appreciate any input, as this seems to be way beyond me at present. Thanks!
 

Attachments

lbenson

Senior Member
Note that if your light/no light condition is dramatic enough so that it could be interpreted as an on/off condition, you might be able to get by with one 08M2 by attaching the photoresistor to C.3, moving car 3's 2N3904 to C.1, and car 4's to C.0. Just a thought.

That would imply no use of sertxd on C.0.

There should be no inherent problem with running two 08M2s off of the same 5V supply. Typically, 1 100nF cap is recommended for each picaxe, as close to the supply pins as you can get it.

10K would be fine for your serin to 0V connection, or even a direct link. A 10uF reserve cap on your 5V line might help. A cap of that size or greater on the 12V supply might help if you are experiencing glitchiness.

If I were doing this I might want to use a 14M2 with a programming link so I could debug with sertxd and reprogram as needed. Then with a "startup" message, you could see if the picaxe is restarting unexpectedly when you turn something on or off.
 
Top