Using diodes with microcontrollers is a classic and effective way to manipulate voltage levels, primarily for two purposes: dropping voltage and steering current.
Here’s a detailed breakdown of how to use diodes to achieve different voltage outputs, including the circuits, the theory, and important caveats.
The Core Principle: The Forward Voltage Drop (Vf)
The key to using diodes for voltage control is their forward voltage drop (Vf). When current flows through a diode in the forward direction, it "uses up" a certain amount of voltage.
-
Silicon Diodes (e.g., 1N4001, 1N4148): Have a typical Vf of 0.6V to 0.7V.
-
Schottky Diodes (e.g., 1N5817): Have a lower Vf of 0.15V to 0.45V.
-
LEDs: Have a higher Vf, typically between 1.8V and 3.3V, depending on the color.
By placing diodes in series, their forward voltage drops add up.
1. Creating a Lower Voltage from a Higher Source
This is the most common use case. You have a higher voltage power supply (e.g., 12V) and you need a lower voltage (e.g., 5V or 3.3V) to power a low-power component, like a sensor, but a dedicated voltage regulator is overkill.
Circuit: Diodes in Series as a Voltage Dropper
How it works:
-
The input voltage is 9V from a battery.
-
Each silicon diode drops approximately 0.7V.
-
With four diodes, the total drop is
4 * 0.7V = 2.8V
. -
The output voltage is
9V - 2.8V = 6.2V
.
To get closer to the 5V needed by the microcontroller (MCU), you would need to adjust the number of diodes: (9V - 5V) / 0.7V ≈ 5.7 diodes
. Since you can't have a fraction of a diode, you would use 5 diodes for a drop of ~3.5V (outputting 5.5V) or 6 diodes for a drop of ~4.2V (outputting 4.8V).
Important Considerations for this method:
-
Load Dependency: This output voltage is not regulated. The Vf changes slightly with the amount of current drawn. If the load (your MCU circuit) changes its power consumption, the output voltage will wobble.
-
Power Loss: The dropped voltage is dissipated as heat in the diodes. For example, if your circuit draws 100mA, the power lost in one diode is
P = Vf * I = 0.7V * 0.1A = 0.07W
. This is fine for a 1N4001, but for higher currents, the diodes will get hot and you should use a proper voltage regulator (like an LM7805). -
No Safety Net: There is no over-current or short-circuit protection.
Best for: Low-current, non-critical applications where efficiency and stability aren't important (e.g., powering an old 5V cooling fan from a 12V source).
2. Protecting Microcontroller Inputs from Higher Voltages
This is an essential and highly recommended practice. MCU GPIO pins are fragile and can only tolerate voltages up to their supply voltage (e.g., 3.3V). If you need to read a signal from a 5V device (like an Arduino Nano) with a 3.3V MCU (like a Raspberry Pi Pico or ESP32), you must shift the voltage down.
Circuit: Simple Level Shifting (5V → 3.3V) with a Diode
This clever circuit uses the diode's properties and a pull-up resistor.
How it works:
-
The 3.3V MCU's GPIO pin is configured as an INPUT.
-
The pull-up resistor connects the input pin to the MCU's own 3.3V supply.
-
When the 5V device outputs a HIGH (5V) signal:
-
The diode is reverse-biased. No current flows from the 5V source into the 3.3V pin.
-
The pull-up resistor simply holds the MCU's input pin at 3.3V (HIGH).
-
-
When the 5V device outputs a LOW (0V) signal:
-
The diode is now forward-biased.
-
It pulls the MCU's input pin down to
0V + Vf (0.7V) ≈ 0.7V
, which is safely below the 1.0V threshold the MCU will read as a LOW.
-
This is a cheap, one-way, and effective level shifter for digital signals.
3. Controlling Voltage with PWM and a Diode (Very Basic Idea)
You cannot use a diode alone to create a variable analog voltage. However, you can combine a microcontroller's PWM (Pulse Width Modulation) output with other components (a diode, resistor, and capacitor) to create a simple DAC (Digital-to-Analog Converter).
Circuit: Simple PWM Low-Pass Filter (RC Filter)
How it works:
-
The MCU generates a PWM signal. This is a digital square wave that switches rapidly between 0V and 3.3V.
-
The "Duty Cycle" (the percentage of time the signal is HIGH) determines the average voltage.
-
The RC Low-Pass Filter (Resistor + Capacitor) smooths this rapid switching into a steady analog voltage. The diode is not part of the filtering; it's often added at the output for protection (e.g., to prevent a reverse voltage from being applied back to the MCU pin).
-
The output is a DC voltage proportional to the PWM duty cycle. A 50% duty cycle gives roughly 1.65V, 100% gives 3.3V, etc.
Why the diode is there: The 1N4148 diode protects the MCU pin. If a voltage higher than Vf
is accidentally applied to the "Analog Out" node, the diode will conduct and clamp the voltage seen by the MCU pin to ~0.7V
, preventing damage.
Summary: When to Use Diodes for Voltage
Application | Circuit | Pros | Cons | Better Alternative |
---|---|---|---|---|
Voltage Dropper | Diodes in series | Very simple, cheap | Unstable, inefficient, load-dependent | Voltage Regulator (e.g., LM1117-3.3, AMS1117) |
Level Shifting | Diode + Pull-up | Simple, effective, cheap | One-direction only (high to low) | Dedicated Level Shifter IC (e.g., TXS0108E) for bidirectional |
Output Protection | Diode in series | Protects from reverse voltage | Causes a voltage drop (Vf ) |
MOSFET for "ideal diode" (no voltage drop) |
Final Recommendation:
-
For powering components, avoid using diodes as a primary voltage dropper. Use a voltage regulator for any critical circuit.
-
For protecting inputs from higher voltages (level shifting), the diode + pull-up resistor method is excellent and highly recommended.
-
Always include a series resistor (e.g., 220Ω to 1kΩ) when using a diode directly on a microcontroller GPIO pin to limit current and protect both the MCU and the diode.