I'm new to Arduino and building circuits, and was wondering if there is a type of component, or some other way to do the following task:

I want to control the resistance of a circuit from my computer linked to my Arduino, presumably using an output analog PIN.

Basically what I have is an LED whose brightness I want to control from my computer. I want to be able to control the resistance of the circuit to limit the electricity going to the LED, and I want to be able to do this from my computer (not physically, with a potentiometer) (unless a potentiometer's resistance can also be controlled from a PIN?).

How can I do this?


1 Answer 1


The easiest way to drive an LED with variable brightness is to use one of the pulse width modulation (PWM) output pins on an Arduino.

A PWM pin takes an input value from 0 to 255 (or is it 0/1023? I don't remember.) and converts it to a ratio of on-time to off-time. At the highest value, the output pin is in the on state, 100% of the time. At the halfway point, the output pin is HIGH 50% of the time, and LOW 50% of the time.

Our eyes average out the flashes and see a variable brightness. It works very well.

If you absolutely must have a variable resistance controlled by a digital value, there are devices known as digital potentiometers. These are variable resistors controlled by digital inputs. For an Arduino, you should think about an I2C or SPI potentiometer, which is controlled by serial port commands. You tell it the resistance you want, and it sets itself to that resistance.

As CrossRoads points out in their comment below, you still need a current limiting resistor to drive an LED from a logic pin, to protect both the LED and the output driver on the Arduino. You want to limit the current to the max current for your LED, and then use PWM to lower the brightness from the absolute max set by the current limiting resistor. CrossRoads explains how to calculate the resistor value.

  • I'll try all of this, thanks for the help!
    – Aidan
    Mar 14, 2019 at 1:32
  • See this example: arduino.cc/en/tutorial/fading Mar 14, 2019 at 1:35
  • Cool, thankyou for that
    – Aidan
    Mar 14, 2019 at 1:39
  • A PWM pin takes an input value from 0 to 255. (analogRead() returns 0-1023). Don't forget a current limit resistor so you don't damage the IO pin, or the LED. (5V - Vf)/.02A = resistor. Say the LED has 2.5V across it when on (measure it, or read its data sheet), then (5V - 2.5V)/20mA = 125 ohm. 20mA with current LEDs will be really bright. 250 ohm or 270 ohm may be plenty bright for the max brightness.
    – CrossRoads
    Mar 14, 2019 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.