12-bit analogue-to-digital converter

This is a 12 bit A/D converter based on the MAX186, and the hardware side is very straightforward. For medium resolution applications, it's hard to find fault with this device. A particularly good feature is the SPI interface, which is not difficult to implement with a parallel port.

Code: MAX186 serial A/D converter

The code should be fairly understandable by inspection. It runs an interval timer in the main loop, which fires every so many seconds (from a value set at compile time). This calls a function which starts a conversion and returns a value. Multiple functions (convert, read_conversion, read_byte) handle different subtasks that make up the whole conversion process.

Various levels of information reporting can be set by changing the booleans named verbose, debug and inform. They allow you to get a glimpse of some of the actions that are happening as the program runs.

Any number of improvements could be made, for instance:

Allowing configuration parameters to be passed to the program from the command line,

The ability to sample multiple channels per call of the program,

Writing of time-stamped logged values to a user-specified file on disk,

The program is run in conjunction with the I/O enabler, in the same way as the earlier examples.

./io ./max_adc

Add-on: LM35 temperature sensor

As an example of doing something truly useful with this, a temperature sensor can be added with some simple electronics. The National Semiconductor LM35 is particularly straightforward to interface to, and it puts out +10 mV per ° Celsius between -55 °C and +150 °C with accuracies of between 0.25 and 0.75 °C, depending on the temperature range over which it is used.

The following schematic gives a circuit that can be connected to one of the inputs of the A/D above, and used to read temperatures between approx. 0 °C and +40 °C. Although the temperature measurement range of the LM35 is much greater than this, I have eliminated the ability to read out temperatures below 0°C by leaving out a resistor at the output of the device (if you want to add this, check the datasheet for details of the value needed.) Bear in mind that you will also need to switch the ADC to read in bipolar mode, so that it will read voltages below zero.

The upper limit of the range is set by the fact that the maximum input voltage to the A/D converter (in unipolar mode) is +4.096 V and I have multiplied the +10 mV/°C output of the LM35 by a factor of 10 (using op-amp U2 in a non-inverting configuration) to give a sensor voltage at the output of +100 mV/°C.