Wednesday, April 20, 2022

Widget Wednesday: Vive la Résistance!

First things first:  What is it good for?  In terms of Physics, two interrelated things:

  • Regulating the amount of current in its circuit, and 
  • Absorbing of voltage that would fry the other widget(s) in its circuit.

In more practical micro-controller unit (MCU) and "widget" terms, this amounts to:

  • In digital output mode:  Protecting downstream widgets from excessive voltage/current
  • In digital input mode:  "Is this widget ON or is it OFF right now?"
  •  In analogue input mode:  Protecting the micro-controller from excessive voltage/current
  • Also in analogue input mode:  "If this widget is ON, how much is it actually ON?"

But let's hop back to Physics for a second, because all this ultimately boils down to a straightforward bit of Algebra knowns as Ohm's Law.  The simple version, at least.  In Ohm's Law, voltage is simply what you get when_X_ amount of current flows through a material with _Y_ amount of resistance.  Ohms (abbreviated with the Greek capital letter omega, or 'Ω') are a unit-measure of resistance, as Amperes (abbreviated as "Amp" or just 'A') are the unit of current.

Putting these together with the more familiar Volt ('V'), we get:   

1V = 1A * 1Ω.

Now.  One big, biiiiiig caveat when you're first working with widgets and microcontrollers is that you need to remember is that, while you're working with whole Volts and whole Ohms, currents will measured in mA -- i.e. one-thousandths of an Ampere.  Definitely something that you need to to keep in mind while you're looking at datasheets and doing the math accordingly.  

A typical LED, for instance, will operate at a mere 20mA -- one fiftieth of an Amp.  Fortunately for all our purses, the kinds of resistors we use in bread-boarding go from one Ω up to a million Ω and more -- and they're typically all priced the same.

 

Controlling Output Current/Voltage


As I mentioned, a 5mm LED like this should draw no more than 20mA of current.  (This also happens to be w/in the safe operating range of a micro-controller pin on the Arduino/Boardulno.)  Depending on its colour, it will operate at a voltage level somewhere between 1.8 and 3.6 Volts.  Many electronics tutorials will play it safe by assigning a 220Ω resistor for all colours.  But if you want something brighter, yet still within the safe range, you need to do a little math.  

In this example, our red 5mm "extra-bright" LED has a range of 1.8 - 2.6 Volts and 20mA of current.  By default, an Arduino digital pin in output mode operates at 5V.  So what to do about the excess 2.4 - 3.2 Volts?  

That's mainly where the resistor comes in.  By definition, it "resists" the flow of current and it does this by dissipating electricity in the form of heat.  (Which is why you need to pay attention to the Wattage rating of your resistor, but we'll get to that in a bit.)

Let's say that we want to run this LED something close to its maximum voltage.  So, to stay a bit on the safe side, we shoot for 2.5 Volts, a.k.a. 2V5.  

So, 5V - 2V5 = 2V5.  

That means that the resistor has to dissipate the excess 2V5 while maintaining a 20mA current.  Again, we have to remember that we're operating in fractions of Amperes here and translate that 20mA -- twenty thousandths of an Ampere -- into 0.02A when we plug it into Ohm's law and solve for X.

If 1V = 1A * 1Ω and we know how many Volts and Amps we're dealing with here, we enter our known values and rewrite the equation as:

=> 2.5V = 0.02A * _X_Ω.  
=> _X_Ω = 2.5V / 0.02A
=> _X_ = 125Ω

That's the basic principle of how resistance (and resistors) work within a given circuit.  Easy-peasy-Mac-'n-Cheesy, amirite?

But Ohm's law doesn't have bupkis to say about how that resistance plays out with mass-produced widgets.  Particularly not when we're trying to stick with inexpensive, readily-available, bread-boardable widgets.  That's where wiring configuration comes in.  For our purposes, resistance plays out in three configurations:

  • Resistors in Series
  • Resistors in Parallel 
  • Voltage-divider Circuits 

 

Resistors in Series

With the inexpensive carbon-film resistors we'll be wrangling into breadboards, we may have to fudge the values a bit.  That's because the out-of-the-box values are standardised. By which I mean that they tend to come in multiples of 10, 12, 15, 18, and so on.   

But in the green LED example above, we're looking for 125Ω of resistance.  But, even in the variety-packs that we can buy over-the-counter -- because who wants to pay $10 shipping for a 5-cent part -- that's not an option.  You can settle for 120Ω -- no judgements from me -- or you can edge up on the "ideal" value.

One -- but not the only -- trick to keep in your pocket is to remember that there's no law that says that the 125Ω has to be supplied by a single resistor.  If you have two in your collection that add up to something close to that number, you can wire your way around it if you line them up in series.  Fortunately, when resistors in series are daisy-chained, the math is Grade 1 arithmetic: 

R(Total) = R1 + R2 + R3 + ... + Rn

 


In this example, we have some (unspecified) widget that requires a (fairly high) resistance of 500Ω.  The bad news is that we don't happen to have that value in our stash.  The good news, however, is that we have a whack of 100Ω resistors handy.  We can arrange them on the breadboard so that they form a chain.  The overlapping of the resistors' ends forces the current to hop-scotch through them to complete the circuit.  (Always remember that, if you give current a path of least resistance to GND, it will take it.  This path doesn't allow give it alternatives.)

Safety Warning:  But before moving on, I want to cover the concept of rated Wattage.  Basically, this means that a resistor -- or any widget, really -- has an upper limit in how much voltage and current it can tolerate before Bad Things(TM) start to happen.  That upper limit is measured in Watts -- a.k.a. Volts * Amperes.  

But wait a minute!  Isn't Amperage already part of Voltage?  Aren't we basically talking about Ohms multiplied by Amps squared?  Yes, yes we are.  Ultimately, it doesn't matter how you do the math, though.  All we care about in this case is how close we're running to that resistor's upper tolerance.  (Which, for the record, will be 0.25 Watts for these inexpensive carbon-film resistors.  For whatever widget(s) are in their circuit?  That's on the datasheet, fam.)

In the case of our LED, we're more than good here.  Because 2.5V * 0.02A = 0.05W.  In other words, we're safely running at one-fifth, or 20% capacity.  All good here.  That being said, checking the wattage rating on every single widget before you plug it in and flip the switch is a habit that absolutely cannot be developed too early. 


Resistors in Parallel

LEDs, as long as you swing for the lower end of their operating voltage, tend to be pretty forgiving.  Yeah, lower voltage == dimmer bulb...but at least you know it's working.  Once in awhile, though, you might -- and likely will -- encounter fussier widgets that rely on much more specific resistor values.  If you don't have that specific value on hand, or even values that will add up to that value, you can fall back on a more advanced version of Ohm's law -- one that covers resistors in parallel.  If you haaaaaated the tedium of adding fractions like I did in middle school, you're not going to like this.  Sorry, but them's the breaks.

Resistance in parallel (figuratively and Algebraically) turns resistors-in-series math on its head.  No, really.  Assuming we have any number of resistor values, R1 through Rn, the formula looks like:

1/R(Total) = 1/R1 + 1/R2 + 1/R3 + ... + 1/Rn

Mercifully, we can get the fractions out from underfoot in one shot by re-writing the formula as:

R(Total) = (R1 * R2 * R3 * ... * Rn) / (R1 + R2 + R3 + ... + Rn)

Let's take the same five 100Ω resistors from the "Series" example and re-wire them in parallel on the breadboard (with a little help from jumper-wires):


The resulting resistance, however, is something quite different.  Plugging in the numbers:

R(Total) = (100Ω * 100Ω * 100Ω * 100Ω * 100Ω) / (100Ω + 100Ω + 100Ω + 100Ω + 100Ω)
=> 100^5Ω / 500Ω
=> 10,000,000,000Ω / 500Ω
=> 20,000,000Ω
=> 20MΩ  (Yeah, that's mega-Ohms.)

Wowza.  At 5V, the trickle of current through that circuit will likely be un-measurable to our poor multi-meters.  And, let's be honest, will likely be impractical for basically anything we'd do on a breadboard.  (I'm not an electrical engineer, so I can't speak to whether that would even work on a printed circuit board (PCB).)  But the basic idea is that the difference in scale between in-series resistors and in-parallel resistors is literal multiple orders of magnitude.

But my Gentle Reader gets the point, yes?  Like resistors in series, the resistors in parallel configuration is another tool in your toolbox when you have a limited range of resistor-values handy and none of them are close to what you need for your project.  (In fairness, I will admit that I've never needed to cut it this fine with any project.  YMMV.)  

 

Voltage Divider Circuit

The third and final resistor wiring configuration that I'm going to cover here is a special case known as the voltage divider circuit.  As we've seen, the series resistor configuration adds resistances and the parallel configuration (mostly) multiplies them.  In contrast, the voltage divider configuration is useful for outputting a ratio of whatever current is fed into it. 

In practical terms, that means that (within the limits of the resistors' rated wattage) you can cheaply step down the voltage flowing to whatever widget is downstream in the circuit.  That, bee-tee-dubs, can include output widgets (e.g. LEDs, buzzers, etc.) or input widgets that can measure a range of values read by a micro-controller.  

In either case, the formula is:

R(Output) = R2 / (R1 + R2)

Notice that we're limited to two (vs. a potentially infinite number of) resistors.  But what's super-important to remember is that, unlike series & parallel resistor configurations, the relative values of R1 and R2 make all the difference (unless, of course, they're equal.)  But, for our purposes, equal values of R1 and R2 can still be useful.  

My Gentle Reader might already have crunched the numbers and realised that, when R1 = R2, the output voltage is 2/3 of the original.  Which comes in super-handy when we need to step 5V down to 3V3.  In that case, 1/4W resistors should carry no more than 50mA of current.  But since we already limit ourselves to <= 20mA, this shouldn't be an issue...riiiiiiiiiiiiiiiiiight??????

On the breadboard, a voltage-divider circuit looks something like:  


One final thought is that the actual values of the resistors involved in a voltage-divider circuit don't really matter -- everything boils down to their relative values.  In other words, it's All About the Ratio.  

In the above example, two 100Ω resistors could be swapped out for two 1Ω resistors.  Or two 1Ω resistors, for that matter.  All that truly matters is staying within the resistors' rated Wattage.  But it's what makes a voltage divider circuit so powerful.  For outputted current and, as we'll later explore with variable-value resistors, inputted current.

 

Controlling Digital Input Current/Voltage

The same principles apply here, only the voltage is flowing into the micro-controller unit (MCU) rather than from it.  (A little vocabulary-builder here:  

(Vocabulary Builder:  When a pin on the MCU is powering your widget as an output device, it's called "sourcing" current.  When the widget is sending current to the MCU pin as an input device, it's "sinking" current.)  

Micro-controllers like the Arduino can usually sink more current than they source and still stay within safe operating ranges.  But on the level of individual pins, stick with 20mA for input and output.  Which, naturally, means that you need to double-check the datasheet of each and every widget in your Arduino/Boarduino project.  No more than 5V under any circumstances, and (again) stick with 20mA maximum on any given input pin.  Burn those values into your synapses, tattoo them onto the inside of your eyelids -- whatever does the trick.

That-all being said, let's assume that your input widget -- digital or analogue -- comes in on the cool side of these 20mA@5V thresholds.  Then the same basic math as output applies -- with a twist.  Namely that, even for digital input, there's no law that says that the voltage level must be 5V.  The reason being that, within limits, "digital" is just another word for a micro-controller's digital ON vs. OFF dichotomy.  

With the Arduino, the the bar is HIGH, not high.  Yeah, bad pun.  Sorry-not-sorry.  It turns out that you actually have a little slop.  Which is fortunate because, although the vast majority of widgets that I've encountered (thus far) are cool with 5V, a non-trivial percentage insist upon 3.3 Volts (abbreviated "3V3").  The consensus is that a minimum of three Volts digital input is a safe bet.  After testing that myself with an UNO, I tend to agree.  Yes, you can fudge it down a bit (meaning 2V8 and above), but below that you risk very random results.

 

Controlling Analogue Input Voltage

On the standard Arduino (the UNO, whose Atmel ATMega328-based chip also plugs into a breadboard), six pins are available for analogue input.  In other words, they can measure variable levels of voltage.  When working with the standard Arduino UNO, this means that any voltage between 0V and 5V is translated into an integer number between 0 and 1023. (In the Boarduino universe, there's a titch more flexibility.  But, as my Gentle Reader has heard many times already, this is another topic for another post.)

The upshot is that if you've ever felt like you're just another number, you're in good company.  And I mean that in a good way:  Just scrounging through my stash, I can hang with a delightful, motley, and very useful crew. 




Welcome to the Resistance.  [rim shot]

As input widgets, they can measure things like:

  • Light intensity
  • Weight
  • Moisture / Water Level
  • The presence of gasses like CO2, CO, NH3, etc.
  • Tension along a fixed area

As output widgets, they can:

  • Control how much current goes to another widget
  • Generate heat
  • Generate cold

Some can even be used for inputs or output.  Output tends to be pretty straighforward.  But the point of analogue input is not to tell you whether or not the widget in question is sending an electrical current (or not) to the Arduino's analogue pin; it's  how much voltage it's sending.  We're looking for nuance here, my ducks.

The basic idea is that we have some sort of range.  In the case of the Arduino, it's typically an integer between 0 and 1023 and it's up to us to decide what that number means.  (Hint:  We usually translate into a 0 - 100 percentage, but not always.)

Some, more advanced, widgets (usually "breakout boards") will handle the translation for you right out of the box.  So long as you can verify that their voltage doesn't exceed 5V and their current stays pretty close to 20mA, plugging them straight into the Arduino should be safe.  

But with the more "primitive" widgets, you want to add resistance.  Whether that is resistance in series, resistance in parallel, or a voltage-divider circuit depends upon the widget in question and the values of resistors that you have on hand.  For most purposes, inexpensive 0.25W or even 0.125W carbon-film resistors get the job done.  

But now I'm getting ahead of myself.  Those basic carbon-film resistors -- which are a whole genre unto themselves -- will be the focus of the next installment.  Until then, Tchein ton siault d'beluets!