Go Down

Topic: Problems with reading A/D... (Read 2589 times) previous topic - next topic


Jan 16, 2009, 03:28 am Last Edit: Jan 16, 2009, 01:05 pm by fkatzenb Reason: 1
My project is a shield for the Arduino.  It is designed to monitor engine knock voltage from peizo sensor w/ conditioning circuit, determine RPM via a frequency to voltage convertor, determine when knock is present by verification against RPM, datalog on serial, and display on a LCD.

I am currently considering a s/w problem because I wrote the code. ;)  I think I did a pretty good job on my code, it's structure, and comment it, but there must be a problem with it somewhere.  Any help is greatly appreciated!  Edit: The voltages do not even remotely reflect the actual voltage as measured by a meter.

Here is the code: http://www.squirrelpf.com/knockboard/code.txt



Well you might start by telling us what works and what doesn't work  ;D



Jan 16, 2009, 01:05 pm Last Edit: Jan 16, 2009, 07:35 pm by fkatzenb Reason: 1
Wow... nicely done Frank. LOL.  Fixed description.


So, what was the problem?  How did you fix it?  It's useful to know, because others will search these forum postings for help when they have the same kind of trouble.


The problem isn't fixed.  i just fixed my post which didn't describe what was wrong.


Hm.  It "does not remotely reflect the voltages" the way you expect.

How about telling us more?  What's producing the voltage?  What range of voltages do you read with a meter?  Where are you sending that signal?  How are you reading that signal?  What range of readings do you get?  Is the reading high when the meter shows a high voltage?  Is the reading low when the meter shows a low voltage?  Are the readings noisy?  random?  clipped?  inverted?  not the same number you see on the meter?  ...

How to ask questions the smart way.


Well I figured people would give it a try.  Put to run it down, 5 volts would appear around 1 volt and it tens to fluctuates a bit.  If I change it to 4 volts, it would still be just a bit less then 1 volt.


Jan 20, 2009, 01:03 am Last Edit: Jan 20, 2009, 01:03 am by matthewsg Reason: 1
I suspect you have a issue of mixing byte with floating point.

byte    knock_volt;                         // this is the raw knock voltage at any instance in time

0-255 is all that knock_volt will ever contain. It will rollover to 0 when it reaches 256.

knock_volt = analogRead(knockPin);                               // grab the sensor data for the knock sensor
 knock_volt = map(knock_volt, 0, 1023, 0, 255);                   // remap the 10-bit input to 8-bit

Okay, you even map the 10 bits down to 8.
May be better to keep the 10 bits and use INT as you lose precision.

Serial.print(knock_volt, DEC);

That should show a value between 0-255?

 printDouble(knock_volt * 0.019531, 3);                                                  // Knock Volts

- knock_volt is not a double
- knock_volt * 0.019531 ?

 255 the maximum knock_volt can be * 0.019531 is never going to be more than 4.

I suggest you do the scaling completely with integer (x10 or x100), then insert the decimal for printing.

For example...
knock_bits  0-1023 or 0-5V

knock_bits = 511 or 2.5V or  204.4 bits per volt.
since we want two decimal place precision voltsPerBit = 2044

long volts (because int will overflow)

volts = (knock_bits * 1000) / 2044

volts now contains the actual volts * 100
Very fast, no floating point.
It's just a matter of formatting the output.

Avoid using floating point.
When reading only 1024 bits, the precision is ony 1 bit or 1/1000 easy to handle without floating point.

Hope that helps.


Jan 20, 2009, 02:09 am Last Edit: Jan 20, 2009, 02:11 am by halley Reason: 1
As George says, you're losing data in knock_volt.

Code: [Select]
byte knock_volt;
knock_volt = analogRead(knockPin);
knock_volt = map(knock_volt, 0, 1023, 0, 255);

After this, the highest value you could have in knock_volt is 63.  What's worse, the value 63 will appear when the voltage is at 1/4, 1/2, 3/4 and full levels.


Thanks folks!  That took care of it!  :D  

Lesson learned!

Go Up