loading

The reason I'm making this is because I couldn't find any really helpful information on how to fix the code for my voltage sensor. Arduinos have built in voltage sensors. Unfortunately, they only support voltages of 0-5V. This module allows you to measure voltages of 0-25V by presenting a lower voltage to the arduino for measuring. After you have this value you simply feed it through some math and you get your actual voltage. Don't ask me how this math works. I don't know. If you do know however, please share. I'm really just editing the example code from the seller so that it will display decimal values instead far less useful int values.

To start you need to wire it up. It's extremely easy as it only needs 3 wires.

Plug + into 5V, - ground and S into an analogue pin. I have removed all but the relevant pins in a pinout of the arduino nano. If you're using another model then you'll have to figure them out on your own. Any analogue pin will do. As far as I am aware at least. Once you have done this you're ready to move on to the software.

Step 1: Fixing the Code

The example code I got from the seller would only output a very limited integer value for the voltage. When you're trying to tell the actual voltage of what you're measuring though it isn't all that helpful. Because of this I fixed the code a bit so that it would give me a float value. In the photos you can see the edits I have made. I will also list the edits below. Keep in mind that the analogue pin will need to be changed depending on which pin you attach the S pin to.

This is the original code.

#include <Wire.h>
int val11;

int val2;

void setup()

{

Serial.begin(9600);

Serial.println("Emartee.Com");

Serial.println("Voltage: ");

Serial.print("V");

} void loop()

{

float temp;

val11=analogRead(1);

temp=val11/4.092;

val11=(int)temp;

val2=((val11%100)/10);

Serial.println(val2);

delay(1000);

}

This is the edited code.



#include

int val11;

float val2;

void setup()

{

Serial.begin(9600);

Serial.println("Emartee.Com");

Serial.println("Voltage: ");

Serial.print("V");

}

void loop()

{

float temp;

val11=analogRead(1);

temp=val11/4.092;

val2=(temp/10);

Serial.println(val2);

delay(1000);

}



I have also included the finished code in case anyone wants it.

<p>ok, so you have a nice setup there to read voltages. Have you ever thought of publishing it realtime to a very very basic webpage so that you can monitor say, a solar panel battery bank remotely? that right there is something id be interested in!</p>
where i find the fact that 4.092 is a valid number for voltage sensor?
<p>val2=(temp/5); //Not 10 Why</p>
Instead of buying a voltage divider, you could have just made it with two resistors.
<p>your right.. I think they are confused... </p><p>In fact it only divides it to bring it down to a level more tolerable to arduiinos, its not a true voltage divider &amp; it cant use it as a divider to bring 3.3 to the arduino from 5 </p>
<p>Bro... I bought them for $1. Also you underestimate my level of lazy. Normally I would agree with you but some times there are things better off just bough than built. This is one.</p>
<p>I need 0-60 volts DC measured (48 volt nominal battery bank). I've ordered some of these sensor boards (5 for $3) to play with coding before upping the voltage (going to use 12 volt batteries to start). I hope that I can replace the SMT resistors with proper values - however, not sure the board can actually handle 60 volts DC? I've replaced such small SMTs before - but have not actually had a board in-hand. </p><p>Any suggestions?</p>
<p>Well what you're actually doing is dividing the voltage. Part of it is being dumped to ground through one transistor and the other part is being sent to the measuring device. As long as the resistors can handle the voltage you can divide anything. The issue is whether or not your device can measure it. In this case, unless you ordered ones that say they handle a 0-60V range then they won't bring 60V below the 5V measuring range of the arduino. What your'e doing with the dividers is similar to the map method in arduino. You're turning a 0-60V range into a 0-5V range that the arduino can read. If you feed too much power into the arduino you'll fry it, and anything over 5V won't be detectable by the arduino anyway. It will read as 1023 on the analogue pin.<br><br>If you can't find an off the shelf solution then your only option might be to build your own. It is actually really easy. I use this site all the time.<br><a href="http://www.raltron.com/cust/tools/voltage_divider.asp" rel="nofollow">http://www.raltron.com/cust/tools/voltage_divider....</a><br>You pick the starting and ending voltages, in your case 60v &amp; 5v, and one of the resistor values. You want to pick a very high resistance for your resistors as you don't want much power leaking through them to ground. You'll waste power and generate a lot of heat. 1-2 Mega Ohms should be good. For example 1M ohm for R1 and 900K ohm for R2. That should get you a reasonably close approximation of what you're looking for. It might also be a good idea to use a multi turn trim pot for R2 so that you can tune it to be more accurate. I hope this helps. If you have any more questions let me know.<br></p>
<p>Thanks for your help. I've been &quot;playing&quot; with my Arduino Uno (first time ever - NOT a programmer!) and the voltage sensor - manipulating the pin 13 LED &quot;blinks&quot; with various input voltages. The stock voltage sensor values are R1=30,000 ohms and R2=7500 ohms - makes sense for a 25 volt battery range. I'm going to replace the R1 with 75000 ohm and play around with my 48 volt battery/UPS system (55 volt range). I believe the wattage draw will be around .044 to ground thru the sensor, if I did the calcs correctly. </p>
<p>Okay I honestly don't know what I was answering in the last one... I'll leave it there since it will probably still help someone. To actually answer your question, I wouldn't. Also it's SMD resistors. SMT is Surface Mount Technology and referrers to the method not the actual components. The board shouldn't have any problem handling the voltage as long as you do what I said in the last comment and keep the resistance high enough to prevent voltage leaking to ground. In this case I might just make it as a cable and shrink tube some regular through-hole, resistors for this. #lazyForLife If you do it right it will come out looking pretty professional, costing about 50 cents, and being easier than soldering on SMD resistors.</p>
<p>Good suggestion about making a &quot;little snake that just ate a bug&quot;. I could simply splice the components into the connection wires and shrink wrap the components/entire assembly. I might actually use the board for the connector wires from the battery bank. Likely will try to desolder/solder some SMD resistors - just to see if I can do it. I've been able to do so with slightly larger SMT components - we'll see.</p>
Lol. I like the way you think.. :-P
<p>If it is just a voltage divider then why are we supplying it with 5V/3.3V ?</p><p>It must divide the high voltage provided to it and divide it and give it to signal pin</p>
<p>hallo,</p><p>myself Mihir Sukhadia. i have one question regrading voltage sensor. can i use this voltage sensor to measure a voltage which is produce by thermoelectric cooler?</p>
<p>Understanding the theory behind voltage dividers is so fundamental to electronics, that I'd really recommend taking a look at a page explaining the theory from someplace like: <a href="http://www.allaboutcircuits.com/textbook/direct-current/chpt-6/voltage-divider-circuits/">http://www.allaboutcircuits.com/textbook/direct-cu...</a><br><br>Again, if you can get this fundamental concept down, you could easily build whatever voltage divider you want for less than $0.25 (given you normally buy resistors in bulk). You could also understand why you need such high resistances for voltage dividers if you don't want to corrupt your signal by drawing too much current from the source that you're measuring. Additionally, you can then begin to understand why voltage dividers are a terrible solution for reliable power conversion. The mathematics is all algebra (at this level of design, at least).</p>
Absolutely. It's basically just a voltage divider to lower the voltage. There are calculators for making them online and some good instructables for them as well. Any DC voltage can be measured as long as it is lower than the 25 volt limit on these sensors. If you need a higher voltage then you can design a voltage decider with a higher measuring voltage.
<p>Really Helpfull, thank you</p>
<p>One more question. </p><p>I want to read voltage from a rectifier capable of delivering 20V DC @ 2000A </p><p>is it ok to use a voltage divider to accomplish this and what values should I use for resistors</p>
<p>Too much code :) Take this and use in your sketches.</p><p>void setup()</p><p>{</p><p> Serial.begin(115200);</p><p>}</p><p>void loop()</p><p>{</p><p> float val2 = analogRead(1) / 40.92;</p><p> Serial.println(String(val2) + 'V');</p><p> delay(1000);</p><p>}</p>

About This Instructable

23,176views

50favorites

License:

More by Superfish1000:Arduino Voltage Sensor 0-25V Arduino I2C display PIN debugging 
Add instructable to: