Simple Serial A/D Interface Project

Introduction

This project describes how to build a simple serial A/D interface (SI430) from an Atmel ATMega32L and a few commonly available parts. The interface can be used with any serial port on Windows or Linux PCs.

The Atmel ATMega32L

If you want to interface to the real world, a good place to start is with a microcontroller. Microcontrollers typically contain a number of useful functions all on a single IC. There are literally thousands of different microcontrollers, but the Atmel ATMega32L has a number of features that make it ideal as a simple PC interface.

The ATMega32L has 8 channels of 10-bit analog input so you can measure DC voltage inputs from 0 to +5 V with a resolution of about 5 mV. This means you can read any type of sensor with a high level DC output signal. There are hundreds of possibilities: pressure sensors, temperature sensors, light level sensing and many others. In addition, the ATMega32L can be configured to accept four digital inputs and switch four digital outputs. This allows you to control relays and read the state of input switches. The ATMega32L also has a built-in USART that provides serial communication, and does not require a clock oscillator or even a crystal.

Using the ATMega32L and just a few external parts you can quickly build up an interface to the real world that can communicate via serial port to Channel 8.

Construction of the Serial A/D Interface Controller

The design of the SI430 has been reduced to the absolute minimum, as can be seen from the schematic, shown here. A complete list of parts is given here. You can build the SI430 in an afternoon with average soldering and assembly skills. The best way to proceed is to build up the project in sections, starting with the power supply. I built the SI430 on a 6" prototyping breadboard (Radio Shack 276-174) first to verify operation. When that worked I hardwired everything to a Radio Shack prototyping board (p/n 276-170). This was then mounted to a wooden base along with the breadboard to facilitate experimentation with input circuits to the SI430.

Power Supply Section

The components in the SI430 need to operate on a regulated +5 Vdc. The power supply section is a classic, using the venerable LM7805 three-terminal regulator. Two filtering capacitors are used to minimize voltage spikes, and an LED provides a "power on" indication. The SI430 is powered from any common AC-to-DC power adapter (Radio Shack 273-1773, for example) that can supply at least 7 Vdc at 300 mA. You probably have several around the house; simply look at the name-plate to see the output voltage and current capacity. You can simply cut off the connector on the DC wires and connect directly to the input of the SI430 power supply section as shown on the schematic.

Note that the capacitors are tantalum, electrolytic types. These are polarized and will have the + lead marked in some way. The acutal uF values are not critical but be sure the input filter capacitor (0.33 uF) is rated to 35 V. Double-check the regulator terminal pin designations from a catalog sheet; it is very easy to get the LM7805 turned around and wired wrong.

Test the power supply section with a good voltmeter before you try to power any other part of the SI430. The LED should light up, but check to see that you are getting +5.00, +/-.010 Vdc, between Gnd and terminal 3 of the regulator. A further precaution is to measure the current draw of the circuit. It should draw less than 40 mA under normal conditions, with just the power-on LED connected. Carefully touch the regulator mounting tab to see if it is overheating; it should be only moderately warm. If the current is too high, check for shorts before you burn up the regulator or the LED!

Level Shifter Section

The ATMega32L has a serial USART, but it operates on different voltage levels (+5V) than does the PC serial port (+12 V). The Maxim MAX232 transciever chip solves this problem and is an important part of the SI430 design.

The MAX232 uses four 10 uF capacitors as part of its level-shifting and these must be carefully connected. They are all tantalum electrolytic types and the polarity is very important. Note that the capacitor polarity between pins 6 and 15, and between pins 2 aqnd 16 of the MAX232 appear to be reversed. This is not a mistake! It is part of the MAX232 scheme for generating a +12 V signal from the SI430 +5 Vdc supply.

Communication to the PC is by a serial cable. A 9-pin cable is most convenient. Select a 9-pin connector that mates with your cable, and wire the pins as shown on the schematic.

ATMega32L Microcontroller

The ATMega32L is easy to wire. All possible shortcuts have been taken in the name of simplicity. The analog reference (pin 32), for example, has been connected to the +5 Vdc supply. This means all the readings from the analog inputs will be a 10-bit fraction of the +5 V supply. There are 1024 counts in a 10-bit reading, so the analog readings will be 0 counts at 0 Vdc input and 1023 counts when the input is the same as the + supply. This is normally +5.00 Vdc, but the LM7805 can vary somewhat and it is not considered good practice to reference the A/D converter to a power supply. But it is close enough for experimental purposes and saves complexity and extra parts.

The reset pin of the ATMega32L has been pulled high (pin 9) with a 470K resistor. So you have to power the SI430 down to reset it. Another short-cut, but it saves a few more parts. The ATMega32L has an internal 1 MHz oscillator so you don't have to provide an external clock signal or even a crystal. This limits serial port speeds to 2400 baud, but this is a small price to pay for the inherent simplicity.

ATMega32L Programming

The ATMega32L microcontroller is a programmable device. It needs its own set of instructions to perform the interfacing tasks and respond to PC serial port commands. Atmel, and others, offer development kits that provide cables and a command compiler so that you can write and debug code on your PC, then upload it to the ATMega32L non-volatile memory. Thereafter, every time the ATMega32L is powered up, it starts executing the embedded program.

In order to make a real-world interface that responds to a set of Linux serial port commands, the program inside the ATMega32L has to be listening for incoming requests for data. So a command string structure has to be developed, and then coded into the ATMega32L. For this project, a command set was defined as shown here.

If you want to develop your own command structure and program the ATMega32L to respond, there are a number of tools and development kits available. The Bascom compiler is a good choice because it allows you to write code in BASIC, then compile and upload it to the ATMega32L. And Bascom offers a free, downloadable version of the compiler.

Miscellaneous

Once the SI430 is assembled, you will want to mount it so you can connect the I/O wires to whatever inputs and outputs you are working with. Some stand-offs, some barrier strips will help here. Be sure to bring out the +5 V and Gnd so you have access to power for your experiments. Alternately, you can hardwire just the I/O points you need and mount the entire thing in a box. To experiment with various input signals and to control various outputs, I choose to keep everything on an open wooden base with the prototyping breadboard. Now I can devise various kinds of input circuits and try them out with the SI430.

Testing the SI430

Once everything is wired up, its time to test the SI430 through the serial port. The best way to do this is to use the HyperTerminal utility in Windows. Set the serial parameters as follows:

To start, type in the Ping command P; you should receive SI430 65535 in reply. Try sending 0 to get the readings on analog input Ch 0. You can wire up a signal to pin 40 of the ATMega32L and check it with a voltmeter. The returned value in counts should reflect the actual applied voltage, within a few millivolts.

Digital output ports can be checked with LEDs. Digital inputs with switches wired with pull-up resistors to +5 V.

Channel 8 Driver Code

Now that you have a working interface to the real world, it is time to write software to allow Linux to communicate commands over the serial port.

Here is a listing of a C program that communicates with the SI430 and formats the analog readings for Channel 8:

/* Serial Interface Program */
#include stdlib.h
#include stdio.h
#include time.h
#include string.h

main()
{

FILE *fptr;
char IP[5];
char *Vlu;
char Val[50];
double v[7];
double volts;
int ctr = 0;

while (ctr <8)
{
fptr = fopen ("/dev/ttyS0", "r+");
fprintf(fptr, "%d", ctr);
usleep(150000);
fgets(IP, 5, fptr);
volts = atof(IP);
v[ctr] = 5*volts/1024;
fclose(fptr);
usleep(150000);
ctr++;
}

ctr = 0;
while (ctr < 8)
{
asprintf(&Vlu, "%5.3f ", v[ctr]);
strcat(Val, Vlu);
ctr++;
}

printf("%s ", Val);

}

Now that the A/D interface code has been verified, it is simply a matter of inserting it into the Ch8RndServer.c file, compiling and testing it with the rest of the Channel 8 system. The section of Ch8RndServer.c that is appropriate is identified by the comments:

"// ------ Put A/D Code Here ---------"

and

" // ------------ End A/D Code ----------------".

Be sure to remove the code for generating random numbers.

Note that if the floating-point A/D values in volts are kept in an array named v[], the correct string will be automatically generated by the existing code for random values. This code is duplicated in the above example.

Before you run the program, you must set the serial port to 2400 baud. Do this using the following command: stty /dev/ttyS0 2400 You can verify as follows: stty /dev/ttyS0 . Run the example program

Source Code Available

The source code for the complete Channel 8 SI430 server is available. Also available is a pre-programmed Atmel ATMega32L chip that responds per the protocol described in this project.

For further info e-mail: documatrix@linuxmail.org