i need to read a voltage with an arduino pro mini using the analog pins.
I created this simple test code:
int sensorValue;
float voltage;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(A0);
voltage= sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
}
my setup looks like this: hardware setup
everything works so far. I get about 4,5v as a result. Now i want to port everything to an arduino pro mini. After i did that i only got 2,5v as a result. I also tried it with different pins and with an other arduino pro mini.
So what did i do wrong? Do i need an other pull-up resistor?
-
12.5 V is what you expect. Why would you read 4.5 V?Edgar Bonet– Edgar Bonet2016年10月10日 14:33:51 +00:00Commented Oct 10, 2016 at 14:33
-
so you think my arduino nano is not working properly?betion– betion2016年10月10日 14:38:57 +00:00Commented Oct 10, 2016 at 14:38
-
Or you used different resistors, or you used different code.Edgar Bonet– Edgar Bonet2016年10月10日 14:49:13 +00:00Commented Oct 10, 2016 at 14:49
1 Answer 1
I made a circuit bellow for you. Note that the top resistor is 100 ohm and the bottom one is only 10 ohm. What I did is called a voltage divider. This way you can read voltages up to 50V. Simply connect the positive of your power source that you want to measure to the red wire and the ground of that same source to the black wire.
I also included a code that you could use:
Please notify me if any mistakes were spotted.
#define sensor A0 //defining sensor pin
float resistor1 = 100; //you can change it if you want, just make sure you also do it on the circuit
float resistor2 = 10;
float Vmax = (5*resistor1 + 5* resistor2)/resistor2;
void setup() {
pinMode(sensor, INPUT); //declaring sensor as an output
Serial.begin(9600);
Serial.print("Do not input more then (V) : "); // print max input voltage
Serial.print(Vmax);
Serial.println(); //skip line
}
void loop() {
float V = analogRead(sensor); //measuring analog values
V = analogRead(sensor) * Vmax / 1024
Serial.println(V); //printing voltage
}
-
Don't see mistakes, but here are a few suggestions: 1) Use higher valued resistors (but no more than 10 kΩ for R2) to avoid excessive current draw. Otherwise a 50 V source would blow up your 1/4 W resistors. 2) Declare the resistor values as
float
. 3) WriteV = analogRead(sensor) * Vmax / 1024
instead of usingmap()
. 4) Why divide the voltage by 100?Edgar Bonet– Edgar Bonet2016年10月10日 15:21:04 +00:00Commented Oct 10, 2016 at 15:21 -
That
float int resistor1
andfloat int resistor2
works? If yes, what type is it in the end?float
orint
?KIIV– KIIV2016年10月10日 17:10:32 +00:00Commented Oct 10, 2016 at 17:10 -
opps, its just float, my bad!Dat Ha– Dat Ha2016年10月10日 17:11:20 +00:00Commented Oct 10, 2016 at 17:11