Introduction: Analog Input Protection Circuit
Analog input pins are a common feature of many microcontroller boards. However, due to the operating voltage of the board, you are limited to a maximum analog input voltage (1.8V, 3.3V, 5V), beyond which you will end up frying your board. But that low voltage range is pretty restrictive if you would like to apply some of the functionality of a microcontroller to a measurement outside that range.
To get around this, you can create an analog input protection circuit to allow you to make measurements on higher voltages, while minimizing the risk of frying your pin or whole board. Here I will go over the design method I used to create a voltage divider and diode clamp to protect an analog input pin on a uC32 microcontroller, but these concepts can be applied to creating a circuit for any board. A basic knowledge of circuit theory and analog components would be helpful in understanding what is going on, but is not strictly necessary to just follow along.
**This Instructable is intended to allow you to approximately measure voltages outside the analog pin's normal range, which may lead to some amount of error in the voltage measurement. Also take care in implementing circuits like these, as you may very well fry your board if you are not careful.**
Step 1: You Will Need...
- A breadboard with a handful of jumper wires
- Set of standard resistor values (I used an Analog Parts kit)
- A resistor color guide or app (I used this app created by Digilent, however there are many available)
- A diode, either switching or rectifying (also in the Analog Parts kit)
- Biasing voltage source (such as a AA battery)
- A bench power supply and digital multimeter (for testing)
Step 2: Determine Your Input and Output Voltages
Before we can do any number crunching to choose resistor values for our divider, we need to determine our constraints. Obviously we are constrained by the maximum voltage of the analog input pin, however we also want our diode to become forward biased before we hit that threshold. That means that we need to have a voltage level that the diode is connected to that will allow it to be forward biased at or below the voltage level we want to protect the pin from. The circuit diagram included illustrates the kind of setup we want.
We are now at a point where we need to be able to provide this particular voltage level to our diode. Diodes are nonlinear components, so the voltage drop across it will vary slightly over a range of inputs, however a general assumption is that there will be a 0.7V drop across it. This means that whatever voltage we choose + 0.7V will be the voltage needed to forward bias our diode, allowing it to protect our analog input pin.
Depending on what you have available, your voltage may be slightly different than mine. I used a single AA battery as the high side, which measured at 1.6V, which would result in an input voltage of ~2.3V seen at the input pin in order for the diode to become active. This is a totally fine value to have as a maximum, since it is well below the 3.3V input maximum for the uC32 analog input pin. The input voltage that I want to step down to this level is approximately 12V, which will be read off of a small DC motor.
Step 3: Determine Resistor Values for Your Voltage Divider
**A quick note, the steps taken to modify these voltages are consistent for various targets. The values we are using here were determined from our constraints, which were the AA battery as our biasing voltage source for the diode and the known voltage we want to measure. Provided you have a dedicated source though, you can change the biasing voltage however you like in order to get different voltage readings at the analog pin (as indicated in the diagram in Step 2)
So now that we have a known target measurement value (our ~2.3V determined in the previous step), and our input value of 12V, we can do a little math to determine the resistor values we will need to satisfy the circuit operating with these voltages. I designed this circuit so the total resistance would be approximately 1 MΩ (similar to the impedance of a typical multimeter).
The blue box in the image above lists our known values, from which we can calculate the resistor values we want with the equations in the green box. The values of R1 and R2 will vary depending on your input and measured voltage values, however it is very likely that they will be quite different from a standard resistor value, so you will need to play with combinations of values to get close to the value. (I would suggest trying to not use more than four resistors for either R1 or R2, since building to the exact values calculated is not critical).
Once you have determined which resistors you will use, measure their resistance values as precisely as you can. This step is important since you will need these numbers to calculate what voltage is actually being measured.
Step 4: Build and Test Your Circuit
In the whiteboard image above I have included the resistor values (to four significant figures) I measured that would satisfy the values of R1 and R2 that were calculated using the equations in the previous step. Again, having the resistors themselves measured to a certain degree of precision is what is important here, not so much that they are exactly matching what you may have calculated for those values.
Now, putting your resistors in series you can test how close to your expectations the circuit works. Using a power supply to provide the voltage, you can use a digital multimeter to measure the voltage at the point between the equivalent R1 and R2 resistors. This will be the voltage seen by your analog pin.
A few connections have been indicated in the bread board picture above. The wires boxed in green are for the input voltage applied (12V), the wires boxed in blue are the leads that our microcontroller will be reading (orange wire to analog pin and black to common ground), and the orange box on the + and - rails will be where the diode biasing voltage source will be applied.
Step 5: Measurements
The image above provides shots of the circuit working for different conditions. The top two include the diode clamp implemented in the circuit at normal 12V operation and at 17.8V (to simulate a potential spike in voltage). The bottom two are pictures of the same(ish) input voltages with the diode portion not connected in the circuit.
While we are clearly reading a voltage that is lower than our expected reading of 2.3V, this still illustrates the diode functioning as a shunt (seeing that range being maintained between 12V and 17.8V). This allows excess current to pass through it, rather than build up at the measurement point and cause an increase in the voltage at that point.
The reading of ~1.9V in the top two images is likely the product of several factors, however it is indicating that the drop across the diode for the voltages we are interested in is lower than our assumed 0.7V. The exact reasons for this are beyond the scope of this Instructable, however we can make an assumption for a compensation factor in order to get more accurate results (given that our expected ~2.3V would be proportional to 12V input).
Step 6: Compensation Factor
As a result of the current in this circuit already being very low (on the order of micro amps), the diode will draw a significant percentage of the current once it becomes even slightly forward biased. This will cause a dip in our voltage measurement proportional to the drop seen across the diode. To make our measurements slightly more accurate for the voltage range we are aiming for, we can measure our output voltage for when the diode is connected and when it is not. Taking the difference of these two gives us this factor, and while it is not exact, it allows us to minimize our error.
For the measurements available in the image in the previous step, my resulting compensation factor is 0.21 V, which will be added to the voltage reading from the analog pin.
Step 7: Results
A final test on our circuit can be done to observe its protection functionality if you have a big enough source (or you can just look at the image above for confirmation of concept). As can be seen from the meters in the image, even with 30V applied to the circuit the output voltage pretty well caps at ~2V (with roughtly 35µA of current). The equation included on the white board gives us a way of calculating the input voltage based on our resistor values, compensation factor, and measured voltage. This will really only reliably provide a result when our input voltage is actually 12V or lower, but that is sort of the point here since we want to protect our board from potential voltage spikes.
Plugging in the measured voltage from the top right image in Step 5, our compensation factor, and resistor values into the above equation, I get a calculated input voltage of ~11.823V (error of ~-1.47%), which is pretty good! As mentioned before, this is not going to be the most accurate data, however you are now able to take your measurements and perform various operations on them on your microcontroller!
We have a be nice policy.
Please be positive and constructive.