Analog Voltage Read not consistant

Hi,
I am using a sick MPS sensor which has analog outputs as current 4-20 mA and voltage 0-10 V. As the Arduino mega 2560 that I am using cannot take more than 5V as input through the analog input ports I am using a current to voltage converter that converts 4-20 mA to 0-5 V. I set up the module and the sensor on my pneumatic piston and the outputs were measured with the help of a multimeter and the outputs are up to specification from the sensor. The piston travel is about 60mm so I set the minimum voltage output from the module to be 0.5V and the maximum at 60mm to be 2.74 V. I read these on my multimeter but when I connect it to the Arduino board it doesn't give me the exact voltage with the analog read program I have. I tested with the board's 5 V, 3.3 V, and GND to see if those values are fluctuating but they don't. Only the input I am giving from my sensor is fluctuating while it isn't actually fluctuating as seen on my multimeter and I really don't understand how to fix this issue. Could someone please guide me through the way I can get the correct and stable input?

Sensor Product Link

Current to Voltage Module

/Code/

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


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

Hi,
Can you post link to your MPS sensor?
Can you please post a circuit diagram?

Also;

float voltage = (float)sensorValue * 5.0 / 1023.0;

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I think the problem is likely to be that you have noise, or mains hum superimposed on your sensor output.

Your multimeter averages out the signal and gives you a 'correct' reading.

With the Arduino you take an instantaneous reading, which can be higher or lower that the mean value by up to the amplitude of the interfering signal.

Try taking multiply readings and taking the mean value.
It might pay to ensure that the samples are taken over a whole number of cycles of the 50Hz/60Hz mains frequency.

Hi Tom,

The link for the sensor is mentioned in my query. I am pasting it here again. Sensor Product Page with Techincal Data Sheets.. The wiring for the sensor is attached below as a JPG.


Please ignore my very basic circuit diagram skills.

Could you tell me on how to do that. I am very new to coding in general so if there is a guide that you recommend with which i can learn that would be really helpful.

You could reduce any interfering signals by using twisted pairs, or screened cable to make your connections to the sensor and converter.

Hi,
You could also put a 0.1uF bypass capacitor between the 5V signal line and gnd.

Tom.... :smiley: :+1: :coffee: :australia:

float avgReadings() {
  const int avgCount = 10;
  long sum = 0;
  for( int i=0; i<avgCount; ++i)n {
    sum += analogRead(A0);
  }
  return sum * 5.0 / 1024.0 / avgCount;
}

then, inside loop()

void loop() {
  float voltage = avgReadings();
  ...

Edit: need to set sum to 0 before accumulating

The average readings are calculated with this code but from what values I see there is an issue. When the module is sending out 0.5V or the maximum output set at 2.74V the Arduino prints in the serial monitor a set of random values which are then averaged and it is not close to the actual value at all. I rechecked the wiring and isolated every other wire that is close to the setup to have no interference at all and I still see the values to be very random. Even when the signal output from he module is not more than 2.75V it shows a few readings that are close to 4.6V. I tested by connecting a set of LED's to see if they are fluctuating because of the voltage or if it is getting very high voltage but it seem to remain with the set limits. Could there be any other reason why the onboard voltage outputs are read perfectly but not the external signals?

The human eye is way to slow to notice any brief changes in an LED. That is why we can use PWM to make an LED appear dim.

Did you try any bypass capacitors on your signal line? And how is everything wired together? A schematic would be useful.

I also edited my last response since the code was not setting sum to 0 before starting.

That's fundamentally wrong.
Default Aref (5volt) on a Mega is potentially unstable, and so will your measuring results be.

You should use one of the two stable internal Aref voltages of the Mega for stable results.
There is a 1.1volt and a 2.56volt available.
For the 1.1volt Aref a 51 ohm resistor should be used, or for the 2.56volt Aref about 120 ohm.
No current to voltage module needed.
This has been a topic in many threads in the past. Do a search.
Leo..

2 Likes

Thank you Leo for the information. I will try to search and find more information on using Aref. From what I understand I need to be providing a reference voltage of the max voltage I will be supplying to my analog input. I am guessing I need to use a separate module for that and the reason I am using that current to voltage module is that the sensor has 0-10v dc output which I cannot directly give to my Arduino and in some of the forums I read online said that using a step-down module would not give the accuracy I want when measuring the position with my sensor. I will read more on how to give analog inputs to the arduino as well as I am really new to electronics I think the way I am connecting the circuit is also not so great. so maybe that is why the voltage read by the arduino is not correct and when i checked the port without the input connected i was getting the random values like i did because of the lack of a reference voltage. I will try to see what needs to be done. if anyone has an idea on how the circuit needs to look like please feel free to give more information so i can learn and rectify the errors. Thanks

I agree, this is #1 factor causing the problem, not Aref.

Show the picture.

The readings are 100% correct, whatever is on on A0 that is shows on readings, put oscilloscope on A0., don't have one ?, use this one.

Hi,

Precision, not accuracy.
The module you need is called a "potential divider" and with two resistors you can make one.

Google;

arduino analog input potential divider

https://startingelectronics.org/articles/arduino/measuring-voltage-with-arduino/

Tom.... :smiley: :+1: :coffee: :australia:

What is the wire (cable) distance between sensor and Mega?

The distance is less than 8cm. I think it is not an issue with the sensor or the board but with the connections. The reason behind this thought is that when i am measuring the voltage out with a voltmeter then i use the module output at the positive terminal and the battery -ve in the module as the negative terminal of the voltmeter. With this setup i read the correct values. But when i connect the voltage out from the module to the arduino it doesn't have the reference of this voltage there to read the analog input as a sensible value. Now i even tried with a voltage divider circuit. I used 2 1k ohm resistors in series, converting my maximum voltage directly from the sensor which is 10v to 5v. For this the measuring was done with the terminals set as the voltage out from the divider circuit and the ground of the circuit. I read the volatge reduced to 5V as intended. Now that i connect it to the arduino it is the same case. It doesn't not read the voltage. Now the main question is how do i provide the 24V -ve which the sensor uses as ground to the arduino as reference to ground. This is a really strange question but how the sensor should be hooked up to be able to read the analog voltage? If anyone can provide me with guidance that would be really helpful.

Hi,

You do not need -24V, just a 24V supply connected as below.

Rather than trying to describe your circuit, can you please draw it out on paper and post an image please?
A picture is worth about 1000 misleading posts here.

Tom... :smiley: :+1: :coffee: :australia:

Hi Tom,
Yes, I am posting the circuit diagram below.

I want to use the voltage out from the sensor directly instead of using the current to the voltage module. The two resistors used for the voltage divider circuit are 1K ohm each. Now, when I connect the ground and the Vout port to the multimeter i can read the voltage as the voltage I am supposed to get. I just want to know now how to connect this circuit to the arduino so that I can actually read this voltage value with the arduino. If I connect the Vout directly to arduino it doesn't read anything, just gives some garbage voltage values. I hope this is now understandable.
thanks.
Edit: Sorry for the reversed symbol for the battery, i was in a hurry to post a reply.

And you have the negative side of that 12V supply connected to the arduino GND?

I don't have the 12V supply but the 24V supply. And no I haven't connected the negative of the 24V power supply ( batteries in my case) to the GND of the Arduino because I didn't want to destroy the Arduino by connecting the cables wrong. I am not sure if connecting the gnd of the batteries to the arduino gnd will solve the issue or if it will destroy my arduino.