Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Ohmmeter_Code.cpp@2:6077294f7007, 2020-04-01 (annotated)
- Committer:
- thomasosullivan
- Date:
- Wed Apr 01 19:45:59 2020 +0000
- Revision:
- 2:6077294f7007
- Parent:
- 1:5aacfef29ca8
- Child:
- 3:b6e887e4ac75
Final with comments;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
decland17 | 0:7f4ed364f7ea | 1 | /* |
thomasosullivan | 2:6077294f7007 | 2 | the code is set up to auto range for different known resistance value |
thomasosullivan | 2:6077294f7007 | 3 | it will base the case statement on its original Restimate value |
thomasosullivan | 2:6077294f7007 | 4 | the voltage drop across the transistor is different for each case |
thomasosullivan | 2:6077294f7007 | 5 | this value was found using simulations from LTspice |
thomasosullivan | 2:6077294f7007 | 6 | a value for Runkown will be printed on the screen and this will be in a |
thomasosullivan | 2:6077294f7007 | 7 | contunious loop from the switch |
decland17 | 0:7f4ed364f7ea | 8 | */ |
decland17 | 0:7f4ed364f7ea | 9 | //Case x: |
decland17 | 0:7f4ed364f7ea | 10 | |
decland17 | 0:7f4ed364f7ea | 11 | lcd.locate(0,0); |
decland17 | 0:7f4ed364f7ea | 12 | lcd.printf("Resistance: "); //display on top line |
decland17 | 0:7f4ed364f7ea | 13 | |
decland17 | 0:7f4ed364f7ea | 14 | DigitalOut 100ohm(D6) = 0; //Transistor connected to 100ohm resistor set to short circuit |
decland17 | 0:7f4ed364f7ea | 15 | DigitalOut 1kohm(D7) = 1; //All others set to open circuit |
decland17 | 0:7f4ed364f7ea | 16 | DigitalOut 10kohm(D8) = 1; |
decland17 | 0:7f4ed364f7ea | 17 | DigitalOut 100kohm(D9) = 1; |
decland17 | 0:7f4ed364f7ea | 18 | DigitalOut 1Mohm(D10) = 1; |
decland17 | 0:7f4ed364f7ea | 19 | DigitalOut 10Mohm(D11) = 1; |
decland17 | 0:7f4ed364f7ea | 20 | |
decland17 | 0:7f4ed364f7ea | 21 | AnalogIn vo(A0); |
decland17 | 0:7f4ed364f7ea | 22 | |
thomasosullivan | 2:6077294f7007 | 23 | double vout = (3.3 * vo)/ 4095; //converts analog read from bits to voltage |
decland17 | 0:7f4ed364f7ea | 24 | |
decland17 | 0:7f4ed364f7ea | 25 | double rest; |
decland17 | 0:7f4ed364f7ea | 26 | |
decland17 | 0:7f4ed364f7ea | 27 | double vt; |
decland17 | 0:7f4ed364f7ea | 28 | |
thomasosullivan | 2:6077294f7007 | 29 | rest = (vout * 100) / (3.3 - vout); //rest is an estimate as it does not account for the voltage drop in transistor |
thomasosullivan | 2:6077294f7007 | 30 | //this essentially decides what pin we will use for final value |
decland17 | 0:7f4ed364f7ea | 31 | switch (rest) |
decland17 | 0:7f4ed364f7ea | 32 | { |
decland17 | 0:7f4ed364f7ea | 33 | case 550: |
thomasosullivan | 2:6077294f7007 | 34 | vt = 0.0647875; // vt is based off ltspice values from simulations |
decland17 | 0:7f4ed364f7ea | 35 | runknown = (vout * 100) / (3.3 - vt - vout); |
decland17 | 0:7f4ed364f7ea | 36 | if (runknown < 10){ |
thomasosullivan | 2:6077294f7007 | 37 | lcd.printf("Out of Range"); //this will be out of the range of our design parameters |
decland17 | 0:7f4ed364f7ea | 38 | } |
decland17 | 0:7f4ed364f7ea | 39 | else{ |
thomasosullivan | 2:6077294f7007 | 40 | lcd.locate(0,1); |
thomasosullivan | 2:6077294f7007 | 41 | lcd.printf("%u Ω", runknown); // prints on the second row of the LCD screen |
decland17 | 0:7f4ed364f7ea | 42 | } |
decland17 | 0:7f4ed364f7ea | 43 | break; |
decland17 | 0:7f4ed364f7ea | 44 | |
thomasosullivan | 2:6077294f7007 | 45 | case 5500: // this case is for if R is in a different range |
decland17 | 0:7f4ed364f7ea | 46 | vt = 0.0189; |
decland17 | 0:7f4ed364f7ea | 47 | 100ohm = 1; |
decland17 | 0:7f4ed364f7ea | 48 | 1kohm = 0; |
decland17 | 0:7f4ed364f7ea | 49 | vout = (3.3 * vo)/ 4095; |
decland17 | 0:7f4ed364f7ea | 50 | runknown = (vout * 1000) / (3.3 - vt - vout); |
decland17 | 0:7f4ed364f7ea | 51 | lcd.locate(0,1); |
decland17 | 0:7f4ed364f7ea | 52 | if (runknown < 1000){ |
thomasosullivan | 2:6077294f7007 | 53 | lcd.printf("%u Ω", runknown); |
thomasosullivan | 2:6077294f7007 | 54 | } // this if correct runknown to the appropriate magnitude |
decland17 | 0:7f4ed364f7ea | 55 | else { |
thomasosullivan | 2:6077294f7007 | 56 | runknown = runknown / 1000; //runkown is now printed in KΩ |
decland17 | 0:7f4ed364f7ea | 57 | lcd.printf("%.2f kΩ", runknown); |
decland17 | 0:7f4ed364f7ea | 58 | break; |
decland17 | 0:7f4ed364f7ea | 59 | |
decland17 | 0:7f4ed364f7ea | 60 | case 55000: |
decland17 | 0:7f4ed364f7ea | 61 | vt = 0.0075148; |
decland17 | 0:7f4ed364f7ea | 62 | 100ohm = 1; |
decland17 | 0:7f4ed364f7ea | 63 | 10kohm = 0; |
decland17 | 0:7f4ed364f7ea | 64 | vout = (3.3 * vo)/ 4095; |
decland17 | 0:7f4ed364f7ea | 65 | runknown = (vout * 10000) / (3.3 - vt - vout); |
decland17 | 0:7f4ed364f7ea | 66 | lcd.locate(0,1); |
decland17 | 0:7f4ed364f7ea | 67 | runknown = runknown / 1000; |
decland17 | 0:7f4ed364f7ea | 68 | lcd.printf("%.2f kΩ", runknown); |
decland17 | 0:7f4ed364f7ea | 69 | break; |
decland17 | 0:7f4ed364f7ea | 70 | |
decland17 | 0:7f4ed364f7ea | 71 | case 550000: |
decland17 | 0:7f4ed364f7ea | 72 | vt = 0.00612; |
decland17 | 0:7f4ed364f7ea | 73 | 100ohm = 1; |
decland17 | 0:7f4ed364f7ea | 74 | 100kohm = 0; |
decland17 | 0:7f4ed364f7ea | 75 | vout = (3.3 * vo)/ 4095; |
decland17 | 0:7f4ed364f7ea | 76 | runknown = (vout * 100000) / (3.3 - vt - vout); |
decland17 | 0:7f4ed364f7ea | 77 | |
decland17 | 0:7f4ed364f7ea | 78 | lcd.locate(0,1); |
decland17 | 0:7f4ed364f7ea | 79 | |
decland17 | 0:7f4ed364f7ea | 80 | if (runknown < 1000000){ |
decland17 | 0:7f4ed364f7ea | 81 | runknown = runknown / 1000; |
decland17 | 0:7f4ed364f7ea | 82 | lcd.printf("%.2f kΩ", runknown); |
decland17 | 0:7f4ed364f7ea | 83 | } |
decland17 | 0:7f4ed364f7ea | 84 | else { |
thomasosullivan | 2:6077294f7007 | 85 | runknown = runknown / 1000000; // this will change the magnitude to MΩ |
decland17 | 0:7f4ed364f7ea | 86 | lcd.printf("%.2f MΩ", runknown); |
decland17 | 0:7f4ed364f7ea | 87 | break; |
decland17 | 0:7f4ed364f7ea | 88 | |
decland17 | 0:7f4ed364f7ea | 89 | case 5500000: |
decland17 | 0:7f4ed364f7ea | 90 | vt = 0.0059684; |
decland17 | 0:7f4ed364f7ea | 91 | 100ohm = 1; |
decland17 | 0:7f4ed364f7ea | 92 | 1Mohm = 0; |
decland17 | 0:7f4ed364f7ea | 93 | vout = (3.3 * vo)/ 4095; |
decland17 | 0:7f4ed364f7ea | 94 | runknown = (vout * 1000000) / (3.3 - vt - vout); |
decland17 | 0:7f4ed364f7ea | 95 | |
decland17 | 0:7f4ed364f7ea | 96 | lcd.locate(0,1); |
decland17 | 0:7f4ed364f7ea | 97 | runknown = runknown / 1000000; |
decland17 | 0:7f4ed364f7ea | 98 | lcd.printf("%.2f MΩ", runknown); |
decland17 | 0:7f4ed364f7ea | 99 | break; |
decland17 | 0:7f4ed364f7ea | 100 | |
decland17 | 0:7f4ed364f7ea | 101 | case 10000000: |
decland17 | 0:7f4ed364f7ea | 102 | vt = 0.0059526; |
decland17 | 0:7f4ed364f7ea | 103 | 100ohm = 1; |
decland17 | 0:7f4ed364f7ea | 104 | 10Mohm = 0; |
decland17 | 0:7f4ed364f7ea | 105 | vout = (3.3 * vo)/ 4095; |
decland17 | 0:7f4ed364f7ea | 106 | runknown = (vout * 10000000) / (3.3 - vt - vout); |
decland17 | 0:7f4ed364f7ea | 107 | |
decland17 | 0:7f4ed364f7ea | 108 | lcd.locate(0,1); |
decland17 | 0:7f4ed364f7ea | 109 | runknown = runknown / 1000000; |
decland17 | 0:7f4ed364f7ea | 110 | lcd.printf("%.2f MΩ", runknown); |
decland17 | 0:7f4ed364f7ea | 111 | break; |
decland17 | 0:7f4ed364f7ea | 112 | |
decland17 | 0:7f4ed364f7ea | 113 | default: |
decland17 | 0:7f4ed364f7ea | 114 | lcd.locate(0,1); |
thomasosullivan | 2:6077294f7007 | 115 | lcd.printf("Out of Range"); // this will apear if runkown is outside the design parameters |
decland17 | 0:7f4ed364f7ea | 116 | break; |
decland17 | 0:7f4ed364f7ea | 117 | |
decland17 | 0:7f4ed364f7ea | 118 | } |