Probably one of the most helpful sketches on this site.
/* Creator: HazardsMind (Andrew Mascolo) Date: 4/15/2014 Vsource | | [R1] | x-----------Vout | [R2] | | GND */ // all possible single resistor values, in the lowest value. Do not change. float CommonRes[24] = { 1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1 }; void setup() { Serial.begin(115200); ResistorCalc(5, 3, 4.5, true); // source is 5V, lowest possible output = 3, highest = 4.5, show values. //FindR1(3357.14, 12, 5); // this will show you what resistor you need for R1 to get your expected output //FindR2(4700, 5, 3); // Same as above but it will find the resistor for R2 } void loop(){ } // Voltage source, Lowest possible output, highest possible output, show values? void ResistorCalc(float Vs, float VoL, float VoH, bool show) { float R1, R2, possibleVoltage; unsigned long count = 0, With_Duplicates = 0; Serial.println(F("# R1 R2 OUTPUT\n-----------------------")); for (byte i = 0; i < (24 * 7); i++) { R2 = (CommonRes[i % 24] * pow(10, (i / 24)) ); for (byte j = 0; j < (24 * 7); j++) { R1 = (CommonRes[j % 24] * pow(10, (j / 24)) ); possibleVoltage = (Vs * R2) / (R1 + R2); if (R1 != R2) { if ((possibleVoltage >= VoL) && (possibleVoltage <= VoH)) { count++; if (show) { Serial.print(count); Serial.print(F(" ")); Serial.print(R1); Serial.print(F(" ")); Serial.print(R2); Serial.print(F(" ")); Serial.println(possibleVoltage); } } } else With_Duplicates++; } } Serial.print(F("No Dups: ")); Serial.println(count); Serial.print(F("With Dups: ")); Serial.println((count + With_Duplicates)); } void FindR1(long R2, float Vs, float Vo) { Serial.println( ((Vs * R2) - (Vo * R2)) / Vo); } void FindR2(long R1, float Vs, float Vo) { Serial.println( (Vo * R1) / (Vs - Vo)); }
I'm also working on a sketch that will visually show the resistor bands using the UTFT library.