Analog signal processing
Moderators: adafruit_support_bill, adafruit

Analog signal processing

by TESBill on Mon Aug 06, 2012 11:57 am

I am taking analog measurements and can't figure out how display decimal fraction numbers. I'll elaborate, I know that the arduino uses a 0-1023 to a 0-5 Vdc conversion. When I apply 1.37 Vdc I don' get it displayed, I only get one volt increments and need evrything in between. Do you know where I could get some sample code to do this? or is it even possible to have that resolution?
TESBill
 
Posts: 5
Joined: Thu Aug 02, 2012 10:38 pm

Re: Analog signal processing

by adafruit_support_bill on Mon Aug 06, 2012 12:34 pm

Something like this: (substitute appropriate pin numbers for your project)
Code: Select all | TOGGLE FULL SIZE
int raw = analogRead(analogPin);  // get the raw reading
float volts = (raw * 5.0)/1024; // scale raw reading to volts
User avatar
adafruit_support_bill
 
Posts: 29218
Joined: Sat Feb 07, 2009 9:11 am

Re: Analog signal processing

by TESBill on Mon Aug 06, 2012 7:42 pm

The math is good but I still only have 1vdc increments. Are there any limitations I am unaware of?
int sensorPin = A0;
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
Serial.begin(9600);
}

void loop() {
// read the value from the sensor:
int raw1 = analogRead(sensorPin);

float mAval = (raw1*20)/1024;
delay(2000);

Serial.println(mAval,2);
}
TESBill
 
Posts: 5
Joined: Thu Aug 02, 2012 10:38 pm

Re: Analog signal processing

by adafruit_support_bill on Tue Aug 07, 2012 4:40 am

Code: Select all | TOGGLE FULL SIZE
float mAval = (raw1*20)/1024;

All the terms in your expression are integers. Try making the multiplier a floating point number as in the example I provided.

What's happening is that all the arithmetic on the right side of the '=' is being done as integer arithmetic, so you're losing the fractional part. The conversion to floating point is done after the calculation is completed. By using 20.0 for your multiplier, you are forcing the compiler to convert raw1 to floating point before the multiply and divide operations are executed.
User avatar
adafruit_support_bill
 
Posts: 29218
Joined: Sat Feb 07, 2009 9:11 am