Thursday, December 11, 2014

Raspberry PI CPU Usage Tachometer


We had an old car tachometer laying around the labs (beats me why...) that seemed perfect for some type of project.  But what?   Since it is pretty common to see a car looking tachometer as a CPU usage meter on the desktop of a PC we decided to create a hardware version of this for the Raspberry PI.

Basically what we are going to do is read the CPU usage on the Raspberry PI.  Then convert that CPU usage value into a corresponding frequency.  That frequency will be output via GPIO on the Raspberry PI pin 11 to drive the tachometer.
----
First step is to characterize the tachometer and find out what type of frequencies made the needle move.  The Tekronix 3252C made easy work of that.

----
The spreadsheet below shows what frequencies move the tachometer needle to what position.  
We also used this spreadsheet to calculate a multiplier to adjust the CPU usage reading on the RaspPI to a 0 RMP to 8000 RPM reading on the car tachometer.  It's a pretty simple calculation and you can see how it is used in the Python source code below.
-----
The tachometer needs 12VDC to power it, but the input signal to move the tachometer needle needs to be 5VDC.  A 7805 voltage regulator solves that problem.  Also, to buffer the RasPI GPIO a 7404 was used to drive the RasPI signal into the tach.  The connection looks like this:
----
Using some Python code the end result is this:

In the video you can see how moving the mouse around on the Raspberry PI increases the CPU usage making the tachometer reading increase.  When the Chromium web browser is launched the CPU usage goes to 100% and the tachometer needle 'redlines'.
-----
The Python code is straightforward.  Drop us a line if you decide to duplicate the build!


#
# Program makes a simple car tachometer read CPU usage percentage.
#
# WhiskeyTangoHotel.Com - December 2014
#
# To run this program you must first do a one time install
# of the following from the terminal commans line.
#
# sudo apt-get install python-pip python-dev
# sudo pip install psutil
# sudo apt-get install python-psutil
# see: http://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/ for PWM info

import time
import psutil
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT) # Pin 11 drives the tachometer

#p = GPIO.PWM(channel, frequencyHz)
p = GPIO.PWM(11, 200) # move the tach needle to about 1/2 scale
#p.start(dc)   # where dc is the duty cycle (0.0 <= dc <= 100.0)
p.start(50)
print ' '
print 'SELF TEST: Tach to about 1/2 scale for 5 seconds...'
time.sleep(5)   
print ' ' 

i = 0 # just a counter
adjust_cpu_to_hz = 3.8   # adjust_cpu_to_hz is calculated on the tach characterization spreadsheet.
# Tektronix 3252C was used to feed input into the tach to create the
# characterization spreadsheet.  Discover how many Hz needed to move needle.

while(True): # loop forever  
i = i + 1
# read the RasPI CPU Usage. Store the value in var cpu_usage.
cpu_usage = psutil.cpu_percent()
# use adjust_cpu_to_hz to scale the cpu_usage result to Hz within the tach's range
p = GPIO.PWM(11, cpu_usage * adjust_cpu_to_hz)  
p.start(50)
print 'Run #:', i, '  ', cpu_usage,'%', '    ', cpu_usage * adjust_cpu_to_hz,'Hz out to tach'
time.sleep(3)   # allow x secs to give the CPU a breather
p.stop()     # stop PWM (also end of while loop)

input('Press return to stop:')  
p.stop()     # stop PWM
GPIO.cleanup() # Take out the trash
----