Ohms law basic calculations for calculating R2 in voltage divider when R1 is known and to calculate voltage drop ratio when both R1 and R2 are known.

Committer:
joeata2wh
Date:
Sat Apr 02 03:48:37 2016 +0000
Revision:
3:b4592b0ae1e3
Parent:
0:fe642275688d
basic data log working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeata2wh 0:fe642275688d 1 /* Ohms law used to calculate things like resistance from voltage drop
joeata2wh 3:b4592b0ae1e3 2 and to compute read voltage adjusted for dividor.
joeata2wh 3:b4592b0ae1e3 3
joeata2wh 3:b4592b0ae1e3 4 By Joseph Ellsworth CTO of A2WH
joeata2wh 3:b4592b0ae1e3 5 Take a look at A2WH.com Producing Water from Air using Solar Energy
joeata2wh 3:b4592b0ae1e3 6 March-2016 License: https://developer.mbed.org/handbook/MIT-Licence
joeata2wh 3:b4592b0ae1e3 7 Please contact us http://a2wh.com for help with custom design projects.
joeata2wh 3:b4592b0ae1e3 8
joeata2wh 3:b4592b0ae1e3 9
joeata2wh 3:b4592b0ae1e3 10 */
joeata2wh 0:fe642275688d 11
joeata2wh 0:fe642275688d 12 #ifndef ohms_H
joeata2wh 0:fe642275688d 13 #define ohms_H
joeata2wh 0:fe642275688d 14 #include "mbed.h"
joeata2wh 0:fe642275688d 15
joeata2wh 0:fe642275688d 16 float volDivideCalcRatio(long r1, long r2);
joeata2wh 0:fe642275688d 17 float voltDivideAdjVolt(float vin, long r1, long r2);
joeata2wh 0:fe642275688d 18 long calcResistV(long refResistR1, float maxV, float measuredV);
joeata2wh 0:fe642275688d 19
joeata2wh 0:fe642275688d 20 /* Uses a voltage dividor to compute resistance of
joeata2wh 0:fe642275688d 21 of R2 based on a known R1 and a source (refVolt) based
joeata2wh 0:fe642275688d 22 on drop of voltage measured where R1 and R2 connect.
joeata2wh 0:fe642275688d 23 normaly used to measure resistance in variable resistance
joeata2wh 0:fe642275688d 24 circuits such as thermister.
joeata2wh 0:fe642275688d 25
joeata2wh 0:fe642275688d 26 class VoltDivideAdjVolt {
joeata2wh 0:fe642275688d 27
joeata2wh 0:fe642275688d 28 public:
joeata2wh 0:fe642275688d 29 resistVoltDivide(PinName pin, long r1, float refVolt) {
joeata2wh 0:fe642275688d 30 _r1 = r1;
joeata2wh 0:fe642275688d 31 _refVolt = refVolt;
joeata2wh 0:fe642275688d 32 _pinName = pinName;
joeata2wh 0:fe642275688d 33 }
joeata2wh 0:fe642275688d 34 ~resistVoltDivide();
joeata2wh 0:fe642275688d 35 float read();
joeata2wh 0:fe642275688d 36
joeata2wh 0:fe642275688d 37
joeata2wh 0:fe642275688d 38 private:
joeata2wh 0:fe642275688d 39 PinName _pin;
joeata2wh 0:fe642275688d 40 long _r1;
joeata2wh 0:fe642275688d 41 long _r2;
joeata2wh 0:fe642275688d 42 float _refVolt;
joeata2wh 0:fe642275688d 43 };
joeata2wh 0:fe642275688d 44 */
joeata2wh 0:fe642275688d 45
joeata2wh 0:fe642275688d 46 /* adjVoltDivide Compute the actual voltage output after effect
joeata2wh 0:fe642275688d 47 of voltage dividor has been removed. Normally used to compute
joeata2wh 0:fe642275688d 48 things like true battery voltage when voltage has been level
joeata2wh 0:fe642275688d 49 shifted into a range safe for use with micro-controller Assumes
joeata2wh 0:fe642275688d 50 R1 is connected to source voltage = refVolt and R2 is connected
joeata2wh 0:fe642275688d 51 to ground measurement is where R2 and R1 meet. Result of read()
joeata2wh 0:fe642275688d 52 will be voltage as if it had been measured without influence of
joeata2wh 0:fe642275688d 53 divider.
joeata2wh 0:fe642275688d 54 class adjVoltDivide {
joeata2wh 0:fe642275688d 55
joeata2wh 0:fe642275688d 56 public:
joeata2wh 0:fe642275688d 57 adjVoltDivide(long r1, long r2, float refVolt);
joeata2wh 0:fe642275688d 58 ~adjVoltDivide();
joeata2wh 0:fe642275688d 59 float adjust(float vin);
joeata2wh 0:fe642275688d 60 float adjust(int adcin);
joeata2wh 0:fe642275688d 61 float read(AnalogIn apin);
joeata2wh 0:fe642275688d 62 uint16_t read_u16(AnalogIn apin);
joeata2wh 0:fe642275688d 63 float ratio();
joeata2wh 0:fe642275688d 64
joeata2wh 0:fe642275688d 65 private:
joeata2wh 0:fe642275688d 66 long _r1;
joeata2wh 0:fe642275688d 67 long _r2;
joeata2wh 0:fe642275688d 68 float _refVolt;
joeata2wh 0:fe642275688d 69 float _ratio;
joeata2wh 0:fe642275688d 70 };
joeata2wh 0:fe642275688d 71 */
joeata2wh 0:fe642275688d 72 #endif