Analogin read_u16 question

31 Aug 2010

Hi guys, i'm trying to get a voltage value from an analog gyroscope that works at 3.3 V. The reading when there's no movement should be 1,65 V and my multimeter can confirm this, also if i use the analogin "read" i get values around 0.5 and that's ok.

However i need the actual voltage value so i tried the method read_u16 but when i printf to serial i get values around 33288 if i cast to int,otherwise i get 18 ..48 ..248 18 and so on.... what's this?

I just need advice on how to make calculations with the voltage value obtained and how to print it to screen! it's a simple u char but i can't get out of it :D

Thanks a lot

31 Aug 2010 . Edited: 31 Aug 2010

Hi Pietro,

The value is normalised (between 0.0-1.0), so just do:

AnalogIn normalised(p19);
float voltage = 3.3 * normalised;

For read_u16(), it is just normalised to a 16-bit value instead.

Simon

And then to print to the terminal:

printf("%f\n", voltage);

31 Aug 2010

Ok i understand this for the "read" but if i want to see 1,65 from the u16 value, what i have to do?

Thanks a lot

31 Aug 2010

Ok undestood i get 33..... because it's 65../2. Ok i thought i could get the real reading using u16. but what is u16 useful for then? is it more precise?

01 Sep 2010

v = (read_u16() /65535) * 3.3 should work.

An integer read (read_u16) is usefull where you need to process the data quickly. Integer operations are (much) faster than floating point operations. An integer operation takes 1 or 2 cycles, whereas floating point operations can take up to 50-100 cycles. The precesion remains the same (i.e. you can't get more information using the integer read).

If you require fast performace, you should do all calculations with ints and convert to a float at the very end (if needed). This limits the number of (expensive) floating point operations.

Most of the time however, it's ok to indulge in floating point operations without concequence.