# How can one vary the resistance of a circuit using a microcontroller? [closed]

I am trying to have a microcontroller light up a few LED's, all LED's have a forward voltage of about 3V. The issue is that my power supply(which in this mock-up is 9V) will push too much voltage if less than 3 LED's are on at one time, my question then is how can vary the resistance of the circuit or change the circuit so that when the microcontroller changes the number of LED's currently on the circuit doesn't fry?

On the real microcontroller there are extra I/O pins so if those need to be utilized feel free.

simulate this circuit – Schematic created using CircuitLab

Edit: I should also add that I am newer to electronics and may need more explanation on more complex ideas

From the comments made it seems I've made some errors in my assumptions it seems that the diodes would actually be in parallel in the original schematic, this leads me to ask, if I simply changed the power supply to a 3V supply, would the circuit work as I wanted it to? I suspect no, simply because that seems too easy

• Individual current limiting resistor on each LED would be the simplest way to go about this, keeping in mind the individual current limits of the microprocessor's pins and overall current limit. Power supplies shouldn't change voltage or "push too much voltage" if load changes, so maybe there's something different about yours that we should know about?
– vir
Jan 20 at 18:59
• Zener diode shunt in parallel with the whole circuit. Or get the right power supply for your circuit in the first place. Jan 20 at 19:21
• Unfortunately your question as it stands doesn't make much sense. Firstly, I'm not aware of any micros manufactured today which run from 9V - 5V or 3.3V is much more likely. Secondly, if your micro is turning LEDs on & off using its GPIOs, then those LEDs are not in series with each other - so their individual forward voltages don't get added up. Thirdly, each of your individual LEDs needs its own current-limiting resistor - these are the parts which makes sure that you don't push too much current through any of the LEDs. Jan 20 at 19:44
• What does it mean for a power supply to "push too much voltage"? Jan 20 at 19:45
• @Hearth by push too much I meant I was concerned about sending more than 3V through one of the diodes Jan 20 at 22:55

It will be really difficult to run the LED's with just a switch like a BJT or mosfet with PWM. You need at least an RC circuit to control the current with a switch, because even if you do have a 'switch' it would only be a short time before the voltage would exceed the maximum rating of the LED and you would risk buring it out. So the best thing to do would be to use a current limiter such as a resistor at minimum, then use a BJT or mosfet controlled by the microprocessor.

Here is an idea below, but instead of the BJT being driven by the 555, you could use the GPIO from the microprocessor

• Thank you for your response but could you simplify for me what you mean? I'm unfamiliar with a lot of the terms. Additionally I was hoping that the circuit would work without having to manually flip a switch, if that is do-able Jan 20 at 22:50
• The other options require ICs, that is the simple option Jan 21 at 0:19

Firstly, do not use a series connection for the LEDs. Much too difficult to control if LEDS need to be individually switchable.

Second, since you have unused IO pins, configure one IO pin per LED as output, and use the pin to drive the gate of a MOSFET that is connected in series with an LED and current-limiting resistor (R= 9/LED current). Connect each of the series-connected MOSFET+resistor+LED legs in parallel on the 9V supply. Now you have a individually controllable LEDS, as many as you wish.

A 3V supply would not really avoid the MOSFET resistor for two reasons: 1) you need the MOSFET to allow the microcontroller to turn the LED on/off, since most microcontroller do not have enough drive to power an LED without such arrangement. 2) a current limiting resister prevents the current from exceeding the LED's safe operating current/power and vapourizing within a short time. Typical LEDS have a maximum rated forward current of 20mA, so the limiting resister needs to be about (9-2)V/20mA = 350 $\Omega$. Or with a 3V supply (3-2)V/20mA = 50 $\Omega$.