Analog signal processing
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 5
- Joined: Thu Aug 02, 2012 11:38 pm
Analog signal processing
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?
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Analog signal processing
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
-
- Posts: 5
- Joined: Thu Aug 02, 2012 11:38 pm
Re: Analog signal processing
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);
}
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);
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Analog signal processing
Code: Select all
float mAval = (raw1*20)/1024;
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.
Please be positive and constructive with your questions and comments.