Voltage Divider: Power Supply Monitor

I had a power supply that was suspected as faulty at my office. I put together a voltage divider on an Arduino with an SD card data logger to keep track of the voltage coming out of the unit. See the caveats at the end to learn why this can’t be the only test you do.

It turned out, in this case, that the problem was the UPS the computer was plugged into. The battery was fully charged and had sufficient capacity. But the unit would randomly act like an electrical failure had occurred but would not make the switch to battery power. We experience frequent short power losses at our office (on average more than 1 a week). It is likely that the unit has been damaged by these regular power failures over the years that it has been in place. Fortunately, it is the unit that is shielding the computer from taking the hit on this.

This is one of those blog posts that I’m really writing for myself so that I can come back and find out how I did something if I need to do this again in the future. I’m just inviting the public to read over my shoulder.

Building a Power Supply Monitor

I had not previously worked with monitoring voltage with an Arduino, but I knew it was possible. I also knew that the answer was by using a voltage divider. However, I didn’t know how they worked, so I had a little studying to do. I understand the math behind it, but I still haven’t wrapped my head around what is actually happening electronically. Mostly it is because it works and I haven’t really tried to understand it.

Why Use a Voltage Divider

The need for a voltage divider is so that you can feed a higher voltage into an analog pin of the Arduino without destroying the Arduino. The analog pin can only take 5V in. Any more than that and you will destroy the unit (how much more makes it melt down is unknown by me, but I suspect it wouldn’t take much).

Since I wanted to measure the 12V output of the power supply, I needed to step down 12V to no more than 5V. But I didn’t want to use a voltage regulator that would give me a smooth 5V out. If I did that then I’m not actually monitoring the power supply output. I needed a way to vary the 5V going into the Arduino in relationship to how much the 12V fluctuates.

I could have measured the 5V or 3.3V outputs of the power supply, but I didn’t think those would have as much fluctuation as the 12V might if there was a problem with the supply. While those would have been simpler, and potentially not needed a voltage divider, if the 5V or 3.3V outputs spiked over 5V then it would have destroyed the Arduino. And, since I suspected there was a problem with the supply, I worked under the assumption that they might be spike over their intended voltage.

Voltage Divider Concept

The idea of the divider is that you can feed in 12V (or, in the way I set mine up, anything up to 48V) and it will proportionally divide the supply voltage down to less than 5V. Any fluctuation in the input voltage affects the output voltage. In this way you can monitor voltage drops or spikes safely with a 5V tolerant analog pin of an Arduino.

Vout = Vin * (R2 / (R1 + R2))
Image Credit: SparkFun

The math is fairly simple. As you can see from the image above the output voltage is equal to voltage in times the value of resistor2 divided by resistor1 and resistor2 added together. There are calculators online that can do the math for you based on resistors you have available. The key is that you never want your output voltage to go over 5V. Therefore you should add in a bit of a buffer in case the voltage at your supply spikes for some reason.

In my case I wanted my output voltage to never be greater than 5V. I had a handful of resistors that I measured with a continuity tester. What worked for me was a 33K (which measured out to 32.7K) and a 3.9K (measured at 3.8K) resistor which would give me a 48V input max. I would have liked my tolerance to be a bit lower (20V) so that I felt like I got more resolution on my final reading, but these were the resistors I had handy.

Code

Here’s the code that I used. The original author of the code is T.K.Hareendran that I got from the ElectroSchematics website. I have modified it to work for my project and therefore have taken his name off of it. But it might be beneficial to read his site on how it works or if you want to add an LCD display to the unit.

#include 
#include 

File myFile;

int pinCS = 10;
int analogInput = A0;
float vout = 0.0;
float vin = 0.0;
float R1 = 32700.0; // resistance of R1 (32.7K)
float R2 = 3800.0; // resistance of R2 (3.8K)
int value = 0;

void setup() {
  Serial.begin(9600);
  pinMode(analogInput, INPUT);
  pinMode(pinCS, OUTPUT);
  if (SD.begin()) {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed.");
    return;
  }
  Serial.println("DC VOLTMETER");
}

void loop() {
  // read the value at analog input
  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0; // Analog input reads voltage as a percentage from 0-1024
  vin = vout / (R2 / (R1 + R2));
  if (vin < 0.09) {
    vin = 0.0; //statement to quash undesired reading !
    //analogInput = analogRead(0);
  }
  Serial.println(vin);

  //Create or Open file
  myFile = SD.open("Voltage.txt", FILE_WRITE);

  if (myFile) {
    myFile.println(vin);
    myFile.close();
  } else
  {
    Serial.println("Error opening test.txt");
  }
  delay(1000);
}

Caveat

This is not a perfect solution for several reasons, but it does (potentially) give enough information that can help me know for certain that the power supply is faulty. However, it can't tell me that it is not faulty because this particular power supply has 3 independent 12V outputs. A whole section of the supply can die and not affect the other 2. If I'm testing a good section there still could be 2 others that are faulty.

The problem, in this case, ended up being a bad UPS. However, if I had monitored this power supply for a few days I may still not have caught a true power supply problem. This circuit can really only tell me if the supply is faulty. It can't determine if it is definitively good. And, it may not present as faulty on the output I'm using during the test period. Therefore, there are plenty of ways that this circuit may not detect a faulty supply and it can never convince me that the supply is definitely not faulty. It only tests a small subset of potential power supply problems.

Conclusion

While, as mentioned, it cannot tell me if a supply is absolutely good, it does give me a chance to test the supply under load. That is one of the reasons I have not invested in a particular type of power supply tester---you can't test under load.

This does, however, help explain how a voltage divider can be used and how to read a higher voltage using an Arduino that is limited to 5V input. And, maybe, in the future when I need to remember how to use a voltage divider I will remember that I wrote this down and I don't have to go searching all over the Internet again for the solution.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.