# Arduino Voltage Sensor 0-25V

53,028

52

23

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.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## 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;

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;

temp=val11/4.092;

val2=(temp/10);

Serial.println(val2);

delay(1000);

}

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

## Recommendations

• ### Internet of Things Class

22,778 Enrolled

## 23 Discussions

I want to no when voltage less than 11 v and send signal to the relay across the arduino board

Too much code :) Take this and use in your sketches.

void setup()

{

Serial.begin(115200);

}

void loop()

{

float val2 = analogRead(1) / 40.92;

Serial.println(String(val2) + 'V');

delay(1000);

}

I have a problem, serial monitor showed 0 v. Is there anything wrong?

I want to stop the voltage reading for a particular reading( suppose after 12V) and this reading how to send client mobile phone using arduino and GSM modem.

Great instructable, however I have hit was seems to be a strange problem. I am using the voltage sensor connected to an Arduino Mega 2560. I used the sketch example as above and showing the result on my Blynk app. I am measuring the voltage of a 12V battery. I put in a delay of a few seconds between loops. When I run the sketch, the first value reported is correct (i.e. the same value that I see when I connect the volt meter) which seems to imply that all is working fine. However on the next and subsequent iterations the value reported is about 2v less than. I cannot understand why this is happening. If I reset the Arduino the exact results are repeated again, the first value is correct (around 12v) followed by lower values (9.9v-10v). I am connecting the signal to analog 10. Has anybody come across this behaviour?

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!

where i find the fact that 4.092 is a valid number for voltage sensor?

val2=(temp/5); //Not 10 Why

8 replies

your right.. I think they are confused...

In fact it only divides it to bring it down to a level more tolerable to arduiinos, its not a true voltage divider & it cant use it as a divider to bring 3.3 to the arduino from 5

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.

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.

Any suggestions?

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.

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.
http://www.raltron.com/cust/tools/voltage_divider....
You pick the starting and ending voltages, in your case 60v & 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.

Thanks for your help. I've been "playing" with my Arduino Uno (first time ever - NOT a programmer!) and the voltage sensor - manipulating the pin 13 LED "blinks" 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.

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.

Good suggestion about making a "little snake that just ate a bug". 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.

If it is just a voltage divider then why are we supplying it with 5V/3.3V ?

It must divide the high voltage provided to it and divide it and give it to signal pin

hallo,

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?