Potentiometer sensor for Arduino

This sample shows how to connect a rotary potentiometer and LED to a Raspberry Pi 2 and 3 or a MinnowBoard Max using Arduino Wiring. We use a SPI-based ADC (Analog to Digital Converter) to read values from the potentiometer and control an LED based on the knob position.

This sample is similar to the C# Potentiometer Sample, but uses Arduino Wiring instead of C#. The wiring and hardware configuration will be nearly identical. However, DragonBoard 410c does not currently support Arduino Wiring.

Hardware

Hardware Setup

In this sample, you have the option of using either the MCP3002, MCP3008, or MCP3208 ADC (Analog to Digital Converter). The differences between the chips are the number of input channels and resolution. 12-bit resolution is more accurate than the 10-bit options, and the number of channels determines how many different inputs you can read. Any of these options will work fine for this sample.

Below are the pinouts of the MCP3002 and MCP3208 ADCs.

MCP3002 MCP3008 or MCP3208
MCP3002 Pinout MCP3208 Pinout

Raspberry Pi

Raspbery Pi 2 and 3 Pinout

Raspberry Pi 2 and 3 pinout

Wiring & Connections

MCP3002

If you chose to use the MCP3002, assemble the circuit as follows. Note that the wiper pin (the middle pin on the 10k potentiometer) should be connected to CH0 on MCP3002. You can also refer to the datasheet for more information.

Detailed connection:

Overall Schematics

The MCP3002 should be connected as follows:

  • VDD/VREF - 3.3V on Raspberry Pi 2 or 3 (Pin 1)
  • CLK - “SPI0 SCLK” on Raspberry Pi 2 or 3 (Pin 23)
  • Dout - “SPI0 MISO” on Raspberry Pi 2 or 3 (Pin 21)
  • Din - “SPI0 MOSI” on Raspberry Pi 2 or 3 (Pin 19)
  • CS/SHDN - “SPI0 CS0” on Raspberry Pi 2 or 3 (Pin 24)
  • Vss - GND on Raspberry Pi 2 or 3 (Pin 6 or any other GND pin)
  • CH0 - Potentiometer wiper pin
MCP3208 or MCP3008

If you chose to use the MCP3208 or MCP3008, assemble the circuit as follows (pinouts are identical on each chip). Note that the wiper pin (the middle pin on the 10k potentiometer) should be connected to CH0 on MCP3208. You can also refer to the MCP3208 datasheet or the MCP3008 datasheet for more information.

Detailed connection:

RPi2 Fritzing 3008

The MCP3208 should be connected as follows:

  • VDD - 3.3V on Raspberry Pi 2 or 3 (Pin 1)
  • VREF - 3.3V on Raspberry Pi 2 or 3 (Pin 1)
  • AGND - GND on Raspberry Pi 2 or 3 (Pin 6 or any other GND pin)
  • CLK - “SPI0 SCLK” on Raspberry Pi 2 or 3 (Pin 23)
  • Dout - “SPI0 MISO” on Raspberry Pi 2 or 3 (Pin 21)
  • Din - “SPI0 MOSI” on Raspberry Pi 2 or 3 (Pin 19)
  • CS/SHDN - “SPI0 CS0” on Raspberry Pi 2 or 3 (Pin 24)
  • DGND - GND on Raspberry Pi 2 or 3 (Pin 6 or any other GND pin)
  • CH0 - Potentiometer wiper pin

MinnowBoard Max

MinnowBoard Max Pinout

MinnowBoard Max Pinout

Wiring & Connections

MCP3002

If you chose to use the MCP3002, assemble the circuit as follows. Note that the wiper pin (the middle pin on the 10k potentiometer) should be connected to CH0 on MCP3002. You can also refer to the datasheet for more information.

Detailed connection:

MBM Fritzing 3002

The MCP3002 should be connected as follows:

  • VDD/VREF - 3.3V on MBM (Pin 4)
  • CLK - “SPI0 SCLK” on MBM (Pin 11)
  • Dout - “SPI0 MISO” on MBM (Pin 7)
  • Din - “SPI0 MOSI” on MBM (Pin 9)
  • CS/SHDN - “SPI0 CS0” on MBM (Pin 5)
  • Vss - GND on MBM (Pin 1 or 2)
  • CH0 - Potentiometer wiper pin
MCP3208 or MCP3008

If you chose to use the MCP3208 or MCP3008, assemble the circuit as follows (pinouts are identical on each chip). Note that the wiper pin (the middle pin on the 10k potentiometer) should be connected to CH0 on MCP3208. You can also refer to the MCP3208 datasheet or the MCP3008 datasheet for more information.

Detailed connection:

MBM Fritzing 3208

  • VDD - 3.3V on MBM (Pin 4)
  • VREF - 3.3V on MBM (Pin 4)
  • AGND - GND on MBM (Pin 1)
  • CLK - “SPI0 SCLK” on MBM (Pin 11)
  • Dout - “SPI0 MISO” on MBM (Pin 9)
  • Din - “SPI0 MOSI” on MBM (Pin 7)
  • CS/SHDN - “SPI0 CS0” MBM (Pin 5)
  • DGND - GND on MBM (Pin 1)
  • CH0 - Potentiometer wiper pin

Code

Replace the existing code in your main .ino file with the following code:

/*
 * we'll set this channel number to match the channel we've plugged our potentiometer's
 * wiper pin into. In this case, channel 0 (C0). If you use a different channel,
 * you'll need to update this number.
 */
int channel = 0;

void setup()
{
    /*
     * it is not necessary to set the channel to input, as the analogRead command must
     * assume the 'pin number' it is given refers to a channel on the ADC chip.
     */
}

void loop()
{
    //read the value from the ADC channel
    int value = analogRead( channel );

    //report the value to the output console
    Log( L"Analog value: " );
    Log( value.ToString()->Begin() );
    Log( "\n" );

    //delay for 500ms
    delay( 500 );
}

Build and deploy

Press F5 to build and deploy your project.

Refer to the Arduino Wiring Project Guide for more instructions on how to deploy your app!

Result

You will see the LED lit to indicate that power is flowing, and the current value of the potentiometer’s wiper pin will be read and displayed in the output console twice per second!

Having trouble?

Refer to the Arduino Wiring Porting Guide for common issues and concerns when working with Arduino Wiring sketches.


« Return to Samples

No sample code available

Language: Wiring

Tags: beginner, c#, gpio, Wiring, foreground

Verified to work with:
Windows 10 IoT Core: Version 10.0.15063.0
Visual Studio 2017
Windows SDK: Version 15063
(Included with Visual Studio)


Edit this page on GitHub


Was this page helpful?
Additional feedback?
Thank you! We appreciate your feedback.
Follow us