Fluctuating ADC Reading for 4-20mA Pressure transducer input - Page 1
Forum section: electronics / general

author: Bhargav_1990
author_rank: Newbie
timestamp: August 02, 2018, 06:19:12 pm
content: 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:const int analogInPin = A0; // Analog input pin that the potentiometer is attached toint sensorValue = 0; // value read from the potvoid 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);}

author: Siwastaja
author_rank: Super Contributor
timestamp: August 02, 2018, 07:23:13 pm
content: 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.

author: Seekonk
author_rank: Super Contributor
timestamp: August 02, 2018, 08:49:23 pm
content: 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 / 8average = average + A0You 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.

author: JS
author_rank: Frequent Contributor
timestamp: August 03, 2018, 02:10:08 am
content: 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

author: newbrain
author_rank: Super Contributor
timestamp: August 03, 2018, 07:49:53 am
content: Quote from: JS on August 03, 2018, 02:10:08 amReally?I beg to differ:Code:C:\avr8-gnu-toolchain-win32_x86>bin\avr-gcc.exe -O0 -g divtest.cC:\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 set3e: 12 f8 bld r1, 240: b6 95 lsr r2742: a7 95 ror r2644: 97 95 ror r2546: 87 95 ror r2448: 16 94 lsr r14a: d1 f7 brne .-12 ; 0x40 <__SREG__+0x1>4c: 89 83 std Y+1, r24 ; 0x014e: 9a 83 std Y+2, r25 ; 0x0250: ab 83 std Y+3, r26 ; 0x0352: bc 83 std Y+4, r27 ; 0x04uint32_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 set66: 12 f8 bld r1, 268: b6 95 lsr r276a: a7 95 ror r266c: 97 95 ror r256e: 87 95 ror r2470: 16 94 lsr r172: d1 f7 brne .-12 ; 0x68 <__SREG__+0x29>74: 8d 83 std Y+5, r24 ; 0x0576: 9e 83 std Y+6, r25 ; 0x0678: af 83 std Y+7, r26 ; 0x077a: b8 87 std Y+8, r27 ; 0x08return 0;7c: 80 e0 ldi r24, 0x00 ; 07e: 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.

author: Siwastaja
author_rank: Super Contributor
timestamp: August 03, 2018, 08:16:23 am
content: Quote from: newbrain on August 03, 2018, 07:49:53 amJust 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 isnota high-level algorithmic description. Sorry. We areveryfar from that.By explicitly writing the shift operation, you are documenting the code that itneedsthe shift operation. Compiler canneverknow 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 getsreallybad very quickly if you then use aconstor #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 tolook likehigher 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^nwhich 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 finewithouttrusting 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.

author: JS
author_rank: Frequent Contributor
timestamp: August 03, 2018, 08:44:09 am
content: Quote from: Siwastaja on August 03, 2018, 08:16:23 am@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

author: Seekonk
author_rank: Super Contributor
timestamp: August 03, 2018, 08:31:19 pm
content: Somehow, I sense for the OP this whole discussion is in vain.

author: JS
author_rank: Frequent Contributor
timestamp: August 03, 2018, 08:59:16 pm
content: Quote from: Seekonk on August 03, 2018, 08:31:19 pmI 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

author: Seekonk
author_rank: Super Contributor
timestamp: August 04, 2018, 12:26:46 am
content: Optimizing code in a UNO...... I needed that bit of humor after having my equipment hit by lightning.

author: mikerj
author_rank: Super Contributor
timestamp: August 04, 2018, 12:14:36 pm
content: Quote from: JS on August 03, 2018, 08:44:09 amI 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.Quote from: JS on August 03, 2018, 08:44:09 amAnd now you disagree with yourself.

author: Siwastaja
author_rank: Super Contributor
timestamp: August 04, 2018, 05:14:00 pm
content: 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.

author: jancumps
author_rank: Unknown
timestamp: August 04, 2018, 05:22:58 pm
content: How off-topic can a reasonable human being get? Why post this crap in a genuine post?

author: Siwastaja
author_rank: Super Contributor
timestamp: August 05, 2018, 10:50:34 am
content: Quote from: jancumps on August 04, 2018, 05:22:58 pmYou 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.

author: Simon
author_rank: Unknown
timestamp: August 05, 2018, 05:29:00 pm
content: Quote from: Siwastaja on August 05, 2018, 10:50:34 amAnd no matter how write you have decided you are it does not give you the right to abuse anyone!

author: EEVblog
author_rank: Unknown
timestamp: August 06, 2018, 12:28:35 am
content: Quote from: Siwastaja on August 05, 2018, 10:50:34 amSiwastaja - Personal attacks will not be tolerated on this forum, please don't do that again.

author: Siwastaja
author_rank: Super Contributor
timestamp: August 06, 2018, 07:03:44 am
content: 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'sveryinteresting 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'scommentsandeffort, 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'slogicalto draw it here: talking shit about people's effort, time, skills and comments is OK, while talking about thepersondirectly 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 shitcompletely, 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 gotseveralPM'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.

author: Simon
author_rank: Unknown
timestamp: August 06, 2018, 11:44:37 am
content: not even bothering to read that lot.