# Creating a constant current source using an Arduino, BC548 transistor and LM317LZ Voltage Regulator

I'm trying to create a constant current source that can be turned on/off using Arduino's digital output.

I'm supplying the BC548 with a 5V vcc supply from the Arduino. The base is supplied with 5V using arduino's digital output. The output of the BC548 is sent to the input of the LM317LZ to produce a constant current source based on the design on http://www.bristolwatch.com/ccs/LM317.htm

The issue is that the voltage at the Adj pin of the LM317LZ changes with R1. With R1 set to 100 Ohms, the voltage at the Adj pin is 1.25 V as expected, but when R1 is set to 50 Ohms, by adding another 100 Ohms in parallel, the voltage changes to 1.73 V.

Here's the circuit diagram:

• I edited in a schematic using the integrated schematic editor. Bipolar Transistors are usually not represented as a block with three pins, but as transistor with base, collector and emitter – I still kept your notation, so you're not confused Jan 21, 2017 at 22:18
• You probably have more problems than you expect. A constant current source maintains the current at designed value by varying the voltage. If you apply Ohm's law to the load and the current, you will get a voltage that the 317 must supply. If that is more than a couple of Volts (5V in, minus .7 for the transistor, minus 3V for the minimum drop across the 317) then your plan won't work.
– JRE
Jan 21, 2017 at 22:48

In theory it could work. However, the LM317(LZ) has internal capacitors in it's circuit. It is not ment for a PWM signal at its input.

Connecting a Arduino pin directly to the base seems dangerous. That could result in peaks of 40mA through the base and peaks of negative voltages of Vbe.

It is possible to create a current PWM, but that is often done in a different way. A chopper driver for a stepper motor uses some kind of current PWM, and some led controllers use some kind of current PWM.

Here is the same question: Is it OK to PWM a current source?. There are more links in the answers over there to more info and also a schematic.

What is it for ?

The TI and ST datasheets show the performance of the LM317 for input-to-output voltage differences of 3 - 40 volts, so I wouldn't expect it to work with only the 1.5 volts difference you have. You will need a higher input voltage to make it work as you expect.

• The circuit does not have a 1.25 V difference from input to output The 1.25 V is the reference voltage, so represents the lowest voltage you can create between V(out) pin and V(adj) pin. Jan 22, 2017 at 1:47

1. No base series resistor for the transistor, if you try to pull this low with your PWM output you will have uncontrolled current flow into the MCU output pin.
2. The VCE(sat) of your transistor is too large and your quickly run out of voltage to run the LM317LZ. The minimum input to output differential is 3V.

Perhaps as shown below might work for you, though you could select a TO-92 FET.

simulate this circuit – Schematic created using CircuitLab

The voltage on the V(adj) pin will always be the output voltage developed across your load resistance.
You must also work within the limitation of the regulator that a minimum load of 10 mA be present.

Since you are working with a 5 V supply, the highest R2 value you could have is (5 - 3 - 1.25) * I(out) = 60 Ohms, with 0.75 V across it.
Obviously for any value of load < 60 Ohms the voltage will vary, and this is what you measure on the V(adj) pin.
Conversely for any output load voltage greater than 0.75 V you may run out of regulation.

• I've replaced the PWM input by a constant 5V digital output. Could I replace the LM317 with the LM1086, which has a drop-out voltage of 1.5 V? If I want to obtain a constant current of 100 mA across a load of 5 mOhms, I'll need to set R1 to 12.4 Ohms, right? Jan 22, 2017 at 10:15
• You could use the LM1086, but I doubt you need to if you want 100 mA into 5 mOhms (12.5 Ohms is correct). The LM317 would appear to work just fine. Your voltage output is only 0.5 mV giving you about 0.75 V above the dropout voltage limit for the regulator at 5 V input. Jan 22, 2017 at 16:44
• I've modelled the circuit in Multisim with the BC548 transistor, and a 220 Ohms resistor at the base. I'm using an 'Interactive Digital Constant' to switch the transistor. It seems that it takes a very long time for the transistor to switch. I'm not sure if this is real or something wrong with my simulation. Jan 22, 2017 at 19:38
• I'd suggest that you use a FET for the PWM switch. The circuit above works in CircuitLab at 100 Hz, but it does not work beyond 200 Hz. You have to remember that the slew rate of the regulator is limited by internal capacitance values. I'd not expect better than 5 V/mS though I can't find anything in the datasheet directly specifying it. The corner frequency for ripple rejection is at about 2-300 Hz. Jan 22, 2017 at 22:44
• I have replaced the PWM by a constant 5V logic from the Arduino's digital output pins. I'm not sure if the switching time I see with the Multisim is realistic. I've got my model here forums.ni.com/t5/Multisim-and-Ultiboard/… Jan 22, 2017 at 22:57

Here's the circuit diagram:

it can work but will be dicey, as you may not have enough room for the necessary drop-out voltage across LM317.

three suggestions: 1. use a pnp / p-ch to switch; 2. use a ldo; 3. use a r2r opamp.

The issue is that the voltage at the Adj pin of the LM317LZ changes with R1.