Author Topic: Fluctuating ADC Reading for 4-20mA Pressure transducer input  (Read 1141 times)

0 Members and 1 Guest are viewing this topic.

Offline Bhargav_1990

  • Newbie
  • Posts: 1
  • Country: in
Fluctuating ADC Reading for 4-20mA Pressure transducer input
« on: August 02, 2018, 06:19:12 pm »
Hello...

I am working on one project of water pressure measurement, in which i used pressure transducer which gives me output between 4-20mA Current form.

I am using one 250 ohm resistance to convert current into voltage, and than i give this voltage to ADC of arduino UNO channel 0 (A0).

ADC reading is continuously fluctuating up to 10 decimal numbers.

I have also check by connecting multi-meter and measure sensor current reading, for particular pressure. but current reading is stable (even not fluctuate 1 or 2 points.)

also i have checked voltage value after 250 Ohms +Ve and ground. converted voltage is also showing very stable.

but why ADC reading is fluctuating..?

i am using arduino uno, i doesn't have any changes in AREF, AVCC etc...

please suggest effective solution,

Thank in advance.

Code: [Select]
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to

int sensorValue = 0;        // value read from the pot

void setup()
{
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}



void loop()
{
 
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
 
 
  // print the results to the Serial Monitor:
  Serial.print("Reading: ");
  Serial.println(sensorValue); 
 
  delay(1000);
}
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 2123
  • Country: fi
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #1 on: August 02, 2018, 07:23:13 pm »
Try adding a capacitor (small ceramic is fine) between 10nF and 1uF directly between the ADC input pin and ground, and report back if it resolves the noise issue.

If it doesn't help enough, consider using the AVR's reduced noise (CPU sleep) conversion mode, and/or averaging the results in software.
 

Offline Seekonk

  • Super Contributor
  • ***
  • Posts: 1836
  • Country: us
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #2 on: August 02, 2018, 08:49:23 pm »
Order emerges out of chaos. That sounds a little worse than normal, but not much. These numbers easily vary 5 counts on A/D. It may be 10 bits, but it is pretty noisy. How much of that 4-20 are you using? I always do a running average that multiplies the number also.

average = average - average / 8
average = average + A0

You will get 8 times A0 and pretty stable.  It does slow down response.  You could have noise on on the +5V.  Put a cap on the REF pin.
 

Offline JS

  • Frequent Contributor
  • **
  • Posts: 944
  • Country: ar
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #3 on: August 03, 2018, 02:10:08 am »
A filter like that could help, doing x/8 isnt very efficent, but you could do x>>3 wich numerically is the same but computationally much more efficent.

Also, the cap will reduce external noise, the frequency will depend on the value of the cap and the resistor. If you are sampling slow you could use a bigger cap to reduce noise even further but there is a limit where the internal noise dominates.

The measurements with your multimiter is already filtered, that's why you dont see noise there. The adc of the arduino makes the sampling in 1.5 clocks, which means the response is up to 1/1.5 the clock frequency (of the adc, not the sampling frequency or the system clock) but that goes well beyond audio frequency, IIRC it can sample in 13 clocks at 9kHz or something, so the response will go up to 80kHz or something like that. Any noise up to that frequency will show in the conversion unless you do some filtering. The best solution might be using some analog filtering, at least to get rid of aliasing and some digital filtering to get rid of the internal noises. Also, you can add an external cap to the vref pin, to clean the reference a bit.

JS

If I don't know how it works, I prefer not to turn it on.
 

Offline newbrain

  • Frequent Contributor
  • **
  • Posts: 783
  • Country: se
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #4 on: August 03, 2018, 07:49:53 am »
doing x/8 isnt very efficent, but you could do x>>3 wich numerically is the same but computationally much more efficent.
Really?
I beg to differ:
Code: [Select]
C:\avr8-gnu-toolchain-win32_x86>bin\avr-gcc.exe -O0 -g divtest.c

C:\avr8-gnu-toolchain-win32_x86>bin\avr-objdump.exe -S a.out
...
#include <stdint.h>
volatile uint32_t test1 = 1234;
volatile uint32_t test2 = 1234;

int main(void)
{
...
    uint32_t z = test1/8;
  2c:   80 91 60 00     lds     r24, 0x0060     ; 0x800060 <__data_start>
  30:   90 91 61 00     lds     r25, 0x0061     ; 0x800061 <__data_start+0x1>
  34:   a0 91 62 00     lds     r26, 0x0062     ; 0x800062 <__data_start+0x2>
  38:   b0 91 63 00     lds     r27, 0x0063     ; 0x800063 <__data_start+0x3>
  3c:   68 94           set
  3e:   12 f8           bld     r1, 2
  40:   b6 95           lsr     r27
  42:   a7 95           ror     r26
  44:   97 95           ror     r25
  46:   87 95           ror     r24
  48:   16 94           lsr     r1
  4a:   d1 f7           brne    .-12            ; 0x40 <__SREG__+0x1>
  4c:   89 83           std     Y+1, r24        ; 0x01
  4e:   9a 83           std     Y+2, r25        ; 0x02
  50:   ab 83           std     Y+3, r26        ; 0x03
  52:   bc 83           std     Y+4, r27        ; 0x04

    uint32_t x = test2 >> 3;
  54:   80 91 64 00     lds     r24, 0x0064     ; 0x800064 <test2>
  58:   90 91 65 00     lds     r25, 0x0065     ; 0x800065 <test2+0x1>
  5c:   a0 91 66 00     lds     r26, 0x0066     ; 0x800066 <test2+0x2>
  60:   b0 91 67 00     lds     r27, 0x0067     ; 0x800067 <test2+0x3>
  64:   68 94           set
  66:   12 f8           bld     r1, 2
  68:   b6 95           lsr     r27
  6a:   a7 95           ror     r26
  6c:   97 95           ror     r25
  6e:   87 95           ror     r24
  70:   16 94           lsr     r1
  72:   d1 f7           brne    .-12            ; 0x68 <__SREG__+0x29>
  74:   8d 83           std     Y+5, r24        ; 0x05
  76:   9e 83           std     Y+6, r25        ; 0x06
  78:   af 83           std     Y+7, r26        ; 0x07
  7a:   b8 87           std     Y+8, r27        ; 0x08
    return 0;
  7c:   80 e0           ldi     r24, 0x00       ; 0
  7e:   90 e0           ldi     r25, 0x00       ; 0
}
...
Any half decent compiler would do that kind of transformation for you, even without asking for optimization.

There is no need to obfuscate your code, using a shift when a division is intended.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 2123
  • Country: fi
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #5 on: August 03, 2018, 08:16:23 am »
Any half decent compiler would do that kind of transformation for you, even without asking for optimization.

There is no need to obfuscate your code, using a shift when a division is intended.

Just about maybe five years ago, avr-gcc didn't do this. I optimized many codebases by replacing divisions with shifts, with clear speed and code size benefits.

I don't have a full confidence on fairly new optimization features. Why I need to test and verify every time, when I can be explicit?

There's nothing wrong in explicitly writing the intended operation, instead of the "higher level description", especially in the case where every half-decent programmer instantly understands why the shift operation is used, both the reason and the actual value it's being divided by.

But there's also a deeper meaning here.

Microcontroller C code is not a high-level algorithmic description. Sorry. We are very far from that.

By explicitly writing the shift operation, you are documenting the code that it needs the shift operation. Compiler can never know this. The next coder won't know, either.

This means, you can't just change the divisor to, say, 10. But you can change the amount of shifting in the correct steps, automatically. If this is a timing-critical situation enough, where you don't have time for the division, using a division would be just plain wrong operation; as wrong as doing multiplication or substraction or cosine or any other wrong operation. Then, why would you want to write down the wrong operation? That would be highly misleading and it would be tempting to change the divisor, hence changing the operation to something the timing was not originally designed for. This gets really bad very quickly if you then use a const or #define to give that nice "generic" touch to the code. Now a slight parameter adjustments change the operation from correct to wrong, and the code just stops working, and it's hard to see why.

Hence, your "clean" way of using a division is actually obfuscation - it's obfuscating low-level microcontroller code to look like higher level description, which it still really isn't; then relying on compiler optimization to turn the code back to what the programmer actually intended. This is stupid, and dangerous. It would at least require a comment:
// Compiler optimizes this to a shift; only use divisors 2^n
which would highlight the stupidity of not just explicitly writing the shift to begin with, when you wanted a shift. Yes! This is a textbook example of obfuscation.

Your argument of obfuscation is only valid when you are completely fine without trusting the compiler to optimize this, and are writing cross-platform algorithm on purpose. Or when the optimization is only about a small percentage of CPU time on a workstation. But on an AVR, whether you have a shift of a divisor in the ADC averager code, is often a matter between functionality and non-functionality.
« Last Edit: August 03, 2018, 08:26:28 am by Siwastaja »
 

Offline JS

  • Frequent Contributor
  • **
  • Posts: 944
  • Country: ar
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #6 on: August 03, 2018, 08:44:09 am »


...
  This is stupid, and dangerous. It would at least require a comment:
// Compiler optimizes this to a shift; only use divisors 2^n
which would highlight the stupidity of not just explicitly writing the shift to begin with, when you wanted a shift. Yes! This is a textbook example of obfuscation.

@Siwastaja thanx, you made my point and save me many lines... Good code shouldn't need comments, comments takes time to write, read, and even worse, maintain which is commonly forgotten, so NEXT NEXT guy gets confused forthe FIRST guy comments on the NEXT guy modified code who didn't maintain the comments.

So yes, maybe sometimes comments are necessary, but many times avoidable by choosing better code or just naming, which is much nicer and easier to get than comments all over the place.

JS

If I don't know how it works, I prefer not to turn it on.
 

Offline Seekonk

  • Super Contributor
  • ***
  • Posts: 1836
  • Country: us
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #7 on: August 03, 2018, 08:31:19 pm »
Somehow, I sense for the OP this whole discussion is in vain.
 
The following users thanked this post: hans

Offline JS

  • Frequent Contributor
  • **
  • Posts: 944
  • Country: ar
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #8 on: August 03, 2018, 08:59:16 pm »
Somehow, I sense for the OP this whole discussion is in vain.
I disagree, he is learning programming and if he gets good modes now he will thank it later, and as discussed later this could make ornbreak your program, doing real time processing in an 8bit μC is all about optimization.

JS

If I don't know how it works, I prefer not to turn it on.
 

Offline Seekonk

  • Super Contributor
  • ***
  • Posts: 1836
  • Country: us
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #9 on: August 04, 2018, 12:26:46 am »
Optimizing code in a UNO...... I needed that bit of humor after having my equipment hit by lightning.
 
The following users thanked this post: newbrain

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 2243
  • Country: gb
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #10 on: August 04, 2018, 12:14:36 pm »
Good code shouldn't need comments,

I strongly disagree.  Even the best written code may be doing something that isn't immediately obvious to someone that's never seen it before.  Writing good comments in places that they will be helpful, and maintaining them is an inherent part of writing good software.

So yes, maybe sometimes comments are necessary,

And now you disagree with yourself.
 
The following users thanked this post: hans, newbrain

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 2123
  • Country: fi
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #11 on: August 04, 2018, 05:14:00 pm »
Comments - especially describing highler level design on low level language - are highly necessary, but if you find yourself needing to write a lot of comments about small details with the meaning: "this line of code is supposed to produce this-and-this low-level end result", then chances are you can do it better, and explicitly to begin with. When you just can't, the comment will be very valuable.

But if you want a shift for any reason, write a shift.
 

Offline jancumps

  • Supporter
  • ****
  • Posts: 1216
  • Country: be
  • New Low
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #12 on: August 04, 2018, 05:22:58 pm »
How off-topic can a reasonable human being get? Why post this crap in a genuine post?
 
The following users thanked this post: hans

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 2123
  • Country: fi
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #13 on: August 05, 2018, 10:50:34 am »
How off-topic can a reasonable human being get? Why post this crap in a genuine post?

You are crap, as is your post, which is the only crap post in this thread so far. (This is the second one. Congratulations on being the trendsetter.)

The OP was actually given the actual advice by the same posters already (see replies #1 and #2). We are waiting for feedback whether it works or not, or more questions. While at it, the discussion at hand is (was) meaningful and educative. You are free to ignore it and fuck off.
« Last Edit: August 05, 2018, 10:52:40 am by Siwastaja »
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 14035
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #14 on: August 05, 2018, 05:29:00 pm »


You are crap, as is your post, which is the only crap post in this thread so far. (This is the second one. Congratulations on being the trendsetter.)

The OP was actually given the actual advice by the same posters already (see replies #1 and #2). We are waiting for feedback whether it works or not, or more questions. While at it, the discussion at hand is (was) meaningful and educative. You are free to ignore it and fuck off.

And no matter how write you have decided you are it does not give you the right to abuse anyone!
https://www.simonselectronics.co.uk/shop
Varied stock of test instruments and components including EEVblog gear and Wurth Elektronik Books.
Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559
 

Online EEVblog

  • Administrator
  • *****
  • Posts: 30261
  • Country: au
    • EEVblog
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #15 on: August 06, 2018, 12:28:35 am »
How off-topic can a reasonable human being get? Why post this crap in a genuine post?
You are crap, as is your post, which is the only crap post in this thread so far. (This is the second one. Congratulations on being the trendsetter.)
The OP was actually given the actual advice by the same posters already (see replies #1 and #2). We are waiting for feedback whether it works or not, or more questions. While at it, the discussion at hand is (was) meaningful and educative. You are free to ignore it and fuck off.

Siwastaja - Personal attacks will not be tolerated on this forum, please don't do that again.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 2123
  • Country: fi
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #16 on: August 06, 2018, 07:03:44 am »
But I do see now that steering a completely civil, factual and well presented thread with direct personal attacks - "your post is crap" - is allowed without even getting any moderator comment on the matter. And that you can then go "boohoo, moderators", and you never have to justify what you say.

I'm a bit disappointed.

I've seen this trend a few times lately (which is why I snapped, and am sorry for it): it's clearly OK to just jump in to a random, civil, factual discussion out of nowhere, and expliticly dismiss the effort of the posters, using fairly strong words that clearly fall into your "abuse" category, for no apparent reason whatsoever, with no clear motive.

You are right that my response is not any better, and actually it is even worse. Still, it's very interesting to see the total lack of comment about the actual root cause of this issue, even when it's in plain sight. But you have set a clear line here. It's completely OK to go and dismiss people's comments and effort, using any words available with the "you are shit" message written all over, but if you reply back commenting about the poster themselves using the same words, it's not OK. Thanks - I admit I tested this line on purpose. I understand now where the line is. And I agree it's logical to draw it here: talking shit about people's effort, time, skills and comments is OK, while talking about the person directly isn't. Indirect "fuck you" - anything goes. Responsing back directly - not OK.

Still, I'm disappointed in you guys. I would either lower or rise the line; to either disallow this kind of shit completely, or let it be, and let the people fight. I.e., either forcing civil, noise-free environment, or creating the Wild West. Now you are failing to do either.

Luckily, the OP got the actual help from me 1 hour 4 minutes from their initial post, and from Seekonk just 1.5 hours later. Later replies give more insight (with a tad of civil debate) how to apply the tip in reply #2, which is an important pointer, because implementing a running cumulative average in a microcontroller is not trivial for a beginner (even more could be said about the precision and level of fixed-point math required, but I don't bother to do it right now). When combined, these replies include everything the OP needs to try out first, as the level of noise seen on the line is not indicative of any serious issue. Most likely, the problem will be solved with the combination of these two pieces of advice. But we need to hear back from the OP to see how it goes before we can help further.

I like to help people, and do it a lot on these forums, which I think no one will disagree with. But since no one pays for this "service", I have an ego such that I don't like my help being totally dismissed, without actual pointers to factual errors or important omissions I could fix. I guess I'm not the only one here who's like this.

BTW, I feel that the lack of the "ignore" function - present in most forums - puts a huge burden on the moderators. The ignore function is the most reliable way to improve S/N ratio, and everybody can do their own decisions. This prevents conflicts, and avoids a situation where the decision of not having the ignore function "forces" the people to be together, at the mercy of the moderators; in which case, most of the time, at least one of the parties will feel unhappy about the moderator decisions.

I mentioned the ignore function once on a post - I got several PM's from well known posters with good karma, who also were wondering the lack of this feature, and definitely wanted to have it. We wondered it together. Without giving us the basic tools to cope with trolls and shitposters, you better do a damn good job dealing with it "universally", and better be fair to everyone, including the contributors.

BTW, being one of the victims of actual abuse years back, I don't like the fact that this term is now used to describe any random "you suck" - "you suck too" discussion. In my books, abuse is about death threads, continued stalking, things like that that have a real and verifiable chances of causing psychological trauma. You calling me an abuser this easily is something I don't like and don't want to hear again. Thank you for your consideration.

Dear moderators, please give your comment on what you think about my points, and I'll make my choice. Thank you.
« Last Edit: August 06, 2018, 08:09:54 am by Siwastaja »
 
The following users thanked this post: hans

Offline Simon

  • Global Moderator
  • *****
  • Posts: 14035
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Fluctuating ADC Reading for 4-20mA Pressure transducer input
« Reply #17 on: August 06, 2018, 11:44:37 am »
not even bothering to read that lot.
https://www.simonselectronics.co.uk/shop
Varied stock of test instruments and components including EEVblog gear and Wurth Elektronik Books.
Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559
 
The following users thanked this post: Siwastaja


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf