Skip to main content
Add Me To Your Mailing List
HomeBlogsRead Blog

Zog's Blog

LED control w/Arduinos - Brightness
By Chris Herzog
Posted on 2/20/2017 7:47 AM

The  Arduino (a family of small programmable microcontrollers) is a great basis for all sorts of cool projects. It's easy to drive LEDs, read simple sensors, drive motors, and do networking (wired and wireless).

Here is another LED brightness control program including links to an Arduino project file containing the source code.

Potentiometer to LED Brightness control - use a potentiometer (or really any A2D input) to drive the PWM (Pulse Width Modulation) settings to control the LED brightness. The code can handle any number of LED outputs and A2D inputs (up to physical limits of course).

Originally designed to support 3 LED outputs and 3 control inputs to allow the control of an RGB string of LEDs for color mixing but could be used any place you want smooth LED brightness control. The code is configured to use analog inputs A0-A2 and PWM output pins 3, 5, and 6 but can be changed to any functionally equivalent pins on your platform.

Two operating modes are supported:

    • If the LED_CONTROL_INPUT pin is low (the pin used for this is configurable in the source code with the #define LED_CONTROL_INPUT and is currently set to I/O pin 9) - just scale the A2D reading directly into a PWM value. This works well but the human eye responds in a non-linear fashion so the high end of the brightness scale uses more than 1/4 of the high end of the A2D input with almost no perceptible brightness change down from full brightness and very rapid change at the lower end of the adjustment range.

 

  • If the LED_CONTROL_INPUT pin is high - lookup a logarithmically-derived PWM value to give more of the A2D range to the bottom end and less to the top end where change are hardly noticeable - this sort of brightness curve is perceived to be more" linear" by the human eye even though it changes slowly at the dim side and much more aggressively at the brightest settings.

 

Changes to the LED_CONTROL_INPUT pin are picked up dynamically and can be changed while the program is running.

Wrap up

This code should be built using the 1.6.7 (or later) version of the Arduino IDE which can be downloaded free at https://www.arduino.cc/en/Main/Software  and were tested using Arduino Pro, Nano, Uno and Mega 2560 boards. Comments are in the code.

Feel free to download the examples, build them, run them, and if you make some useful modifications you'd like to share, drop me a note at  chris.herzog@gmail.com.

Also available on GitHub at github.com/cgzog/LED_pots. If you make a change, just send a pull request and I'll be happy to integrate it.