Search This Blog

Sunday, July 14, 2013

LCD system monitor for PC case bling using Arduino

This post is about a project to build a CPU/system monitor display for the front of a Windows desktop computer. Lights that get brighter as the CPU load increases.  A little fun and bling for the new machine.

I'm building a new high end desktop PC, and due to a delay in getting the processor shipped to me I've been wasting time thinking of what accessories to fill up those unused 5.25" bays.

I saw some widgets with LCDs to display temperature and fan speeds, etc.  However after reading reviews I'm not thrilled with any of them.   Additionally they don't read the real temperatures, they have probes you glue on.

That got me thinking of a quickie project that would be fun.   I'll make a panel LCD myself,   I'd like to display the CPU load, or other parameters, and temperatures if I can.  I know it's fairly hard to pull the temperatures from motherboards due to the wide variety of designs.

The basic design will be this.  I'll use an Arduino with an LCD plugged into either a USB port or onto a USB header on the mother board.   It will be a fairly simple serial client that displays whatever text you send it.
Running on the PC will be a Java program feeding info to the USB serial com port.   The Java program will pull the CPU load and System temperatures and format it for display.   I could do color changes, whatever to make it fancy.

The first task is to access the desired information from the Java program running on the Windows PC.

Some useful posts


Some info can be pulled from within Java, but it is fairly limited.  I tried this code pasted into a little netbeans program

OperatingSystemMXBean OS = ManagementFactory.getOperatingSystemMXBean();    
        jLabelCPUs.setText("Number of CPUs: "+ Integer.toString(OS.getAvailableProcessors())+ " " +OS.getArch());;
        jLabelLoad.setText("CPU load: "+ Double.toString(OS.getSystemLoadAverage()));
The OS.getSystemLoadAverage() always Returns -1.   A little more reading reveals that this doesn't work on Windows.  Not definitive but discouraging.   The CPU architecture commands do work though.

Instead I thought I'd run a windows command line program from within Java and pull the output.  This approach worked much better.

There are two basic approaches.  wmic and typeperf.  Entered these commands into a cmd window and got back a string of the CPU load.   Both have some pretty obscure command arguments.
        wmic cpu get loadpercentage
        typeperf -qx "\Process" > config.txt typeperf -cf config.txt -o perf.csv -f CSV -y -si 10 -sc 60

Now to embed this in Java and get the output back to the Java program.
This Java code runs the cmd line wmic.

try {
            Process p = Runtime.getRuntime().exec("wmic cpu get loadpercentage");
} catch (IOException ex) {
            System.out.println("cmd line failed");
            Logger.getLogger(BlingGui.class.getName()).log(Level.SEVERE, null, ex);

Looking for how to get the output back, I see I just gotta use buffered stream to read it:

Back to polishing up the windows cmd line commands to get the data I want.

Wmic is awesome!   I can get more than cpu load.  All sorts of process and machine info.
wmic cpu get currentClockSpeed
wmic cpu get loadpercentage

Typeperf is also very useful.

This command gives processor and memory usage in running 1 second intervals.  Awesome.
typeperf "\Memory\Available bytes" "\processor(_total)\% processor time"

This makes just one sample, also seems a little slow to respond
typeperf -sc 1 "\processor(_total)\% processor time"
typeperf command format is a mess to escape in Java, but got it to work like this:
Process p = Runtime.getRuntime().exec("typeperf \"\\processor(_total)\\% processor time\" ");
This makes just one sample at a time.
typeperf -sc 1 "\processor(_total)\% processor time"

Here is the output of typeperf writing to a hack GUI in Java. The yellow is the CPU%.  Obviously haven't done the formatting yet.   Making progress!

I wasted some more time looking for how to get the temperature command line, and it is not easy. I'm avoiding using somebody else's freeware program like CPUID or speedfan. Both of which work but won't get me to my goal. I'm going to stick with CPU load, memory and frequency data for the prototype.

A little more work and I have a Java performance meter program. Grabbed the cmd line output, did some string manipulation and formatting.  I haven't added the serial client stuff yet to talk to the Arduino yet.  This demos all three methods.  Java commands, wmic and tyeperf commands.  The typeperf is the one running the progress bar.

The executable is here:
This is the gist of the code, minus the GUI stuff.

 public BlingGui() {
        OperatingSystemMXBean OS = ManagementFactory.getOperatingSystemMXBean();
        jLabelCPUs.setText("Number of CPUs: "+ Integer.toString(OS.getAvailableProcessors())+ " " +OS.getArch());;

        Timer t_update = new Timer(10000, new ActionListener(){
            private String t1;
            public void actionPerformed(ActionEvent e){
                try {
                    Process w1 = Runtime.getRuntime().exec("wmic cpu get CurrentClockSpeed");
                    BufferedReader stdInput1 = new BufferedReader(new InputStreamReader(w1.getInputStream()));
                    BufferedReader stdError1 = new BufferedReader(new InputStreamReader(w1.getErrorStream()));
                    while ((t1 = stdInput1.readLine()) != null) {
                            if( Character.isDigit( t1.charAt( 0 ) )) jLabelFreq.setText("Clock: "+ t1.trim() +" MHz");

                } catch (IOException ex) {
                    System.out.println("cmd line failed");
                    Logger.getLogger(BlingGui.class.getName()).log(Level.SEVERE, null, ex);
        try {
            // execute windows command line:  typeperf -sc 1 "\processor(_total)\% processor time"     
            Process p = Runtime.getRuntime().exec("typeperf \"\\processor(_total)\\% processor time\" ");
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            String s;
            while ((s = stdInput.readLine()) != null) {
              s = s.replace("\"", "");
              String[] sArray = s.split(",");
              s = sArray[sArray.length-1].trim();
              jLabelLoad.setText("CPU Load: " + s + "%");
             try {
                jProgressBarCPU.setValue( Integer.valueOf(s.indexOf('.') ));
                double dd = Double.valueOf(s);
                int gg = (int) dd;
                jProgressBarCPU.setValue( gg);
                if (gg<33) jProgressBarCPU.setForeground(;
                else if (gg<66) jProgressBarCPU.setForeground(;
                else jProgressBarCPU.setForeground(;
             } catch (Exception e) {
                //Sometimes this is not a number
            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
        } catch (IOException ex) {
            System.out.println("cmd line failed");
            Logger.getLogger(BlingGui.class.getName()).log(Level.SEVERE, null, ex);

Next step is to combine this with my Arduino serial communication program to send the CPU load data to an external Arduino with LCD shown in this post:
This is just for prototyping, I'll build a custom LCD unit that fits in a drive bay once I get the s/w working.

For proof of concept I did a Frankenstein and simply pasted the CPU monitor GUI class right into my serial comm tool.   That way I use that tool to find the Arduino comm port and connect, and then the CPU monitor   kicks in and sends the CPU load string information as if I were pushing the send button on the GUI.

The Arduino is just running the Examples->Liquid Crystal->SerialDisplay sketch right out of the box.
The hardware is an Ardino with a large 4x20 LCD connected just like the Arduino example says.   I won't repeat all that here.  I reused this from   Everybody should have an Arduino with an LCD on it lying around for emergencies.

W00t!  It works!.   Here is the PC running the serial comm tool, with the new CPU GUI below it.  On top of the screen is the Arduino/LCD displaying the CPU load every second.

Now to clean up the code and make the LCD display a bar graph instead of just number.

The Arduino code need some upgrades.
  • Sleep whenever it hasn't gotten data in a while.  Turn off the LCD backlight and blank the display.
  • Initially send a character like "AAAAA" until it's connected so the Java GUI can find it and initialize communication
  • Eventual features using the Analog pins to control some LEDs brightness or color
Here is the Arduino sketch.  I'd like to keep it simple like this.  It can also display messages as I choose if I leave the brains in the Java host side:

  LiquidCrystal Library - Serial Input

This sketch displays text sent over the serial port
 (e.g. from the Serial Monitor) on an attached LCD.

 The circuit:
 * LCD RS pin to digital pin 7
 * LCD Enable pin to digital pin 6
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

// include the library code:
#include <LiquidCrystal.h>
const int backlight = 8;   // the LED backlight is wired to pin 8

long previousMillis = 0;            //stores the last time the display got data

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup(){
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // set up the LCD backlight
  pinMode(backlight, OUTPUT);
  digitalWrite(backlight, LOW);
  // initialize the serial communications:
  establishContact();  // send a byte to establish contact until receiver responds

void loop()
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    // clear the screen
    // wake up and turn on the backlight
    digitalWrite(backlight, HIGH);
    previousMillis = millis();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
  } else if (millis() - previousMillis > 10000) {
     //turn off the backlight and clear the display
     digitalWrite(backlight, LOW);


void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("Z");   // send an initial string


Now the display wakes up when it gets a message, and goes blank 10 seconds after the last update.  Perfect.

None of the LCD's in my drawer will fit behind a 5.25" drive bay panel.   Actually some of the tiny ones will do but I want something snazzy.
The panel on the PC is about 1 5/8 * 5 3/4, (42mm x146mm)  and the LCD will need to be smaller.   I probably will need to use a 2x20 to get the right shape, the 4x20s are too tall.
It is also clear the Arduino itself is too tall and I will have to use a right angle header to connect to the LCD.

It occurred to me that I would also like to have some connector/header on the front I can use to access a few of the pins of the Arduino so I can use it for development and whatever GPIO project I dream up.

This vacuum fluorescent one looks cool, a bit pricey at $29
20x2 Character VFD (Vacuum Fluorescent Display) - SPI interface
It also has a serial interface.  That will save pins but require the code to be changed.
Outer dimensions are 37mm x  116mm.  It will just barely fit.
This one is 36x80mm and has a three color backlight.  $14. Only 2x16 chars
RGB backlight negative LCD 16x2 + extras

Looking at digikey...
MODULE VF CHAR 2X20 5.34MM | M0220SD-202SDAR1-1G | M0220SD-202SDAR1-1G-ND | Digi-Key Corp.
$31, 116mm x 37mm, 20x2, parallel interface like I'm used to.  However price is a bit steep, not sure if VF is worth it.

Searched a while, decided the VF displays are too expensive and I like the multicolor backlight price and convenience of LCD.

Digikey has this one, looks similar to the one on Adafruit.

I decided to get the Adafruit display, they say they have resistors onboard for the backlight, and it comes with a trim pot.

The display came.  I'm going to mount it in an old 5.25 bay blank plastic cover from an old PC.  Here is the display sitting on top of the plastic blank bay cover.

My plan is to wire it up just like the Adafruit tutorial
And to add a small header connector to some unused pins to come out the front of the PC for development projects.

I cut a hole in the plastic panel to fit the LCD with a Dremel. It didn't come out as well as I'd hoped.  I'm going to have to fill and paint or try again with another blank.   Forging forward to work out the rest of the details before wasting time remaking something that may have to change anyway.

I'm using the header pin strips that came with the LCD to connect some scrounged IDE ribbon cables to the display.  I had thought I'd connect that directly to the Arduino with another header, but the power, ground and potentiometer connections will be awkward and messy.   Plus I wanted to put the analog pins on another header that sticks out the panel so that I can use them for future prototyping projects.    So I decided to bite the bullet and use an $6 adafruit prototyping shield to serve as a patch board and the $2 headers.  These I had in my parts bin already from past projects.
Adafruit Proto Shield for Arduino PCBShield stacking headers for Arduino (R3 Compatible)

The IDE ribbon cables are 17 pins wide, and I need 18, so I'll take advantage of the fact that the R/W pin is grounded and just short them together on the LCD.

Here is the protoboard soldered up with the headers and pot, connecting the cable header in the middle to the Arduino headers.   Careful consideration of the direction and the connections were all pretty easy to make.

Plugged in the IDE ribbon cable to the headers.   I used an ohm meter to double check the wiring.  I found no mistakes.  First time that ever happened.

Attached the Arduino to the protoboard, plugged in the USB.  I copied the code directly from the Adafruit page since the hardware was exactly the same as they show.  Plugged in the USB, uploaded the sketch, twisted the pot until the contrast showed the words.  Win on the first try!

Rather than remake the front panel, I put some electrical tape on the edges of the display to stop the light leakage, and used foam tape to mount the display in the panel blank.   I decided to forge ahead and throw it together to see if there were any issues and polish up the software.  It does look a tad ratty.  LCD is crooked and the cutout is ragged.  The picture even makes it look worse than it does in real life.

Hopefully I'll go back and remake the front panel and mount the LCD better later.

Next step is to update the previous serial display sketch to handle the multicolor backlight.  Then I need to polish up the PC Java program to send it the CPU load.

I combined the multicolor LCD Arduino code with the earlier serial LCD program and it's working great.  I write to the display, it pops up blue, then goes to sleep after 10 seconds.   I won't post the code yet because there are two issues.  
1) I need someway to control the multiple colors
2) The serial program only writes to the first line of the display.  It will wrap to the second line in a buggy way and skip many characters

I need to add some control characters to set the colors and which line I want the text to show up on.

This code did the trick!  Intercept the characters and test them.  Now I can change the color and move the cursor with the special characters.  I want to use some more obscure characters but this was a first pass.  

    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      char temp;
      temp =;
      if (temp=='^') setBacklight(0, 255 , 0);
      else if (temp=='<') setBacklight(255 , 0, 0);
      else if (temp=='>') setBacklight(0, 0 , 255);
      else if (temp=='@') lcd.setCursor(0,0);
      else if (temp=='$') lcd.setCursor(0,1);
      else lcd.write(temp);


This chart shoes how to combine the RGB to get different colors

I also found that many of the combinations, at reduced brightness have a bad flicker.  That is probably due to the PWM on the arduino being too low.   Since I don't want to mess up the delay timers, I'm just going to pop the brightness on those colors.   

Now my if statement is getting hairy, but I have a good collection of color:

      char temp =;
      if (temp=='^') setBacklight(0, 255 , 0);        //green
      else if (temp=='<') setBacklight(255 , 0, 0);   //red
      else if (temp=='>') setBacklight(0, 0 , 255);   //blue
      else if (temp=='{') setBacklight(0, 255 , 128); //cyan
      else if (temp=='}') {
        brightness = 255;  //must max out to prevent flicker
        setBacklight(255, 100 , 0);                    //orange 
      else if (temp=='|') {
        brightness = 255;  //must max out to prevent flicker
        setBacklight(128, 0 , 255);                    //purple  
      else if (temp=='~') {
        brightness = 255;  //must max out to prevent flicker
        setBacklight(255, 0 , 255);                    //magenta 
      else if (temp=='&') {
        brightness = 255;  //must max out to prevent flicker
        setBacklight(255, 255 , 255);                   //grey
      else if (temp=='_') {
        brightness = 255;  //must max out to prevent flicker
        setBacklight(255, 255 , 0);                   //yellow
      else if (temp=='@') lcd.setCursor(0,0);
      else if (temp=='$') lcd.setCursor(0,1);
      else lcd.write(temp);
I think the Arduino code is done, I posted the whole thing here
It is basically the Adafruit example plus the serialdisplay example and the color selection code.

Now to switch back to the Java PC side to clean up the code sending information.
I need to format the information, include the control characters.  Also I've noticed I need to kill the typeperf process when I exit, I've been getting zombies.

Fixed the zombies by putting a custom window listener on the window exit command, that does a p.stop() on the typeperf runtime.   

Updated my serial arduino tool to automatically touch each port and read from it.   Once the desired character stream, in this case QQQQQ..... appears the program grabs that comm port and begins sending the CPU information.

Spending a little more time on typeperf to try to get it to give me the processor load, processor clock speed and memory all in one command.  wmic is good at processor clock, but I have to spawn multiple processes to get both typeperf and wmic running at once.  Typeperf does the 1 second sampels all by itself and sends the data back on one line.

typeperf -qx dumps all the possible information.
some juicy lines....I marked the ones that I liked in red.  Some didn't seem to work and gave back 0.

\Processor Information(0,_Total)\% of Maximum Frequency
\Processor Information(0,1)\% of Maximum Frequency
\Processor Information(0,0)\% of Maximum Frequency
\Processor Information(_Total)\Processor Frequency
\Processor Information(0,_Total)\Processor Frequency
\Processor Information(0,1)\Processor Frequency
\Processor Information(0,0)\Processor Frequency
\Processor(0)\% Processor Time
\Processor(1)\% Processor Time
\Processor(_Total)\% Processor Time
\Memory\Page Faults/sec
\Memory\% Committed Bytes In Use
\Memory\Available KBytes
\Memory\Available MBytes
\PhysicalDisk(0 C: D:)\Disk Transfers/sec
\PhysicalDisk(_Total)\Disk Transfers/sec
\PhysicalDisk(0 C: D:)\Disk Reads/sec
\PhysicalDisk(_Total)\Disk Reads/sec
\PhysicalDisk(0 C: D:)\Disk Writes/sec
\PhysicalDisk(_Total)\Disk Writes/sec
\PhysicalDisk(0 C: D:)\Disk Bytes/sec
\PhysicalDisk(_Total)\Disk Bytes/sec

to use any of these just put them in quotes after typeperftypeperf "\PhysicalDisk(_Total)\Disk Bytes/sec"
To dump all the ones I want, I used this command:

typeperf "\Processor Information(0,0)\Processor Frequency" "\Processor(_Total)\% Processor Time" "\Memory\% Committed Bytes In Use" "\PhysicalDisk(_Total)\Disk Bytes/sec"

Funny, using this command I discovered on my work laptop that the power management settings were running the CPU capped at 800MHz.  Turning up the slider to "performance" got the CPU speed up to 2534.  It is much peppier now.  A nice Easter egg.

Here is the typeperf command in Java with all the escapes
final Process p = Runtime.getRuntime().exec("typeperf "
                    + "\"\\Processor Information(0,0)\\Processor Frequency\" "
                    + "\"\\processor(_total)\\% processor time\" "
                    + "\"\\Memory\\% Committed Bytes In Use\" "
                    + "\"\\PhysicalDisk(_Total)\\Disk Bytes/sec\" "
                    + "");

I wrote some code to slice up the response and pull out the numbers, and change color depending on load.   Ha ha funny part is the new machine is so bloody fast it is almost impossible to max out the CPU.  I finally had to download Sisoftware sandra benchmark tool.  No amount of running youtube videos even got me to 10% CPU load.

Here is a snippet of the Java that decodes the typeperf response, makes a bar graph and changes the color of the display using the control characters.

            String sp;
            while ((sp = stdInputp.readLine()) != null) {
                //Parse and clean up the typeperf response
                sp = sp.replace("\"", "");                
                String[] spArray = sp.split("\\s*,\\s*");
                try {
                    //System.out.println(spArray[1]+" "+ spArray[2]+" "+ spArray[3]+" "+ spArray[4]);                                     
                    float CPUfreq =  Float.valueOf(spArray[1]);
                    float CPUload = Float.valueOf(spArray[2]);
                    float MemUsed = Float.valueOf(spArray[3]);
                    float diskBytes = Float.valueOf(spArray[4]);
                    int CPUf = (int) CPUfreq;
                    int CPUl = (int) CPUload;
                    int memu = (int) MemUsed;
                    int bargraph = (int) (CPUload*16/100 + 0.5);
                    String bars = "";
                    for (int jj=0; jj<bargraph; jj++) {
                    String displayString = bars + "$" + Integer.toString(CPUf) + "MHz  Mem" + Integer.toString(memu)+"%";

                    // add color control characters based on load
                    if (CPUload>79) displayString = "<" + displayString;       //red  
                    else if (CPUload>49) displayString = "}" + displayString;  //orange
                    else if (CPUload>24) displayString = "_" + displayString;  //yellow
                    else if (CPUload>14) displayString = "^" + displayString; //green
                    else displayString = ">" + displayString;  //blue
                catch (Exception e) {  
                    System.out.println("Exception "+ e.toString());

I left the Java GUI fairly rough, since it will run in hidden mode most of the time.  The Hide button sets the form to run invisibly.   This is detecting several Arduinos on the system at once, and automatically picking the one that is sending the QQQQQ stream identifying itself.

I need to put the .jar file somewhere it will start when the computer starts, so I put the .jar executable into the startup folder.

Here are a couple shots and a movie of the display responding and changing color.   Note the processor clock jumps up and the bar graph across the top moves to indicate greater load.  You can see in detail the crap job I did cutting the hole in the panel.

The video is a bit like a minute of watching paint dry, but you can see the processor benchmark test finish and ramp down the processor load.  In these videos I wasn't showing the Memory load yet.


The java executable is here to download:
If you try to run this, the usual caution that you need Java JRE installed on your machine, plus you have to copy the rxtx jar and dll to the proper places from the Arduino directory.  The java program will prompt you to do it if you don't do it right.  It's covered in my other posts.

The source and Arduino code is also on my download page

Win!  A quick project and my hot new PC has something nobody else's does.  A performance monitor on the front panel, and an Arduino development board built in for other playing around.  When I get bored I'm going to remake the front panel and cut the holes for the extra Arduino pins.

1 comment:

  1. This comment has been removed by a blog administrator.