Some devices like the Raspberry Pi cannot read analog values directly so rely on analog to digital converters, like the ones available from Microchip in the Mcp3000, Mcp3200 and Mcp3300 ranges. These chips can be accessed as an SPI device or manually via raw GPIO pins.
You can use these converters in your project to access analog devices.
The following fritzing diagram illustrates one way to wire up the Mcp3008, with a Raspberry Pi and a potentiometer.
The sample is based on following resources:
- Analog Inputs for Raspberry Pi Using the MCP3008
- Raspberry Pi Analog to Digital Converters.
- Raspbery Pi Analog Input with MCP3008
- MCP3008.py
Major thanks to Adafruit for providing python implementations, which were ported to C# for this sample.
The following elements are used in this sample:
The Raspberry Pi has support for SPI. You need to enable the SPI interface on the Raspberry Pi since it is not enabled by default.
You can use the following code to access the MCP3008 via hardware SPI:
var hardwareSpiSettings = new SpiConnectionSettings(0, 0)
{
ClockFrequency = 1000000
};
using (SpiDevice spi = SpiDevice.Create(hardwareSpiSettings))
using (Mcp3008 mcp = new Mcp3008(spi))
{
while (true)
{
double value = mcp.Read(0);
value = value / 10.24;
value = Math.Round(value);
Console.WriteLine($"{value}%");
Thread.Sleep(500);
}
}
The following pin layout can be used:
- MCP3008 VDD to RPi 3.3V
- MCP3008 VREF to RPi 3.3V
- MCP3008 AGND to RPi GND
- MCP3008 DGND to RPi GND
- MCP3008 CLK to RPi SCLK
- MCP3008 DOUT to RPi MISO
- MCP3008 DIN to RPi MOSI
- MCP3008 CS/SHDN to RPi CE0
You can also access the MCP3008 via GPIO pins, implementing SPI manually. This method is referred to as bit-banging.
You can use the following code to access the MCP3008 via GPIO:
using (SpiDevice spi = new SoftwareSpi(clk: 18, miso: 23, mosi: 24, cs: 25))
using (Mcp3008 mcp = new Mcp3008(spi))
{
while (true)
{
double value = mcp.Read(0);
value = value / 10.24;
value = Math.Round(value);
Console.WriteLine($"{value}%");
Thread.Sleep(500);
}
}
The following pin layout can be used:
- MCP3008 VDD to RPi 3.3V
- MCP3008 VREF to RPi 3.3V
- MCP3008 AGND to RPi GND
- MCP3008 DGND to RPi GND
- MCP3008 CLK to RPi pin 18
- MCP3008 DOUT to RPi pin 23
- MCP3008 DIN to RPi pin 24
- MCP3008 CS/SHDN to RPi pin 25
Independent of the way in which you access the MCP3008 chip, the code to process its results is the same, which follows.
while (true)
{
double value = mcp.Read(0);
value = value / 10.24;
value = Math.Round(value);
Console.WriteLine(value);
Thread.Sleep(500);
}
The chip is 10-bit, which means that it will generate values from 0-1023 (recall that 2^10 is 1024). We can transform the value to a more familiar 0-100 scale by dividing the 10-bit value by 10.24.
These bindings support the following ADC's
-
Mcp3001 10 bit resolution with a single pseudo-differential input.
-
Mcp3002 10 bit resolution with two single ended inputs or a single pseudo-differential input.
-
Mcp3004 10 bit resolution with four single ended inputs or two single pseudo-differential inputs.
-
Mcp3008 10 bit resolution with eight single ended inputs or four single pseudo-differential inputs.
-
Mcp3201 12 bit resolution with a single pseudo-differential input.
-
Mcp3202 12 bit resolution with two single ended inputs or a single pseudo-differential input.
-
Mcp3204 12 bit resolution with four single ended inputs or two single pseudo-differential inputs.
-
Mcp3208 12 bit resolution with eight single ended inputs or four single pseudo-differential inputs.
-
Mcp3301 13 bit signed resolution with a single true differential input.
-
Mcp3202 12 bit resolution with four single ended inputs or 13 bit signed resolution with two true differential inputs.
-
Mcp3304 12 bit resolution with eight single ended inputs or 13 bit signed resolution with four true differential inputs.
Note: Currently untested on the Mcp33xx family.