# map analogRead to voltage value

if there a way/method/formula to convert an analogRead value to a voltage value?

I think I could use the 'map' function to do the conversion. So I guess I'm wondering if the analog to digital converter is accurate enough to do this? I'm assuming that the value of 1023 would be 5V? 511 would be 2.5V? ect...

I just want to know the input voltage value of a piezo element when I 'tap' it or 'knock' on it.

Thanks

Yes the map function will do as you say. if mapped from 0-1023 counts to 0-5 then each count would represent 4.9 millivolts.

There are a couple of methods to get better (higher) resolution if what you are attempting to measure is too low in voltage to obtain decent readings. The first is to wire a stable lower voltage to the analog reference pin on the AVR chip and set the software to activate external reference voltage. The other method is to utilize op amps to amplify lower voltage ranges up to the 5vdc maximum input voltage.

Measuring your pizo's output voltage may be a challenge as it generates a small AC voltage and without rectification and amplification you will probably not get a useful measurement.

Lefty

The map function uses long, so you won't get a float back. this function will return the supply voltage 0.0 - 5.0 based on the analog reading of 0-1023

void loop() {

int raw = analogRead(analoginput); float val = fmap(raw, 0, 1023, 0.0, 5.0); Serial.println(val); //voltage delay(1000); }

float fmap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }

Have a look at my website, I put an example up on how I did mine. Measuring 0-20V with a voltage divider, and then mapping to something usefull again.

http://bld.is-a-geek.com/wordpress/2010/10/18/voltage-dividers-and-arduino/