Analog to Digital to Analog

The Gertboard has an MCP3002 dual-channel 10-bit, analog to digital converter. This is connected to the SPI bus as device number 0, as well as an MCP4802 dual-channel, 8-bit digital to analog converter. These can be used from your own programs as follows:


#include <wiringPi.h>
#include <gertboard.h>


gertboardAnalogSetup (int pinBase) ;


analogWrite (pinCase + channel, value);
value = analogRead (pinBase + channel) ;

pinBase is the first pin number you want the analog hardware to appear at for the analogRead() and analogWrite() functions. Note that the input and output channels overlap each other – the channels are 0 and 1.


In the Gertboard directory you’ll find voltmeter.c and temperature.c. The first is a simple voltmeter to read the analog inputs. You can connect a potentiometer to one of the Gertboard analog inputs, or connect one of the analog outputs to an input. If you connect an output, then you can use the gpio command to set an output value – e.g. gpio gbw 0 127 will write the value 127 to analog output channel 0 – if this is connected to an input channel you should read approximately 1.02 volts.

temperature.c is designed to be used to the LM35 (or equivalent) temperature sensors. These are reasonably accurate analog sensors.

The program gertboard.c is designed to have analog output 0 connected analog input 0, and generates a site ave on the output and reads the input, and plots the value horizontally on the terminal, whole scrolling up. The result should be a vertically scrolling sine wave on the terminal if all is working well.