Input clamp for a microcontroller pin
Build it step by step
0 of 8 doneWire the test source
9 V battery across the pot, battery minus to Nano GND.
Wire the clamp
Pot wiper through 1 kilohm to a node, Zener cathode on the node and anode to GND.
Wire the readings
Node through 10 kilohm to A0, wiper through a divider of two 10 kilohm to A1.
Upload the sketch
Serial monitor prints three columns.
Turn the pot slowly
Input rises from 0 to 9 volts.
Watch the clamp column
It follows the input then stops near 3.3 V.
Read the Zener current
It rises as the input rises.
Compare with the multimeter
The numbers should agree within a tenth of a volt.
How do you build an input clamp for a microcontroller pin?
A resistor and a Zener keep a signal safe for a 3.3 V pin, and you test with higher voltages in a safe range. You need Arduino Nano, Zener diode 3.3 V (1N4728A), Resistor 1 kΩ, Resistor 10 kΩ × 3, Potentiometer 10 kΩ, 9 V battery, 9 V battery snap with DC jack and Breadboard 830 Tie Points. It takes 8 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 8
Parts list
- Development BoardsBuyArduino Nano × 1
- SemiconductorsBuy
- PassivesBuyResistor 1 kΩ × 1
- PassivesBuyResistor 10 kΩ × 3
- PassivesBuy
- BatteriesBuy9 V battery × 1
- PowerBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| 9 V battery plus | Pot outer leg | Test source up to 9 V |
| 9 V battery minus and pot other outer leg | Nano GND | Common ground |
| Pot wiper | 1 kilohm in series to the clamp node | Series resistor limits Zener current |
| Clamp node | Zener cathode, Zener anode to GND | 3.3 V clamp |
| Clamp node | 10 kilohm to Nano A0 | Reading of the clamped voltage, resistor protects the pin |
| Pot wiper | 10 kilohm to Nano A1, and 10 kilohm from A1 to GND | Divide by two so the Nano can read it |
Code
A 3.3 volt microcontroller input can be damaged by a signal above about 3.6 volts, but many sensors and old circuits output 5 volts or more. The simplest protection is a series resistor and a Zener diode to ground. Below the Zener voltage it hardly conducts and the pin sees the input unchanged; above it, the Zener conducts and holds the node near 3.3 volts while the resistor drops the extra voltage and limits the current. With 9 volts at the input and a 1 kilohm resistor, the Zener carries about 5.7 milliamps, well inside a 1N4728A's rating of around a quarter of an amp at 1 watt. The sketch reads the input through a divider, because the Nano cannot measure 9 volts, and the clamped node through a 10 kilohm resistor, so even if the Zener is missing the Nano pin sees almost no current. The table shows the clamp holding near 3.3 volts while the input climbs. Zener clamps are slowish, so for fast digital signals use a level shifter or a resistor divider instead. Keep the test source to 9 volts or less.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const int CLAMPED = A0; // node after the 1 kilohm and the Zener
const int INPUT_HALF = A1; // input voltage divided by two
const float R_SERIES = 1000.0;
float volts(int raw) { return raw * 5.0 / 1023.0; }
void setup() {
Serial.begin(9600);
Serial.println("input_V,clamp_V,zener_mA");
}
void loop() {
float vin = volts(analogRead(INPUT_HALF)) * 2.0; // undo the divide by two
float vclamp = volts(analogRead(CLAMPED));
float izener = (vin - vclamp) / R_SERIES * 1000.0; // milliamps through the series resistor
if (izener < 0) izener = 0;
Serial.print(vin, 2); Serial.print(',');
Serial.print(vclamp, 2); Serial.print(',');
Serial.println(izener, 2);
delay(300);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
input_V,clamp_V,zener_mA 5.00,2.51,2.50 5.00,2.50,2.50 5.01,2.50,2.51 5.00,2.50,2.50 5.00,2.50,2.51
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Input below 3 V | Clamp follows the input |
| Input 9 V | Clamp near 3.3 V |
| Zener current at 9 V | About 5.5 mA |
| Zener removed | Clamp follows the input |
| Series resistor removed | Do not test, the Zener would overheat |
If it does not work
Clamp reads 0
Check the 10 kilohm to A0 and the Zener wiring.
Clamp reads 5 V or more
The Zener is the wrong way round, stripe goes to the node.
Input reads half
Check the divider multiplies by two in the code.
Readings noisy
Add a 100 nanofarad capacitor from A0 to GND.
Zener gets warm
Check the 1 kilohm series resistor is in place.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the input clamp for a microcontroller pin?
Arduino Nano, Zener diode 3.3 V (1N4728A), Resistor 1 kΩ, Resistor 10 kΩ × 3, Potentiometer 10 kΩ, 9 V battery, 9 V battery snap with DC jack and Breadboard 830 Tie Points.
How long does the input clamp for a microcontroller pin take to build?
About 120 minutes for a beginner maker, in 8 steps.
How do I know the input clamp for a microcontroller pin works?
Input below 3 V: Clamp follows the input. Input 9 V: Clamp near 3.3 V. Zener current at 9 V: About 5.5 mA.
Clamp reads 0: what should I check?
Check the 10 kilohm to A0 and the Zener wiring.
Clamp reads 5 V or more: what should I check?
The Zener is the wrong way round, stripe goes to the node.
Input reads half: what should I check?
Check the divider multiplies by two in the code.
Readings noisy: what should I check?
Add a 100 nanofarad capacitor from A0 to GND.
