Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

measuring analog voltage using arduino

Status
Not open for further replies.

marrc

Junior Member level 2
Joined
Mar 1, 2017
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
india
Activity points
219
hello!
I am trying to measure and display analog +5 to -5 v on arduino using below sketch,

Code:
void setup() {

 // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  float sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

unfortunately its only dispalying 0-5 v but i want -5 to 5 v range
pot connections: ceter pin to A0 and other two to +5 and -5 v
where am i going wrong?

thanks in advance
Marrc
 

You can only input 0 to 5V to Arduino's adc input pin. If you input -5V or negative voltage then the adc module will get damaged. As your Arduino runs from 0 to 5V that is GND to 5V you can only input 0 to 5V to adc input pin if your Vref+ is 5V.
 

If you need to measure negative voltages, electrically scale the voltage range so it lies between 0V and 5V and reverse the process in software. So for -5 to +5 input you have a range of 10V and you need to halve that. You also have to prevent the voltage going below zero to protect the ADC input so you need to add a positive offset to it, again this can be removed in software.

Electrically do this: (input voltage / 2) +2.5V.
so -5V becomes -2.5V then has 2.5V added = 0V and +5 becomes +2.5C and has 2.5V added = 5V.

Then when you have the ADC result, subtract 2.5V (probably 512 numerically) and double the result to restore the original voltage value.

Brian.
 

Connect one side of your pot to +5v and the other side to grd. The center of the pot goes to the Arduino's input. Write a routine in your code to convert analogeRead of 0 to -5V, 2.5V to 0V and 5V to +5V.
 

Write a routine in your code to convert analogeRead of 0 to -5V, 2.5V to 0V and 5V to +5V.
What use is that?
The objective is to MEASURE a negative voltage, not to manipulate the figures so a positive voltage looks as though it is negative.

Brian.
 

Hi again,

I have tried your idea, i am getting proper scaling (electrically)
though am getting range on serial monitor i.e -5v ip on a0 shows as -5 and 5v shows as 5v but in between the ops are unstable, so like for 1.3v i am getting following readings on serial monitor(in mv):

2776.15
2629.52
928.64
1925.71
1368.52
1583.58
1026.39
1769.31
2678.40
1407.62
1984.36
997.07
1065.49
2707.72
1681.33

there seems that some tweaking is required in the code
the code is as follow:

Code:
void setup() {
  // initialize serial communication at 9600 bits per second:

  
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  float sensorValue = analogRead(A0);
   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = (sensorValue - 512) * 2 * 5000 / 1023  ;
  // print out the value you read:
  Serial.println(voltage);
}

thanks in advance
marrc
 

I'm assuming (can't check at the moment) that the Serial.println() function can convert a float to a string.
Are those consecutive readings with a stable 1.3V input? Such wide variation suggests the input isn't stable or the ADC isn't finishing the conversion properly.

Brian.
 

Hi,

I assume you used VCC as ADC_Reference .. this I don´t recommend for precise measurements.

ADC_OUT = 1024 * V_IN / V_REF
--> if V_IN is not stable then ADC_OUT will fluctuate
--> if V_REF is not stable then ADC_OUT will fluctuate. Here you should not consider VCC as stable reference. Thus you should expect ADC_OUT fluctuation.

As ADC_OUT max is 1023 --> you can never decode 5V ... it´s always 1 LSB below.

I see you used "1023" instead of "1024". This is not accurate, but you are free to do this if you don´t care about accuracy.

Your values correspond to ADC_OUT values of 95 ... 284.
You need to read the ADC datasheet (section) how to treat V_IN, V_REF, sample rate to gete precise ADC values.

We can´t verify this, because you don´t show us your ADC connections (circuit)

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top