# ARDUINO / DASDUINO VOLTMETER PROJECT

## INTRODUCTION

This voltmeter project is a continuation of  a tutorial on INPUT / OUTPUT pins.

WARNING: Be careful when testing voltage. Atmega328 bears a maximum voltage of 5V, if you want to measure higher voltages skip to the part of the resistant divider (voltage divider).

## CONNECTING

We need two cables: GND (-) and (+). GND connects to GND, and (+) to measure the analog pin 0 Dasduino. By no means, do not connect voltage greater than 5V to Dasduino!

## CODE

If you don’t understand the code, we suggest that you look at the tutorial on the input / output pins.

`int` `input = A0;`
`void` `setup``() {`
`    ``pinMode``(input, ``INPUT``);`
`    ``Serial.begin(9600); ``// starting serial communication`
`                        ``// We will review the data in Serial Monitor`
`}`
`void` `loop``() {`
`    ``int` `inputVoltage_analog = ``analogRead``(input);  ``// reading analog values`
`    ``float` `inputVoltage_volt = ``map``(inputVoltage_analog, 0, 1023, 0, 5); ``// converts the analog values ​​in voltage Serial.print("Input voltage = "); Serial.println(inputVoltage_volt); }`

## VOLTAGE DIVIDER

Voltage divider or resistance divider is a series of two resistors which descend voltage. Resistor closer to the input voltage (Vin) is denoted as R1, and resistor closer to gnd is denoted as R2. The voltage drop across R2 is Vout, and marks the divided voltage of our circuit. In the example above, we use R1 = 10K and R2 = 1kΩ. It is calculated according to the formula:

That is, if the value of the input voltage (Vin) is equal to 40V, output (Vout) will be at Vout = 40 * (1 / (10 + 1)) = 3.636V. This is the voltage that the microcontroller can handle. The maximum input voltage for this combination of resistor is 55V, but for safety, let’s call it 0-40VDC voltmeter.

This formula will be included in the Arduino code, and it is up to us to be careful not to cross the maximum allowable input voltage.

`int` `r1= 10; ``// r1 is 10k ohms`
`int` `r2 = 1; ``// r2 is 1k ohm`
`int` `input = A0; ``// voltage is measured via analog pins A0`
`void` `setup``() {`
`    ``pinMode``(input, ``INPUT``);`
`    ``Serial.begin(9600); ``// starting serial communication`
`}`
`void` `loop``() {`
`    ``// summon the function that returns the corrected input voltage`
`    ``float` `inputVoltage = VoltageDivider(input);`
`   ``// printing voltage`
`    ``Serial.print(``"Input voltage = "``);`
`    ``Serial.println(inputVoltage);`
`}`
`void` `VoltageDivider(``int` `pin) {`
`    ``int` `vout_a = ``analogRead``(pin);  ``// The first step is to read the analog value of Vout voltage divided with voltage divider`
`    ``float` `vout_v = ``map``(vin_a, 0, 1023, 0, 5);  ``// mapping the printed value in voltage`
`    ``float` `vin = vout_v * ( (r1+r2) / r2 );  ``// Since we know the value of the resistor, the voltage at pin A0 is converted to the actual voltage`
`                                          ``// Vin, before voltage divider`
`    ``return` `vin;  ``// return restated value`
`}`

The accuracy of the voltmeter can easily be calculated by dividing 5V / 1023, which gives us a step of 4,88mV. Depending on the resistors that you use, there will be a deviation of the actual and the read voltage. If you have the option, compare the voltmeter with the laboratory one.