The PC Meter Arduino Code

Intro | Building the Device | Arduino Code | Desktop Application | Download & More

The Arduino Program

Quick Overview

The Arduino program continuously reads the virtual COM port for incoming data. The data format consists of a single letter identifier, followed by the reading and then a line break. Here is an example:


In this example, CPU load is at 15% and memory usage is 45%. The format allows for additional stats to be added, each using a single character as an identifier.

The reading for the CPU load is "smoothed" by averaging the last four values received. This makes the movement of the meter less "jumpy". Memory is not smoothed since the values are more stable. The values are then converted to match the range of values needed by the analogWrite function and the corresponding outputs are updated to make the meters move.

The LEDs are bicolor, with two anodes: one for green, and one for red. Each anode is wired to a separate output on the Arduino. When a stat is below 80% then the corresponding green LED is lit, and when it's 80% or higher red is lit instead.

If the program goes for more than three seconds without valid data to parse, it enters a "screen saver" mode where it moves the needles of the meter back and forth in opposing directions to show that it's running but not receiving valid data.

Setting the PWM output range

The Arduino's analogWrite function is used to set the needle position of the meters. The maximum value is 255, which gives an output of 5v. However, this may not be the "true" max value for the meters, depending on various circumstances.

In the meter I built, the first issue is the meters and resistors. If I had used 0-5V meters, they could be wired directly to the Arduino and no further circuitry would be required. But I used 0-1mA meters instead, which measure current. So a resistor is needed to create that current. At 5V, a 5kΩ of resistance is needed to get 1mA of current:

5V / 5,000Ω = 0.001A, or 1mA

So the ideal resistor to use would be a 5kΩ resistor with 1% accuracy or better. But that's not common, and it could be pricey. So I used 4.7kΩ 5% resistor instead, which are common. Less resistance means more current:

5V / 4,700Ω = 0.00106A, or approximately 1.1mA

So setting analogWrite to 255 will slightly overdrive the meter. In addition to this problem, it's also possible the meters aren't 100% accurate (they're very inexpensive) so it's good to have a way to adjust the output. That's what the constants METER_A_MAX and METER_B_MAX are for - they can be adjusted to match a specific meter/resistor combination.

Posted 3/22/2018