Acquiring data from the physical world

25

Author: Rob Reilly

If you want to track something physical, such as counting people passing through a gate, or recording how the temperature changes over time in a factory process, you can use a microcontroller to do the measurement. And if you move the data onto a Linux server, you can store it in a database, perform calculations, or send it out on a Web page. The cost is reasonable and the process is simple.

You can buy a Netmedia BX-24 microcontroller for around $50. It’s programmed via a serial line and stores its compiled program in an EEPROM (electrically erasable programmable read-only memory) — no need for a clunky old PROM burner. The chip’s 16 digital I/O pins can respond to inputs at several thousand cycles per second. It also has an 8-channel 10-bit ADC (analog to digital converter), to directly read pressure, light, and various other analog inputs, turning them into numbers from 0 to 1,023.

The BX-24 compiler runs under Windows 98. Sadly, I was unsuccessful at getting it working with Wine, so I raided my garage for an old Pentium machine with which to program the microcontroller chip. If you want, once you cut your teeth on this inexpensive chip, you can explore programming other chips with 100% Linux or open source solutions. Chris Harriman, application engineer from Netmedia, said that a new version of BasicX software will be cross-platform, including Linux, but he didn’t have an exact date or form for the release of the Linux version.

To program the BX-24, you write a program, compile it (using Netmedia’s BasicX software), then download the machine code to the chip over the programming serial cable. Another serial cable (attached to an I/O pin and ground) will then output data to a second computer (preferably Linux) running database software or a Web server. Once everything is working right, you can eliminate the programming serial cable.

Microcontrollers lead a pretty mundane life, and their programs follow suit. The program I wrote to grab position values from a 100K Ohm potentiometer and light levels from a photocell amounted to reading an I/O pin, outputting the number to the serial port, then looping back to the beginning. It took about a dozen lines of code. If you need to get a lot more done, the BX-24 can run programs of up to around 8,000 lines.

I used a 2×6-inch white plastic prototype breadboard to secure the BX-24, the other support parts (like the voltage regulator, resistor, and a switch), and connection points for the external wires of the serial cables. The voltage regulator, 1K Ohm resistor, photocell, and potentiometer I used are all available from Radio Shack or your local electronics store. You’ll also need a pair of suitable serial cables and a six-volt battery pack. I used a 5.5-volt, 950 ma. power supply from an old cell phone to power my project.

This figure shows a diagram of the BX-24, its parts, and its connections.

Use the Netmedia-supplied editor to create the program to read data from the microprocessor. The BasicX code to read a potentiometer and a photocell is straightforward enough. This sample program reads two of the pins as analog inputs, then spits out a time reference along with potentiometer reading and light level. The values are separated by a comma and terminated with a newline character. As soon as the microcontroller receives power, it will start sending data.

On the upsteam end, you can fashion all kinds of complicated programs to read the data coming into your Linux box from the microcontroller. I simply grabbed what came in on the serial port and dumped it into a text file on my Linux/Apache/PHP server with a single line of shell code:


rreilly> while true; do read LINE /home/rreilly/roblog2.txt; done

Here’s an example of the text file generated at the Linux end of the serial cable.

Start Log
0,672,203
1,703,204
2,724,203
.
.
.
17,686,204
18,685,203
19,678,202

For demonstration purposes, I used the double > to append data to the file. In reality, one would just use a single >. The three-value result line is written to the text file as long as the serial port is receiving data.

The PHP code for displaying the data on a Web page is equally simple. I embedded it in a larger HTML code segment written for my personal home page:


$FileName = "/home/rreilly/roblog2.txt";
$FilePointer = fopen($FileName, "r");
$Array = file($FileName);

$today = date("m/d/y g:i:s a");       // example: March 10, 2001, 5:16 pm
echo "&nbsp&nbsp&nbsp&nbsp $today &nbsp&nbsp&nbsp&nbsp Microcontroller Data = $Array[0]";

fclose($FilePointer);
?>

Go forth and acquire

Once you understand the basic operations, you can use your microcontroller for all kinds of things. Hook a pendulum to the potentiometer’s shaft, swing it horizontally, and measure acceleration. Hook up a pressure sensor and weigh things. Connect a dozen reed switches and keep track of doors opening and closing. Put the Linux/Apache/PHP server on a laptop, hook it to a cheapo wireless router, plug the microcontroller into the serial port, and you have a portable data acquisition system. You can connect the ADC pins to various 0-5 volt signals in your circuits and have the makings of a test set.

If you’d like more information, one of the most comprehensive books about using microcontrollers I’ve found is Physical Computing: Sensing and Controlling The Physical World with Computers, by Dan O’Sullivan and Tom Igoe. The authors, who teach at New York University’s Tisch School of the Arts, cover parts, various microcontrollers (including the BX-24), circuits, code, and applications. Their focus is on the practical use of microcontrollers to solve problems in the real world. At 450 pages the text is thorough, without a lot of extraneous information.

Rob Reilly is an independent consultant, commentator, and writer. His company advises clients on technology integration and new trends.