Analog signal processing

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
TESBill
 
Posts: 5
Joined: Thu Aug 02, 2012 11:38 pm

Analog signal processing

Post by TESBill »

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?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Analog signal processing

Post by adafruit_support_bill »

Something like this: (substitute appropriate pin numbers for your project)

Code: Select all

int raw = analogRead(analogPin);  // get the raw reading
float volts = (raw * 5.0)/1024; // scale raw reading to volts

TESBill
 
Posts: 5
Joined: Thu Aug 02, 2012 11:38 pm

Re: Analog signal processing

Post by TESBill »

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);
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Analog signal processing

Post by adafruit_support_bill »

Code: Select all

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.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”