# Simulate voltage meter

## Objective

The course will show you how to simulate a multimeter with Microduino. The reading of the “multimeter” will be displayed on Processing.

## Equipment

• Other Equipment
• A USB cable
• A resistor of 1M Ω
• A resistor of 100K Ω
• A box of jumpers

## Schematic Diagram

The schematic diagram adopts A2 pin. There are four channels totally, corresponding to the pins of A0, A1, A2, A3 respectively. The voltage reading will be displayed in Channel One.

## Program

Referring to AnalogVoltmeter

AnalogVoltmeterProcessing

## Debugging

Step 1：Building the schematic diagram according to hardware environment, as follows:

Step 2：Here is the code we need:

The code of the two ends (Processing and Microduino) Microduino:

//The voltage value will be received by Processing via serial port output

``` void loop()
{
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES) {
// sample each channel A2 to A5
for (l_cnt = 0; l_cnt < 4; l_cnt++) {
}
sample_count++;
delay(10);
}
// calculate the voltage for each channel
for (l_cnt = 0; l_cnt < 4; l_cnt++) {
voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;
}

// each voltage is multiplied by the resistor network
// division factor to calculate the actual voltage
voltage = voltage * DIV_1;
voltage = voltage * DIV_2;
voltage = voltage * DIV_3;
voltage = voltage * DIV_4;

// send voltages to Processing application via serial port / USB
// voltage 1 - A (pin A2)
Serial.print("A ");
Serial.print(voltage, 1);
Serial.print("V ");
// voltage 2 - B (pin A3)
Serial.print("B ");
Serial.print(voltage, 1);
Serial.print("V ");
// voltge 3 - C (pin A4)
Serial.print("C ");
Serial.print(voltage, 1);
Serial.print("V ");
// voltage 4 - D (pin A5)
Serial.print("D ");
Serial.print(voltage, 1);
Serial.print("V ");
Serial.println("");
delay(10);
// reset count and sums
sample_count = 0;
for (l_cnt = 0; l_cnt < 4; l_cnt++) {
sum[l_cnt] = 0;
}
}
```

Processing:

//After getting the data of the first serial port

```   println(Serial.list());
// modify Serial.list() to select correct serial port
```

ser_port = new Serial(this, Serial.list(), 9600);

Function Description:

Serial Event: serialEvent(Serial p)

Displaying the channel and voltage: voltage (int channel, String volts)

Drawing graph axis: DrawGraphAxis(int pos_x, int pos_y, int width, int height)

Drawing graph: DrawGraph (String voltage, int channel, int pos_x, int pos_y, int scale)

Drawing scale and radio box: DrawScaleSelect (int x_pos, int y_pos, int scale)