To set current limit on an LM317 Regulator in constant current mode. 

AmpLimit, originally published in Sam Goldwasser's LaserFAQ, selects pairs of resistors for LM317 and LM338 regulators for specific currents for diode (or TEC) drive, between 0.1mA and 5000mA. It can compile for Windows with GCC or TCC. Sam's compiled it with GCC for Linux so we know that works too.

The new version 1.1 can also calculate a balanced array of ten standard 0.6W metal film resistors, and is an ideal form to use for excellent heat dissipation on a vertical board in a tight space.

This is why you might want to use the program:

That's a laser diode driven by about 152 mA. It was a cheap diode and the 8.2 ohm resistor was convenient, and adequate. Sometimes you need much better precision, and the required current must dictate the resistance used, not the other way round! In a high current system you also need to know how big the resistor must be to safely get rid of its waste heat. For a much more useful laser diode driver, look at the hardware section on this site.

From my original text on Sam's LaserFAQ entry:

In all the variations of laser diode drivers based on three terminal regulators like the LM317, there is the detail of selecting the series resistor. Standard resistor values rarely match exactly. However, two standard resistors can likely get you to within 1 mA of desired current on a range up to 5 amps, in a parallel or series network. But which two will get closest while remaining under the limit? And how large must they be to handle the current they'll take? AmpLimit is a C program to find the best pairing of available resistors for the selected current at maximum power dissipation. The reasoning behind this is that while many of those drivers will preset or even modulate a current, many people will want to do it within a strict, hardwired upper limit, and sometimes 1.25/I just isn't enough information. The code is free to all, and will likely compile on anything that runs C code in a text-based shell or console window.

Even though the standard resistor ranges like E24 or E12 only cover every possible value at low tolerances, metal film resistors are usually sold as 1% regardless of how small the range is from a supplier. This means the coverage is like a net instead of a cloth. Two resistors are chosen by the program on the assumption of infinitely strict tolerance, but this will still deliver pairings whose current limit is very close to the desired current in almost all cases. Of course, the best thing to do is use 1% resistors. If there is a significant error in some rare case, the comparison of wanted and actual currents will show it. In this case, try some current to see if there is a better match to that. There usually won't be because the program tries all possibilities and gives you its best shot anyway. So use the most precise resistors you can get and bear in mind that for very low currents the regulator, rather than the resistors, will determine how accurate the result will be.

Additional notes: 3W resistors at 5% tolerance for E24 ranges are made by Panasonic, Vishay and Yageo. At 5mm width and 15 mm length, they will fit onto a standard space that was originally specified for a 1W resistor.

More details of the new Array10 code in the program.... Resistances below 1R are hard to find. It is better to use 0.6W at higher values, ten at a time, for several reasons:
1. Much higher surface area, so ten of them will easily dissipate 6 watts by silent convection even in a tight space.
2. Much easier to source, cheap, widely used. Even high street shops like Maplin and Radio Shack have good ranges.
3. Low inductance. In low resistance, high power, high speed circuits, a metal film array is better than wirewound.
4. Tight tolerance for accurate current limits. Anything less than 5% is very rare above 0.6W, where 1% is standard.
5. If a resistor fails, the failure of a whole system is much less likely. Maintainance is easier with standard parts.
6. Standard 0.6W metal film resistors cover more possible resistances with E24 and E48 ranges with finer tolerance.
7. An array of ten resistors can use different values for each one, for wide range and high precision with one layout.

The code: AmpLimit.c (5.4 KB)
MD5 checksum for the file: 557D553F1BCCB9D00F34BEBD94BD4D0A

The program, in case you want an executable binary ready to run on Windows, or WINE in Linux: AmpLimit.exe (6.5KB)
MD5 checksum for the file: 46AB1FF0BFCE0EE79B325827D263EEC0

That program is free of any virus or other nasties. It's likely too small to hold one without it being noticed, but if in doubt, grab the source code file and compile that. Either way, test the downloaded file's MD5 to see if it matches the value shown here.