determine unknown resistor in a voltage divider

I know this is simple, but i was pleased as punch to make this little app that will output the ohms of an unknown resistor in a voltage divider when you only know the R of one of the two. I'm applying this to making an NTC based temp probe app.

#include <stdio.h>
#include <math.h>

int analoginput=4;
int vin=5;
int knownResistor = 1000;

void setup() {
pinMode(analoginput, INPUT);
Serial.begin(9600);
}

void loop()
{

int raw = analogRead(analoginput);
float vout = fmap(raw, 0, 1023, 0.0, 5.0);
int unknownResistance = knownResistor * vout/(vin-vout);

Serial.println(unknownResistance);
delay(1000);
}

float fmap(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Simple and good. This inspires me to make my students do the same. I have a box of mixed up resistors (accumulated over the years, not my fault ;D). I will let them each write a program like yours, take 25 resistors, measure them with arduino, measure them with multimeter, read them off the color-resistance chart, then put them back to their drawers! They'll enjoy it and I'll have one less box of mess.

ahhh Vout = Vin / (R1+R2) * R2 = R1

Its all coming back to me now :slight_smile: