Projects / Input clamp for a microcontroller pin

Input clamp for a microcontroller pin

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO++

    Wire the test source

    9 V battery across the pot, battery minus to Nano GND.

  2. NANO

    Wire the clamp

    Pot wiper through 1 kilohm to a node, Zener cathode on the node and anode to GND.

  3. NANO

    Wire the readings

    Node through 10 kilohm to A0, wiper through a divider of two 10 kilohm to A1.

  4. NANO

    Upload the sketch

    Serial monitor prints three columns.

  5. NANO

    Turn the pot slowly

    Input rises from 0 to 9 volts.

  6. NANO

    Watch the clamp column

    It follows the input then stops near 3.3 V.

  7. NANO

    Read the Zener current

    It rises as the input rises.

  8. NANO

    Compare with the multimeter

    The numbers should agree within a tenth of a volt.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
9 V battery plusPot outer legTest source up to 9 V
9 V battery minus and pot other outer legNano GNDCommon ground
Pot wiper1 kilohm in series to the clamp nodeSeries resistor limits Zener current
Clamp nodeZener cathode, Zener anode to GND3.3 V clamp
Clamp node10 kilohm to Nano A0Reading of the clamped voltage, resistor protects the pin
Pot wiper10 kilohm to Nano A1, and 10 kilohm from A1 to GNDDivide 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.

CheckExpected
Input below 3 VClamp follows the input
Input 9 VClamp near 3.3 V
Zener current at 9 VAbout 5.5 mA
Zener removedClamp follows the input
Series resistor removedDo 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 →

Ask Lark AI

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.