Archive | LED

# 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…

## Requirements

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

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.

…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")
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:")
end
if selection ~= 2 then
print("Enter I in
units of Amps:")
end
if selection ~= 3 then
print("Enter V in
units of Volts:")
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:")
print("Enter the LED Voltage:")
print("Enter the LED Current in
units of Miliamps:")
print("Enter the amount of LEDs:")
return led_calc(supply, led_vol,
led_curr, quantity)
end
print("1) Ohm's Law")
print("2) LED Resistor Requirement")
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);
}``````