Author Topic: AnalogInput units  (Read 732 times)

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: AnalogInput units
« on: March 22, 2017, 07:50:33 am »
Maria,

OK I'm confused by what you mean by "reading". If you just take the value from the analog pin (no math whatsoever), you get the voltage in mV. ie 2.8V = 2800, 3.3V = 3300 etc. up to 3600 (3.6V) which is the maximum you can read. You can read this as a short or an integer.

I am doing this in several situations, and it works exactly as predicted. I haven't tried reading higher than the supply voltage though.

The problem is that having the value in mV is not very useful, as you have to translate it into some sort of meaningful number, and most libraries/code expects units, not mV.

The two formulae's I gave you convert the mV to units. One does 0-16384 (14 bit) the other 0-1024 (10 bit). it's simple, divide the analog pin value (mV) by the reference value (3600) and multiply by whatever you want as full range, to get the arduino-style number. Just keep your units and data types straight. ie 3600 (not 3.3), integer division (not float), in integer maths you multiply before you divide, to avoid rounding errors, and so on.

Just FYI, on your particle electron, analogRead() reads units, not volts. On the mc120, analogRead() returns volts, not units. It's the other way round. I think this is the root of most of your confusion.

If you can post your whole code, maybe I can spot what is going wrong...
« Last Edit: March 22, 2017, 08:05:31 am by Nick_W »
Like Like x 1 View List