FINDING THE RESISTOR VALUE
Solder a 10K resistor to the Zero's header connector hole at pin 12, and connect the other end to the meter plus terminal. The meter minus terminal is connected to pin 14. To test it type the following:
sudo gpio -g mode 18 pwm
sudo gpio -g pwm 18 100
The meter needle should move. Then re-type the second line, using different numbers in place of the 100, until the meter is at full scale. Don't go over 1023. If the meter is at full scale at less than 500, replace the resistor with one that's approximately double the value and test again. If you can't get it to go full scale even at 1023, use a lower value resistor and test again. Don't go much below 330 ohms. Your goal is full scale with pwm value somewhere between 500 and 1000. Since it may take a couple of tries to get it right, you might not want to solder the resistor in: you can just hold it in place with your fingers until you determine the correct value. When you have the right value solder it in.
The meter is (probably) slightly non-linear so we'll generate an equation to linearize it. This is most likely overkill in this instance, but it's a technique that's very useful, so we'll go ahead and cover it. First we'll need to determine some calibration values. The process is simple if tedious: find pwm values for several meter needle settings. Try to get around ten equally spaced ones. For instance, my meter goes from 0 to 100, marked at 12.5 intervals. Here are my values, organized in x,y pairs with x being the meter value, and y the pwm setting:
0,0 12.5,75 25,150 37.5,237 50,316 62.5,395 75,470 87.5,545 100,622
Next we'll use Mathematica and these values to generate coefficients for a fourth order polynomial (Quartic) linearizing equation. Enter your calibration values into a text file named meter.wm, formatted like the meter.wm file in this project's files section. I would type it out here but the formatting gets too jumbled up.
Then run the following command:
wolfram < meter.wm > meter.bc
After a few seconds the
meter.bc file will be written. Mine is in this project's files section and is called wolfram-output.txt.
The important part in it is the
Out line. Note that the exponents are actually on the line above, and some coefficient may be in scientific notation. Edit the file to fix these and remove the unneeded stuff. The file should eventually be a single line. Mine looked like this (ignore the line break):
y = 0.47397 + 5.33866 * x + 0.0449517 * x^2 - 0.000653095 * x^3 + .00000291207 * x^4
Now we'll finish up by adding some text to make it into a proper bc program. Refer to the meter.bc file in this project's files section for an example of what needs to be done. Basically you need to just substitute your equation for mine.
Save the file as meter.bc and make it executable by typing
sudo chmod 755 meter.bc
What's bc you ask? It's a command line arbitrary precision calculator program. We're using it because the meter control program will be written in shell script, which doesn't have floating point math capability. (We could use Wolfram instead of bc, but I want to eventually use this on a Zero with the Lite image, which doesn't have Wolfram. Gotta use up those 2G micro SD cards!) Since bc doesn't come with Raspbian we'll need to install it by doing the following:
sudo apt-get install bc
All that's left is to whip up a shell script to grab a single value from my Sparkfun data stream and display it on the meter. My file that does this is in this project's files section and is called meter.sh.
You can modify it to get data from your own Sparkfun data page. (They email you the access key you'll need when you sign up.) As written, it gets the first field of the most recent value.
You can use the meter for other purposes too. The general idea is to get an input value from somewhere and pipe it into the meter.bc linearizing program, then take it's output value and use it to set the PWM value. Reverse single quotes are your friend.
You could even use it to display how many Pi Zeros are in stock at Adafruit!