Training
Learning path
This learning path aims to explain learners how to deploy AI at the edge using Azure services.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
An analog-to-digital converter (ADC) is a device that can read an analog input voltage value and convert it into a digital value. ADCs are used for reading values from thermistors, potentiometers, and other devices that change resistance based on certain conditions.
In this topic, you will use .NET to read values from an ADC as you modulate the input voltage with a potentiometer.
Note
This tutorial is written assuming the target device is Raspberry Pi. However, this tutorial can be used for any Linux-based SBC that supports .NET, such as Orange Pi, ODROID, and more.
Ensure your SBC is configured to support the following services:
For many devices, no additional configuration is required. For Raspberry Pi, use the raspi-config
command. For more information on raspi-config
, refer to the Raspberry Pi documentation.
Use the hardware components to build the circuit as depicted in the following diagram:
The MCP3008 uses Serial Peripheral Interface (SPI) to communicate. The following are the connections from the MCP3008 to the Raspberry Pi and potentiometer:
Supply 3.3V and ground to the outer pins on the potentiometer. Order is unimportant.
Refer to the following pinout diagrams as needed:
MCP3008 | Raspberry Pi GPIO |
---|---|
Image courtesy Raspberry Pi Foundation. |
Tip
A GPIO breakout board in conjunction with a breadboard is recommended to streamline connections to the GPIO header.
Complete the following steps in your preferred development environment:
Create a new .NET Console App using either the .NET CLI or Visual Studio. Name it AdcTutorial.
dotnet new console -o AdcTutorial
cd AdcTutorial
Add the Iot.Device.Bindings package to the project. Use either .NET CLI from the project directory or Visual Studio.
dotnet add package Iot.Device.Bindings --version 3.2.0-*
Replace the contents of Program.cs with the following code:
using System;
using System.Device.Spi;
using System.Threading;
using Iot.Device.Adc;
var hardwareSpiSettings = new SpiConnectionSettings(0, 0);
using SpiDevice spi = SpiDevice.Create(hardwareSpiSettings);
using var mcp = new Mcp3008(spi);
while (true)
{
Console.Clear();
double value = mcp.Read(0);
Console.WriteLine($"{value}");
Console.WriteLine($"{Math.Round(value/10.23, 1)}%");
Thread.Sleep(500);
}
In the preceding code:
hardwareSpiSettings
is set to a new instance of SpiConnectionSettings
. The constructor sets the busId
parameter to 0 and the chipSelectLine
parameter to 0.SpiDevice
by calling SpiDevice.Create
and passing in hardwareSpiSettings
. This SpiDevice
represents the SPI bus. The using
declaration ensures the object is disposed and hardware resources are released properly.using
declaration creates an instance of Mcp3008
and passes the SpiDevice
into the constructor.while
loop runs indefinitely. Each iteration:
mcp.Read(0)
.Build the app. If using the .NET CLI, run dotnet build
. To build in Visual Studio, press Ctrl+Shift+B.
Deploy the app to the SBC as a self-contained app. For instructions, see Deploy .NET apps to Raspberry Pi. Make sure to give the executable execute permission using chmod +x
.
Run the app on the Raspberry Pi by switching to the deployment directory and running the executable.
./AdcTutorial
Observe the output as you rotate the potentiometer dial. This is due to the potentiometer varying the voltage supplied to CH0 on the ADC. The ADC compares the input voltage on CH0 to the reference voltage supplied to VREF to generate a value.
Terminate the program by pressing Ctrl+C.
Congratulations! You've used SPI to read values from an analog-to-digital converter.
The source for this tutorial is available on GitHub.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Learning path
This learning path aims to explain learners how to deploy AI at the edge using Azure services.