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.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ohms.cpp Source File

ohms.cpp

00001 #include "mbed.h"
00002 
00003 /*     By Joseph Ellsworth CTO of A2WH
00004   Take a look at A2WH.com Producing Water from Air using Solar Energy
00005   March-2016 License: https://developer.mbed.org/handbook/MIT-Licence 
00006   Please contact us http://a2wh.com for help with custom design projects.
00007 
00008  
00009 */
00010 
00011 #include "ohms.h"
00012 
00013 float volDivideCalcRatio(long r1, long r2) {
00014   return ((float) r2 / (float) (r1 + r2));
00015 }
00016 float voltDivideAdjVolt(float vin, long r1, long r2) {
00017   float ratio = ((float) r2 / (float) (r1 + r2));
00018   return vin / ratio;
00019 }
00020 
00021 
00022 long calcResistV(long refResistR1, float maxV, float measuredV) {
00023   float VDrop = maxV - measuredV;
00024   float dropRatio =  measuredV / VDrop;  
00025   return  (long) (refResistR1 * dropRatio);
00026 }
00027 
00028 
00029 /*
00030 
00031 adjVoltDivide::adjVoltDivide(long r1, long r2, float refVolt) {
00032     _r1 = r1;
00033     _r2 = r2;
00034     _refVolt = refVolt;
00035     _ratio = volDivideRatio(r1, r2);
00036 }
00037     
00038 float adjVoltDivide::read(AnalogIn apin) {
00039     return apin.read() / _ratio;    
00040 }
00041 
00042 uint16_t adjVoltDivide::read_u16(AnalogIn apin) {
00043     return (int) ((float) apin.read_u16() / _ratio);
00044 }
00045 
00046 */