//Pin declaration const int SupplyVoltageSensorPin = A0; const int ResistorVoltageSensorPin = A1; //Variables declaration float SupplyVoltageSensorRead; //Supply Voltage read from analog input float SupplyVoltageVal; //Supply Voltage real Value float ResistorVoltageSensorRead; //Fixed Resistor Voltage read from analog input float ResistorVoltageVal; //Fixed Resistor real Value float ResistorVal; //Unknow Resistor calculated value float FixedResistor = 100; //Fixed resistor of the voltage divider (2200 ohms) float CurrentVal; //Amount of current flowing in the circuit void setup() { Serial.begin(9600); //Initialize serial port } void loop() { //------------------------------------------ //STEP 1 : Supply Voltage value reading and scaling //------------------------------------------ for (int i = 1; i < 50; i++) { //Calculate the average between 10 read to avoid value floating mistakes SupplyVoltageSensorRead += analogRead(SupplyVoltageSensorPin); //Read the value from sensor delay(2); } SupplyVoltageSensorRead = SupplyVoltageSensorRead / 50.0; SupplyVoltageVal = SupplyVoltageSensorRead * (5.0 / 1024.0); //Convert the read value in the real value (sensor read between 0 and 25V) //------------------------------------------ //STEP 2 : Divider fixed resistor voltage value reading and scaling //------------------------------------------ for (int i = 1; i < 50; i++) { //Calculate the average between 10 read to avoid value floating mistakes ResistorVoltageSensorRead += analogRead(ResistorVoltageSensorPin); //Read the value from sensor delay(2); } ResistorVoltageSensorRead = ResistorVoltageSensorRead / 50.0; ResistorVoltageVal = ResistorVoltageSensorRead * (5.0 / 1024.0); //Convert the read value in the real value (sensor read between 0 and 25V) //------------------------------------------ //STEP 3 : Resistor value calculating //------------------------------------------ ResistorVal = ((SupplyVoltageVal - ResistorVoltageVal) * FixedResistor) / ResistorVoltageVal; //Applying Voltage divider formula //------------------------------------------ //STEP 4 : Current Calculating //------------------------------------------ CurrentVal = SupplyVoltageVal / (FixedResistor + ResistorVal); //Calculate the current using ohm's law CurrentVal = CurrentVal * 1000; //Conversion in milliampere //------------------------------------------ //STEP 5 : Print values on serial monitor //------------------------------------------ Serial.print ("SUPPLY VOLTAGE: "); Serial.print (SupplyVoltageVal); Serial.println (" V"); Serial.print ("FIXED RESISTOR VOLTAGE: "); Serial.print ( ResistorVoltageVal); Serial.println (" V"); Serial.print ("RESISTENCE: "); Serial.print (ResistorVal); Serial.println (" Ohm"); Serial.print ("CURRENT: "); Serial.print (CurrentVal); Serial.println (" mA"); Serial.println(); delay (2000); }