nanoFramework.Iot.Device.Mcp3xxx 1.2.864

Prefix Reserved
dotnet add package nanoFramework.Iot.Device.Mcp3xxx --version 1.2.864
                    
NuGet\Install-Package nanoFramework.Iot.Device.Mcp3xxx -Version 1.2.864
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.Iot.Device.Mcp3xxx" Version="1.2.864" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nanoFramework.Iot.Device.Mcp3xxx" Version="1.2.864" />
                    
Directory.Packages.props
<PackageReference Include="nanoFramework.Iot.Device.Mcp3xxx" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add nanoFramework.Iot.Device.Mcp3xxx --version 1.2.864
                    
#r "nuget: nanoFramework.Iot.Device.Mcp3xxx, 1.2.864"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package nanoFramework.Iot.Device.Mcp3xxx@1.2.864
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=nanoFramework.Iot.Device.Mcp3xxx&version=1.2.864
                    
Install as a Cake Addin
#tool nuget:?package=nanoFramework.Iot.Device.Mcp3xxx&version=1.2.864
                    
Install as a Cake Tool

MCP3001/MCP3002/MCP3004/MCP3008/MCP3201/MCP3202/MCP3204/MCP3208/MCP3301/MCP3302/MCP3304 family of Analog to Digital Converters

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. Normally all MCU has an analogic capability but the number of pins can be limited. This is typically useful when you want your MCU to have more than the number of ADC available. Another case is if you need a different resolution or voltage reference.

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 an ESP32 and a potentiometer.

ESP32 Breadboard diagram

Documentation

The sample is based on following resources:

Major thanks to Adafruit for providing python implementations, which were ported to C# for this sample.

Usage

Important: make sure you properly setup the SPI pins especially for ESP32 before creating the SpiDevice, make sure you install the nanoFramework.Hardware.ESP32 nuget:

//////////////////////////////////////////////////////////////////////
// when connecting to an ESP32 device, need to configure the SPI GPIOs
// used for the bus
Configuration.SetPinFunction(21, DeviceFunction.SPI1_MOSI);
Configuration.SetPinFunction(22, DeviceFunction.SPI1_MISO);
Configuration.SetPinFunction(23, DeviceFunction.SPI1_CLOCK);
// Make sure as well you are using the right chip select

For other devices like STM32, please make sure you're using the preset pins for the SPI bus you want to use. The chip select can as well be pre setup.

Hardware elements

The following elements are used in this sample:

Accessing the MCP3008 via SPI

The MCU has support for SPI.

You can use the following code to access the MCP3008 via hardware SPI:

var hardwareSpiSettings = new SpiConnectionSettings(1, 42)
{
    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);
        Debug.WriteLine($"{value}%");
        Thread.Sleep(500);
    }
}

The following pin layout can be used:

  • MCP3008 VDD to MCU 3.3V
  • MCP3008 VREF to MCU 3.3V
  • MCP3008 AGND to MCU GND
  • MCP3008 DGND to MCU GND
  • MCP3008 CLK to MCU SCLK
  • MCP3008 DOUT to MCU MISO
  • MCP3008 DIN to MCU MOSI
  • MCP3008 CS/SHDN to MCU CE0

ESP32 Breadboard diagram

Accessing the MCP3008 via GPIO

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);
        Debug.WriteLine($"{value}%");
        Thread.Sleep(500);
    }
}

The following pin layout can be used:

  • MCP3008 VDD to MCU 3.3V
  • MCP3008 VREF to MCU 3.3V
  • MCP3008 AGND to MCU GND
  • MCP3008 DGND to MCU GND
  • MCP3008 CLK to any valid GPIO on the MCU
  • MCP3008 DOUT to any valid GPIO on the MCU
  • MCP3008 DIN to any valid GPIO on the MCU
  • MCP3008 CS/SHDN to any valid GPIO on the MCU

ESP32 Breadboard diagram

Processing the data

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);
    Debug.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.

Remarks

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.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.864 201 4/2/2025
1.2.852 195 3/11/2025
1.2.822 137 2/26/2025
1.2.775 125 2/4/2025
1.2.772 131 2/4/2025
1.2.755 149 1/31/2025
1.2.737 119 1/13/2025
1.2.704 132 12/18/2024
1.2.696 124 12/16/2024
1.2.673 144 10/23/2024
1.2.631 143 8/28/2024
1.2.590 145 7/17/2024
1.2.570 147 6/14/2024
1.2.560 148 5/29/2024
1.2.548 148 5/15/2024
1.2.436 282 11/10/2023
1.2.416 143 11/8/2023
1.2.329 215 5/26/2023
1.2.316 205 5/16/2023
1.2.313 211 5/12/2023
1.2.297 201 5/3/2023
1.2.212 375 1/5/2023
1.2.203 391 12/28/2022
1.2.159 454 11/14/2022
1.2.153 449 11/5/2022
1.2.141 466 10/25/2022
1.2.122 503 10/12/2022
1.2.114 470 10/8/2022
1.2.95 531 9/22/2022
1.2.87 594 9/15/2022
1.2.73 501 9/8/2022
1.2.5 518 7/13/2022
1.1.141.41205 518 7/6/2022
1.1.116.8772 539 6/24/2022
1.1.113.2032 533 6/23/2022
1.1.97.17326 526 6/13/2022
1.1.92.53000 507 6/8/2022
1.1.58.10097 523 5/23/2022
1.1.27 535 4/26/2022
1.1.20 533 4/21/2022
1.1.3 565 4/15/2022
1.1.1 519 4/14/2022
1.0.300 546 3/31/2022
1.0.277-preview.126 195 3/25/2022
1.0.277-preview.125 191 3/25/2022
1.0.277-preview.116 180 3/22/2022
1.0.277-preview.115 179 3/21/2022
1.0.277-preview.112 188 3/19/2022
1.0.277-preview.111 187 3/18/2022
1.0.277-preview.110 187 3/18/2022
1.0.277-preview.106 196 3/15/2022
1.0.277-preview.105 197 3/15/2022
1.0.277-preview.99 188 3/10/2022
1.0.277-preview.98 194 3/8/2022
1.0.277-preview.89 191 2/27/2022
1.0.277-preview.87 178 2/26/2022
1.0.277-preview.85 187 2/25/2022
1.0.277-preview.77 183 2/18/2022
1.0.277-preview.75 191 2/16/2022
1.0.277-preview.73 192 2/12/2022
1.0.277-preview.70 191 2/10/2022
1.0.277-preview.65 195 2/9/2022
1.0.277-preview.60 207 2/4/2022
1.0.277-preview.53 214 1/31/2022
1.0.277-preview.41 202 1/28/2022
1.0.277-preview.32 212 1/27/2022
1.0.277-preview.30 202 1/27/2022
1.0.277-preview.17 211 1/24/2022
1.0.277-preview.13 200 1/21/2022
1.0.277-preview.1 203 1/11/2022
1.0.272 569 1/10/2022
1.0.259 431 12/9/2021
1.0.258 400 12/7/2021
1.0.221 243 10/19/2021
1.0.219 246 10/19/2021
1.0.218 265 10/18/2021
1.0.157 242 9/4/2021
1.0.155 243 8/31/2021
1.0.153 234 8/14/2021
1.0.151 252 8/6/2021
1.0.146 240 7/22/2021
1.0.136 311 7/17/2021
1.0.135 239 7/16/2021
1.0.134 244 7/15/2021
1.0.133 268 7/14/2021
1.0.131 244 7/8/2021
1.0.129 240 7/6/2021
1.0.127 261 7/5/2021
1.0.125 272 7/5/2021
1.0.122 293 6/30/2021
1.0.121 279 6/29/2021
1.0.119 303 6/28/2021
1.0.105 360 5/29/2021
1.0.97 250 5/28/2021
1.0.63 254 5/26/2021
1.0.47 266 5/24/2021