Archive | Ohm’s Law RSS for this section

Ohm’s Helper: Scripting My Calculations

So, one day I found myself hunting for my calculator. I had been using the built-in calculator program on my laptop to calculate resistor requirements for LEDs, but I found myself craving the fancy formatting of my fancy graphing calculator. After digging through my backpack and office, I thought to myself “Aren’t you a programmer? Clearly it’s time for a script!”

Oh yes, there will be scripts…


As I opened up gedit and began coding some boilerplate, I thought about what my script should do. There are two main calculations that I find myself doing lately:

My script will automate these calculations. I decided that I’d use the pretty menu input menu over command line switches. Switches are supposed to save you time, but I find that unless I’m using some utility all the time I always end up doing foo -? first. I decided to use Lua for my script; I like Lua’s simple syntax, multiple return values, and first class functions.

The Script

Full source code can be found on my github. Arduino Helper is licensed under the GPLv3.

Functions in my script return two values: a value, and a user-friendly message. If some error is encountered, the value is nil, and the message is the error. If all is well, a number is returned, and an output message to the effect of “You got 3 foos! Huzzah!” is displayed. This message is printed at the end, and the value is largely unused at this time. I figure as I expand the script, a value will be nice to have to pass around to other functions as I add them.

Ohm’s Law

Four functions make up the Ohm’s Law calculations:

local function res(i, v) return v / i, "V / I = " .. v / i .. " Ohms" end local function amp(r, v) if r == 0 then return nil, "R must be greater than 0!" end return v / r, "V / R = " .. v / r .. " Amps" end local function vol(r, i) if r == 0 then return nil, "R must be greater than 0!" end return r * i, "R * I = " .. r * i .. " Volts" end local function ohms_law(i, r, v) if i == nil then return amp(r, v) elseif r == nil then return res(i, v) elseif v == nil then return vol(r, i) else return nil, "i, r, or v must be nil!" end end

Res, amp, and vol are pretty self-explanatory, but ohms_law bears some explanation. Ohms_law takes 3 arguments: i, r, and v. It expects one of these to be nil. This is the value that it is solving for. Depending on which one is nil, it calls amp, res or vol.

LED Resistor Calculation

The LED calculation is handled by one function:

local function led_calc(supply, led_vol, led_curr, quantity) local v = (supply - (led_vol * quantity)) if v <= 0 then return nil, "Insufficient supply voltage!" end return res((led_curr / 1000), v) end

This function is also pretty straightforward: it takes the supply voltage, LED voltage, LED current, and quantity of LEDs, and applies Ohm’s Law to calculate the resistance (by calling res).

It does check to ensure that supply – (led_voltage – quantity) is greater than or equal to zero.

Fancy Menus

…and of course, we have our fancy menu. This is handled by one big function, with two nested functions:

local function director() local function gather_law() local r = nil local i = nil local v = nil print("\nSolve for:") print("1) R") print("2) I") print("3) V") local selection ="*n") if selection ~= 1 and selection ~= 2 and selection ~= 3 then return nil, "Invalid Selection!" end if selection ~= 1 then print("Enter R in units of Ohms:") r ="*n") end if selection ~= 2 then print("Enter I in units of Amps:") i ="*n") end if selection ~= 3 then print("Enter V in units of Volts:") v ="*n") end return ohms_law(i, r, v) end local function gather_led() local supply = nil local led_vol = nil local led_curr = nil local quantity = nil print("\nEnter the Supply Voltage:") supply ="*n") print("Enter the LED Voltage:") led_vol ="*n") print("Enter the LED Current in units of Miliamps:") led_curr ="*n") print("Enter the amount of LEDs:") quantity ="*n") return led_calc(supply, led_vol, led_curr, quantity) end print("Select your calculation:") print("1) Ohm's Law") print("2) LED Resistor Requirement") local selection ="*n") if selection == 1 then return gather_law() elseif selection == 2 then return gather_led() else return nil, "Invalid Selection!" end end

This function handles all menu operations. I felt it would be good to keep calculations and menus separated into separate functions, so that if I want to reuse the calculation functions, I don’t need to worry about refactoring anything. Therefore, I have director, gather_led, and gather_law.

Director requests the user select a calculation to perform, and then returns a call to gather_led or gather_law. The gather functions handle information gathering, then returns a call to the appropriate function. Long story short: the result of the calculation functions are returned to the caller of director.

Learning to Arduino: the LED

…and now for something a little more basic: the LED. Those little blinky lights that keep us up at night. What electronic device is complete without them?

The Basics

LED stands for Light Emitting Diode. Yep, like that. Luckily for us, we don’t need to muck about with I/V curves; we only need to know three things to set up our LEDs: the supply voltage, the LED voltage, and the LED current. These values should be provided on the packaging for your LED, on the vendor’s website, or on the datasheet.

Supply voltage

This is the voltage provided by your power source. If you’re using an Arduino, it will be 5 or 3.3 volts. If you’re using some other power source, it will be the value provided by your power source.

LED voltage

This is the voltage ‘consumed’ by the LED. Say you’re using one 2 volt LED, your supply voltage must be higher than 2. If you’re using two 2 volt LEDs, your supply voltage must be higher than 4. If your supply voltage does exceed the combined LED voltage, none of your LEDs will light up.

LED current

This is the safe operating current for your LED. If your current is low, the LED won’t light. If your current is high, your LED will burn out. It is really easy to destroy your LED, so don’t just do whatever and hope everything will be fine; it won’t be and you’ll be out $0.35.

Wiring it Up

So, you’ve got your supply voltage, LED voltage, and LED current, what now? In order to get our current down to the LED current, we need to use a resistor. In order to calculate what resistor to use, we need to use Ohm’s Law. Specifically we need to calculate the resistance by dividing:

R = (Supply voltage - LED voltage) / (LED current / 1000)

LED current will be given in units of miliamps, therefore we must divide it by 1000 to get the amount in amps. This formula will give you a value in Ohms. For example, I have an LED with a LED voltage of 2.2, and an LED current of 17 miliamps. I will be powering this using a digital output pin on my Arduino Uno, which outputs 5V. Doing the math:

(5 - 2.2) / (17 / 1000) = 164.706

What’s that; you don’t have a 164.706 Ohm resistor? That’s fine, just use one that is close. Pick one that’s a little high, rather than one that’s a little low; better to not have it light up than to burn out. I’m going to use a 180 Ohm resistor.

Now that we know what resistor to use, it’s time to hook it all up. You should wire it up like so:

Note that your LED will have two legs: one will be shorter than the other. The long leg should be hooked up to positive, the short leg should be hooked up to negative. If they are reversed, your LED will be reverse-biased, and won’t light up. This is another good way to potentially destroy your LED.

Open up NetBeans, which I’m assuming you configured for arduino, and create a new arduino project. We’re not doing anything fancy today, we just want our pin to output. Your implementation should look like this:

#include <Arduino.h> extern HardwareSerial Serial; int led = 13; void setup() { pinMode(led, OUTPUT); } void loop() { digitalWrite(13, HIGH); }

Upload your project and, assuming all is well, watch your LED light up!

That Sounds Like a Lot of Math…

Yep. Pretty much. Luckily there are calculators on the internet for figuring these things out. Here’s a nice one that I found while researching this blog post. Feel free to never figure this out on your own again.

Learning to Arduino: the Diode

So, what is a diode? A diode is an electrical component that allows current to flow in one direction, but not another. On an electrical diagram, a diode is represented by this symbol:

The physical diode may have a black band on it, this band corresponds with the band on the symbol. One use for a diode is to protect against the power source being hooked up backwards. Let’s look at an example.

In this diagram, the diode is hooked up backwards. This configuration is said to be reverse biased. In this configuration, ‘no’ current will flow across the circuit. In fact, if the current exceeds the diode’s Maximum Repetitive Reverse Voltage (VRRM), the diode will be damaged. This value can be found on the diode’s datasheet. When I say ‘no’ current flows in this configuration, I actually mean a negligable amount of current flows. More on this in a minute.

In this diagram, the diode is hooked up in the correct direction. This configuration is said to be forward biased. In this configuration, current will flow across the circuit. How much current? Well, according to Ohm’s Law:

1.5 Volts / 10000 Ohms = 0.15 Miliamps

But let’s check just to be sure… If we set our multimeter to amps, and we measure, we get… 0.1 Miliamps… …that doesn’t seem right…

Well, the bad news is that it is just one post into this series, and we’ve already come across an exception to Ohm’s Law. To figure out the current across a diode, we need to use the Shockley Ideal Diode Equation. Do yourself a favor: don’t waste your time trying to figure this out. Just use the Voltage/Amperage curve graphs provided on the datasheet for your diode and save yourself a headache.

Therefore, you will see a somewhat level amount of current flowing through your diode, then suddenly it will shoot up. If this happens, it’s time to consider that you need a different diode.

Learning to Arduino: Ohm’s Law

For a good six months now, I’ve been trying to “get into Arduino”. I’ve been doing this mostly by a) throwing money at it and b) just trying stuff. This has been going about as good as you’d imagine. Reflecting back on when I first tried getting into programming, I think about how much better I got at it when I stopped trying to learn C++ and started learning how to program. Similarly, I’ve been spinning my wheels trying to learn to Arduino, perhaps it’s time to learn how electronics work.

In the spirit of this, I’ll be writing this series of blog posts about this. These posts will serve as reference material for myself so I don’t have to google how a diode works, or whatever. Hopefully it’ll prove useful to others as well. I’m not claiming to be an expert. This is my understanding of things, as I go along. If I say something that’s not true, please let me know!


Volts refer to the difference in electric potential between two points.

Amperes, or Amps, refers to the flow of electrons between two points.

Ohms refers to the resistance to the flow of electrons that some substance has.

Some Context

As you may remember from chemistry class, an ion may be positively (if it has more protons than electrons) or negatively (if it has more electrons than protons) charged. Long story short; atoms and molocules do not want to be positively or negatively charged. They will shed or gain electrons until they have equalized their charge. This positive/negative charge differential creates the Volt. The process of electrons flowing from negative to positive creates the Amp. While in transit, the flow of electrons can be impeded depending on the substance they are traveling through. This process of impediment creates the Ohm.

Ohm’s Law

Ohm’s law states that the current through a conductor between two points is directly proportional to the potential difference across the two points. Two variables are said to be proportional if a change in one always results in a change in the other, and the two changes are always related by the use of a constant. In the case of Ohm’s Law, Amps (I) and Volts (V) are the variables, and Ohms (R) is the constant. The formula for Ohm’s Law is:

I = V / R

Basically, if we have two AA batteries (3 volts) connected through a 10,000 Ohm resistor, then 0.3 miliamps are flowing across the wire.

0.0003 = 3 / 10000

Using this, given two of the values, we can calculate the third. Given Amps and Ohms, we can determine Volts:

V = R * I

Given Amps and Volts, we can determine Ohms:

R = V / I

“No Resistance”

So, maybe you hooked a multimeter up to a piece of copper wire, and measured no resistance. Let’s plug that into the Ohm’s Law formula:

I = V / 0

See the problem? The good news is that in the real world, there is no such thing as “zero resistance”. Even the purest copper wire has some amount of resistance. If your multimeter says zero, just keep turning the knob to finer settings until you get some number greater than 0. If you don’t get there, just call it 0.0001 or somesuch really small number. It may not be exact, but it’ll be good enough. The real world isn’t binary, after all.

Meanwhile, fancy scientists wearing fancy labcoats are trying to create Superconductors with 0 resistance. We’ll let this be their problem.

%d bloggers like this: