3

I am starting my first project in electronics and I am having a hard time understanding the information available in the 84A's datasheet (pg. 179, Electrical Characteristics)

While drawing the schematic to my project I've not really been able to understand how to calculate the various resistor values for a couple of different situations.

A side, but very important question: is the output current on a pin only defined by what's "ahead" of it? Can I "hurt" the MCU if too much current is being drawn due to low resistor value with a LED like the image below?

With the exception of calculating the resistor for LED case (2V drop and 12.5mA) I don't really know how to approach the remaining ones.

  1. Resistors before input pins

    Online, I've seen people using (with 5V supplies) between 4.7k to 10k Ohms in series with the pushbutton connected to the MCU pin but why? If the pull up is activated isn't already a 50k Ohm resistor in series? Adding a 10k one wouldn't just up the total resistance to 60k?

  2. Resistors for NPN transistor use

    How do I chose the values for my base and collector resistors having in consideration current consumption? Are there guidelines to find a balance?

Outputs

Should I use a MOSFET instead of the NPN transistor?

RoboNewb
  • 33
  • 4

1 Answers1

2

There are a few things to address here:

  1. You seem to have a misunderstanding about current flow, evidenced by asking things like "what's ahead of the pin" and "resistors before pins."

    Imagine you have a battery, a resistor, and an LED. It does not matter if the resistor is on the positive or negative side; it will limit current either way. Electrical current only flows when a complete circuit exists, so it is better to refer to the position of components relative to others using positive/negative, or in this case, source/sink.

    If the microcontroller is more positive than the thing being driven (the LED), then the pin of the microcontroller is said to be sourcing current. The resistor could be between the MCU and LED, or between the LED and ground, and it does not matter. Conversely, if the LED and resistor were connected to positive voltage and the microcontroller providing the path to ground, then it would be sinking current. Again, the serial position of resistor and LED do not matter.

  2. Components can be damaged when too much current or voltage is applied. (These are intrinsically linked: voltage causes current to flow, and current creates heat which damages materials).

    The absolute maximum ratings section of the datasheet shows:

    ATtiny84 Absolute Maximum Ratings

    What this is telling you is that on any input/output (I/O) pin, a maximum of 40 mA is possible before damage may occur. Equally important, the total current may not exceed 200 mA. It's a little confusing how they've written it here, by describing it as "VCC and GND Pins" but it effectively means in total. Thus if you have six pins each operating at 40 mA, resulting in a combined 240 mA total, you will be exceeding the microcontroller rating.

  3. Now that you know the current limitations of the MCU, you have some guidelines for selecting remaining components. Driving the LED with an MCU pin depends on:

    • The supply voltage
    • The maximum current the microcontroller can sink or source
    • The forward voltage drop of the LED
    • The maximum current of the LED
    • How bright you want the LED to be

    Values chosen will have to fall within the constraints of the above limitations. Your supply voltage is 5 V, the maximum current sourced by the MCU is 40 mA. I don't know the characteristics of the LED, but you've alluded to it with a 2 volt drop. (Red and green LEDs typically have a ~2 VF while blue and white are often ~3+.) If you are supplying 5 V and the LED forward voltage is 2 V, then you'll have 3 V remaining across the resistor. If you wish to limit the current to 12.5 mA, you use Ohm's Law to calculate the resistor value (R = E/I, 3/0.0125 = 240 Ω). As you can see, you've correctly calculated the resistor value for your current-limiting resistor on PB0.

    You shouldn't consider the 240 Ω resistor to be a "low" value; "low" and "high" are relative. Consider this: with a 2 VF LED, a 5 V supply, and 20 mA limit on the LED (yes, less than the MCU pin maximum), 240 Ω is practically a "middle" value. 150 Ω could be used for 20 mA, while 3 kΩ could be used for 1 mA.

    In other words, maybe you are comparing the 240 Ω resistor value to other, larger values used in the circuit, and thinking that it is too low by comparison. However, that comparison should not be made because the resistors are doing different jobs with different components.


Now we finally get to your question #1. You didn't provide a schematic of the example you gave, but I'll presume it is roughly:

schematic

simulate this circuit – Schematic created using CircuitLab

You wrote:

If the pull up is activated isn't already a 50k Ohm resistor in series? Adding a 10k one wouldn't just up the total resistance to 60k?

When you configure an MCU pin to be an input, you can specify if it should be pulled high or not; here's the relevant text from the datasheet (§10.1.1):

The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin.

If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin.

The internal pull-up resistor is between 20 and 50 kΩ (according to the datasheet), so you are correct in that adding an additional external resistor simply increases the resistance to the sum. However, consider what happens when you accidentally configure the pin as an output pin and connect it to VCC, even temporarily. Without the external resistor, you would have a short circuit and the resulting current would release the magic smoke in your MCU. Also consider cases such as what is the default state of pins before you program the MCU? Or when a bug occurs and changes pin states unpredictably?


Your question #2:

How do I chose the values for my base and collector resistors having in consideration current consumption? Are there guidelines to find a balance?

BJT NPN transistors work by having a current from base to emitter. There is far more detail than I can possibly cover here, so you should research transistor operation. Essentially you need to provide a small current (from base to emitter) in order for the transistor to pass a larger current (from collector to emitter). The "guidelines" are going to depend again on supply voltage, microcontroller limitations, the current you wish to switch, and the specifications of the transistor. With a 50 kΩ resistor, and treating the transistor BE junction as having a 0.7 V drop, you should expect roughly (4.3 / 50k) 86 µA. DC current gain of a BC337 is somewhere between 100 and 630, so optimistically that would work out to pass a CE current of 54 mA.

Skipping to your last question: Should I use a MOSFET instead of the NPN transistor? Perhaps, depending on what you're trying to do. A MOSFET may be much simpler to implement if all you want to do is switch a relay on and off. But it's not clear what you're trying to do from your schematic, which I'll address in the last part of this monstrous answer...


Looking at your schematic, let's examine what's happening with R8, D2, and K1. (As Marla pointed out in comments, D2 is backward from what you probably want.) Consider 5 volts applied at R8; ignore the connection to Q1. Current then flows both to D2 and K1's coil to ground. The diode is forward-biased (meaning its anode is more positive than its cathode) and conducts, but has a forward voltage drop of ~0.7 V. The coil of K1 will have some resistance, let's say 178 Ω (based on a random relay datasheet). Very roughly, this means about 2.8 mA of current flows through the diode while 3.5 mA flows through the relay coil. (I just quickly simulated this using Falstad to get these values.) Whether that's enough current to operate the relay is not important; what I'm trying to show is that as-depicted, the relay will have some current flowing all the time.

When the transistor is switched on, it provides a third path to ground which will do little more than draw additional current from the power supply and not affect the relay at all (unless the power supply can't deliver current, and... that's well beyond the scope of this answer).

Without making this answer even longer, I recommend you research how to drive a relay with a microcontroller. In particular, also research freewheeling or flyback diodes, because that's what D2 is probably meant to be.

I would expect you probably want something like this:

schematic

simulate this circuit


Hopefully this helps to clarify some of the things you're asking about and some of the concepts you're confused about. Here is a collection of other questions that may interest or help you as well:

JYelton
  • 32,302
  • 33
  • 134
  • 249
  • 1
    Thank you for the edit on the original post and for such complete answer. I do still miss a lot of base knowledge, but you made it easier and clear for me to understand the concepts you are conveying. Besides that you gave me plenty of material to go check out as well, THANK YOU – RoboNewb Apr 18 '22 at 18:02
  • What I am trying to do is: when the output PB1 is at '1', the NPN transistor or the MOSFET would open the normaly closed relay – RoboNewb Apr 18 '22 at 18:12
  • @RoboNewb Most relays should have normally open and normally closed pins. By this I mean that the relay coil doesn't need to be energized "normally" and a circuit element deactivates it. For example, you could wire your load to the NC terminal of the relay, and implement a logic 1 (to activate the transistor) to energize the coil, which would open the NC connection. – JYelton Apr 18 '22 at 18:15
  • @RoboNewb The relay I drew in schematic is only SPST for simplicity, but check your physical relay. It's very common for them to be SPDT. If you still need to adhere to a logic 1 in firmware being used to deactivate a normally-energized relay, I highly recommend asking that as a new and separate question. Good luck! – JYelton Apr 18 '22 at 18:18
  • The max of 200mA between Vcc and GND is weird. I mean, it's not all IO pins is it? There's some unknown extra share used by the MCU itself which we're not being told how to estimate. – Dan Sheppard Apr 18 '22 at 18:28
  • Just a last quick question, if I do follow up with the NPN, the problem with the 50k resistor is that the 54mA CE current is probably not enough to operate the coil correct? – RoboNewb Apr 18 '22 at 18:35
  • @DanSheppard my opinion isn't worth much but the way Jyelton explained made sense, I guess it's just poor phrasing? – RoboNewb Apr 18 '22 at 18:51
  • 1
    @DanSheppard Yes, that total probably does include internal usage by the MCU, which is expressed in table 20-1. This just means you need to leave some headroom for the chip internals as well. For example, if you're driving ten LEDs at 20mA each, if there's a chance they could all be on at the same time, it might be best to re-think the design! – JYelton Apr 18 '22 at 20:34
  • @RoboNewb The current to operate the coil will depend entirely on the relay in question. The one I used as an example should operate with ~28 mA. If your relay is bigger or has larger contacts, it likely will also have a larger coil and thus require more current to operate. It's worth noting that the initial current to switch it will be higher than the current to sustain it. (But the relay datasheet would be needed for even approximate numbers.) – JYelton Apr 18 '22 at 20:40
  • @RoboNewb One last suggestion for you: Research "saturation region" for BJT NPN transistors. Operating them thusly makes them behave more like a switch and less like an amplifier. I can't really suggest specific actions or components (like an N-channel MOSFET) without knowing more about your components' needs. You're welcome and encouraged to ask a new/separate question about more specific aspects of your project as you encounter them. – JYelton Apr 18 '22 at 20:43