# PIC18F various output voltage level

How can I control voltage level on a specific pin in PIC18F? Let's say then I want to send about 0.6V to transistor base. But I cannot use a voltage divider. I need to control output voltage by my PIC18F (PIC18F2525).

• Is this a programming problem? It sounds like you just need a resistor, which is clearly a hardware issue.
– Gabe
Mar 13, 2011 at 7:40
• It is programming issue. I know that I can use PWM in some way. Let's say that I want to achieve a fading LED effect via change the output voltage level (using timer modlue).
– sennin
Mar 13, 2011 at 7:45
• So you need software control over the voltage output. Are you asking how to do PWM? Or do you know how to do PWM and are asking for other solutions?
– Gabe
Mar 13, 2011 at 7:49

You don't actually want to send 0.6V to the transistor base. $V_{BE}$ will vary a good deal between various transistors, so you won't get a well defined current through the transistor if you set it to 0.6V.

The standard way to bias a transistor is by sending a small amount of current through it. Let's assume you're working with a MMBT3904, and want to send 30mA through it. Get the datasheet, and find the graph which relates $h_{FE}$* and $I_C$ (Line 2 is at $25^{\circ}{\rm C}$):

$h_{FE}$ is about 140, so you need to send $214\mbox{ }{\mu}A$ through the base:

$$\frac{I_C}{h_{FE}}=\frac{30\mbox{ mA}}{140}=214\mbox{ }{\mu}A$$

Note that this parameter isn't precisely defined (that's why they used a graph), is temperature dependent (that's why there are three lines in this figure), and varies with $V_{CE} (as indicated by Figure 3). However, it varies a lot less than$V_{BE}$. There are many circuits to perform the task of sending a small current through the base. Basically, every way you can connect resistors to$V_{CC}$, GND, and$V_{EE} (if a negative supply is used) has a name. Wikipedia has a good article, but my college course spent a good two weeks talking about these different modes, so don't be discouraged if you don't get it in the first pass. I'll explain the simplest circuit (fixed bias) here.

The schematic looks like this:

$R_L$ is your load; if you need to connect your load to ground, use a PNP and invert everything.

When you drive pin 5 into a low or high-impedance state, only a leakage current will flow through the collector. According to the datasheet, this value is $I_{CBO}=50\mbox{ }nA$ with $V_{CB} = 30\mbox{ }V$. When you drive it high (say, $3.3\rm V$), there is a voltage established across $R_B$ and the base-emitter junction of the transistor. This is an N-P silicon junction, very similar to a diode. It has a similar voltage characteristic - According to Figure 4 of the datasheet, it will be about $790\rm mV$, but that will vary as we've said before. But $800\mbox{ }mV$ is close enough for our purposes. We need to find the value of $R_B$ that gives a current of $30\mbox{ }mA$:

$$I_C = h_{FE} * I_B = 140 * \frac{ 3.3\mbox{ }V - 0.8\mbox{ }V }{R_B}$$
$$R_B = \frac{140 * ( 3.3\mbox{ }V - 0.8\mbox{ }V ) }{I_C}$$
$$R_B = \frac{350}{I_C} \rightarrow 11.66\mbox{ }k\Omega$$

An $11\mbox{ }k\Omega$ 5%, 2%, or 1% resistor should do nicely!

* - $h_{FE}$ is roughly synonymous with $\beta$, so you may see both values in datasheets. $h_{FE}$ usually refers to the quiescent/DC state of the transistor, while $\beta$ is used when working with small-signal/AC currents. They both denote the ratio of base current to collector current.

Some PICs have built in low resolution DACs that you can use. Check the datasheet!

If you need to supply a digital signal to a transistor base of 0v or 0.6v, use a resistor.

If you want to provide an analogue output, you need to use an external DAC (Digital to Analogue Converter) (some PICs may provide these internally). You can make a simple DAC with a resistor ladder and several digital output pins. The more pins you use, the more bits of resolution you'll get.

If you want to control the brightness of an LED you can rapidly toggle a digital signal and modulate the width of your pulses. This is called PWM, Pulse Width Modulation. Because we humans cannot see the rapid pulses, we perceive them as a continuous analogue output.