In this tutorial we show how to make ohmmeter and voltmeter using Arduino. Ohmmeter is an instrument that measure resistance across a resistor and voltmeter measures the voltage across a component. Here the values are displayed in correct units Ohm, KOhm and MOhm.
Arduino Ohmmeter & Voltmeter Circuit Diagram
The following shows the circuit diagram of how to wire the resistor whose resistance value we want to calculate and the voltage across which we want to calculate.
In the above circuit diagram the R_REF resistor is a reference resistor and the R_VAL is the resistor whose unknown value we want to determine. The reference resistor R_REF which has a value of 1KOhm here should be exact because it will be used to calculate the the value of the R_VAL resistor. The two resistors, R_REF and R_VAL forms a voltage divider with one end connected to +5V of Arduino and the other end connected to Arduino ground. The junction of these two resistors is connected to the analog pin A1 of the Arduino Nano as shown in the diagram above. A 16x2 LCD is connected as shown which is used to display the value of the unknown resistor R_VAL and the voltage at the junction of the two resistor, that is, the voltage input to the analog pin A1. In the code we also display the value of the unkown resistor R_VAL and the voltage on the serial monitor besides on the LCD.
The mathematics behind calculating the value of the R_VAL resistor resistance and the voltage at the input of the analog pin is as follows.
We know from the voltage divider rule that the voltage \(V_{out}\) at the junction is given by,
\(V_{out}=\frac{R_{VAL}}{R_{REF}+R_{VAL}} V_{in}\)
The input voltage \(V_{in}\) is +5V and the value of \(R_{REF}\) is 1KOhm. From this equation we have to find the \(R_{VAL}\) and \(V_{out}\).
If we rearrange the above equation to solve for \(R_{VAL}\) we get,
\(R_{VAL} = \frac{V_{out}}{V_{in}-V_{out}}R_{REF}\) ................(1)
Since \(R_{REF}\) and \(V_{in}\) are known quantities we only need to know the value of \(V_{out}\) to calculate the value of \(R_{VAL}\).
The value of \(V_{out}\) can be calculated using the fact that this is the voltage read(\(V_{read}\)) in by the Arduino ADC(Analog to Digital Converter) which is as follows.
\(V_{out} = \frac{V_{read}}{1024}V_{in}\) .............(2)
Since \(V_{read}\) can have value between 0 and 1024.
So we first find out \(V_{read}\) value using the analogRead() function and then using this we find the \(V_{out}\) value using equation(2) and then find the the \(R_{VAL}\) using equation(1). These two values, \(R_{VAL}\) and \(V_{out}\) are the values we wanted to determine and display on the LCD and serial monitor.
Arduino Ohmmeter and voltmeter Program Code
The following is the Arduino program code to calculate the resistor value and the voltage as described above.
#include <LiquidCrystal.h>
#define RS 7
#define E 6
#define D4 5
#define D5 4
#define D6 3
#define D7 2
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);
const int resistorPin = A1;
float Vin = 5.0;
float Vout = 0;
float Rref = 1000;
unsigned long Rval=0;
char RvalBuffer[40];
char VoutBuffer[20];
void setup(){
Serial.begin(9600);
lcd.begin(16,2);
//display initial message on LCD
lcd.setCursor(0,0);
lcd.print("Ard. Ohmmeter");
lcd.setCursor(0,1);
lcd.print("& Voltmeter");
//display initial message on Serial Monitor
Serial.println("Arduino Ohmmeter & voltmeter");
Serial.println("****************************************");
delay(1000);
//clear LCD
lcd.clear();
}
void loop(){
int Vread = analogRead(resistorPin); // reads the input
Vout = (Vread * Vin) /1024.0; // calculates the voltage on the input pin
Rval = (Vout*Rref)/(Vin - Vout);
//check and display in Resistor value with correct units
if(0 <= Rval && Rval < 1000){
//Convert to string and store in buffers
dtostrf(Rval, 3, 2, RvalBuffer);
strcat(RvalBuffer,"Ohm");
dtostrf(Vout,3,2,VoutBuffer);
strcat(VoutBuffer,"V");
//display on LCD
lcd.setCursor(0,0);
lcd.print("Rval:");
lcd.print(RvalBuffer);
lcd.setCursor(0,1);
lcd.print("Vout:");
lcd.print(VoutBuffer);
//display on serial monitor
Serial.print("R value: "); //Resistor Value
Serial.println(RvalBuffer);
Serial.print("Vout: "); //voltage Value
Serial.println(VoutBuffer);
Serial.println("--------------------");
}
else if(1000 <= Rval && Rval < 1000000){
Rval = Rval/1000;
//Convert to string and store in buffers
dtostrf(Rval, 3, 2, RvalBuffer);
strcat(RvalBuffer,"KOhm");
dtostrf(Vout,3,2,VoutBuffer);
strcat(VoutBuffer,"V");
//display on LCD
lcd.setCursor(0,0);
lcd.print("Rval:");
lcd.print(RvalBuffer);
lcd.setCursor(0,1);
lcd.print("Vout:");
lcd.print(VoutBuffer);
//display on serial monitor
Serial.print("R value: "); //Resistor Value
Serial.println(RvalBuffer);
Serial.print("Vout: "); //voltage Value
Serial.println(VoutBuffer);
Serial.println("--------------------");
}
else if(1000000 <= Rval){
Rval = Rval/1000000;
//Convert to string and store in buffers
dtostrf(Rval, 3, 2, RvalBuffer);
strcat(RvalBuffer,"MOhm");
dtostrf(Vout,3,2,VoutBuffer);
strcat(VoutBuffer,"V");
//display on LCD
lcd.setCursor(0,0);
lcd.print("Rval:");
lcd.print(RvalBuffer);
lcd.setCursor(0,1);
lcd.print("Vout:");
lcd.print(VoutBuffer);
//display on serial monitor
Serial.print("R value: "); //Resistor Value
Serial.println(RvalBuffer);
Serial.print("Vout: "); //voltage Value
Serial.println(VoutBuffer);
Serial.println("--------------------");
}
else{
lcd.setCursor(0,1);
lcd.print("Out of Range");
Serial.print("Out of Range");
}
delay(1000);
}
In the above code, first we have setup the LiquidCrystal display library to use the LCD with Arduino. Next we have created an alias name resistorPin for analog pin A1 for easier coding purpose. Next we have created couple of float variables and array variables to hold the string value to be displayed on the LCD and serial monitor.
In the setup() function we have initialized the LCD and the serial monitor with baud rate 9600. Next we have printed some initial message on the LCD and the serial monitor. The tutorial display temperature on lcd arduino explains in details how to use LCD with Arduino.
In the loop() function, we read in the analog value using the analogRead() function on pin resistorPin. The read in value is stored in local int variable called Vread which has digital representation in the range of 0 to 1023 corresponding to range 0V to 5V. This Vread value is converted to analog voltage value Vout with data type of float. Once we have the Vout value we then calculate the R_val as described before using the fact that Rref is set to 1KOhm and Vin is set to 5V(the value for these variables are declared at the top). Then using the if statements we have printed out the resistor value in Ohm, KOhm and MOhm range by dividing the Rval which is in Ohm with either 1, 1000 and 1000000 for each range. In each range, since the Rval is in float we first convert it to string using the dstrof() function with 3 digits and 2 decimal value and store it the buffer variable RvalBuffer[]. Then to add the unit we use strcat() to add Ohm, KOhm and MOhm for resistor Rval and V unit for voltage Vout and store them in the array variables which are declared at the top of the program. These values stored in the buffers are then printed out on LCD and serial monitor in each range of the resistor. In this way we get an automatic range Ohmmeter.
The next step is to compile and upload the code. You can use either Arduino IDE of the PlatformIO IDE for Arduino.
Video demonstration of Arduino Ohmmeter and Voltmeter
The following video demonstrates how the Arduino Nano based Ohmmeter and Voltmeter works.
So in this Arduino programming tutorial we showed how one can build an Ohmmeter and Voltmeter using Arduino Nano. We also provided Arduino program code for display the resistor value and voltage on LCD and serial monitor with automatic range indicator.
Arduino can be useful to make other kinds of simple test instruments such as Ohmmeter illustrated in this tutorial. Following are tutorials to build different kinds of scientific and engineering testing equipment with Arduino you might be interested in.
- Inductance Meter using Arduino
