I am working through a series of lessons with the Raspberry Pi 3 Model B and the next lesson is Raspberry Pi Starter Kit Lesson 10: Raspberry Pi SPI Ports which involves hooking up a MCP3008 ADC output to several pins on the Raspberry Pi and then connecting two of the ADC analogue input pins to the 3.3v rail and the GND rail with a reference voltage of 3.3v from the 3.3v rail.

connections graph for MCP3008 ADC and Raspberry Pi

What I would like to do is to be able to vary the input to the MCP3008 ADC to simulate a sensor such as an analogue temperature sensor.

I was wondering if I could do this by adding a 10K ohm potentiometer in connection between the 3.3v rail and CH0 of the MCP3008 ADC?

Addendum: adding PWM driven LED with varying levels

After adding in the potentiometer, connecting the wiper output to CH2, and looking at the numbers change as I changed the knob setting, I then modified the circuit by adding an LED to GPIO #18, the hardware PWM pin.

So the modified application used a loop to query the relative voltage level, 0 to 1023, from the MCP3008 on the channel to which the potentiometer was connected, CH2. The value was then used to set the Duty Cycle of the GPIO #18 pin which was provisioned as Pulse Width Modulation (PWM) with the default range of 1024. Changing the Duty Cycle in turn changed the illumination level of the LED. So as the knob on the potentiometer was twisted to a new setting, the LED brightness level changed.

Addendum: Use photo-resistor with MCP3008 as alternative

An alternative to using a potentiometer is to use a photo-resistor connected to ground with a 10K ohm pullup resistor connected to the 3.3v rail. A connection is then made between a channel pin of the MCP3008 and a connection point between the 10K ohm resistor and the photo-resistor. enter image description here

The result of this circuit is to provide voltage levels that vary depending on the amount of light the photo-resistor sees. Light levels can be made to vary by using a shading device such as a physical paper tube over the photo-resistor or by using a flashlight to illuminate the photo-resistor.

Addendum: SPI protocol with MCP3008

Here is a nice tutorial and explanation of the MCP3008 and how to interface to it with SPI. Interfacing an SPI ADC (MCP3008) chip to the Raspberry Pi using C++ (spidev).


2 Answers 2


Yes, you can use a pot to provide a variable input to the ADC.

Connect one end of the pot to +3.3V, the other end to Ground, and the wiper to the input of the ADC. As you turn the pot, the voltage at the wiper will vary between 0 and 3.3 V.

  • \$\begingroup\$ Just tried it out and it worked like a champ. Thank you so much. \$\endgroup\$ Dec 9, 2017 at 18:10

To be engineeringly accurate, there is one caveat. The MCP3008 is a plain successive-approximation ADC with naked sample-and-hold (S/H) unit on front end. The S/H unit has 1k series resistance and 20 pF hold capacitor, see Figure 4.1 of the datasheet. This is a fairly low input impedance, and it needs to be considered when sampling relatively high-impedance sources. The chip doesn't have any analog buffers that would allow a high-impedance source to drive this S/H circuit with good accuracy.

The issue is that the 10k source impedance is on the borderline. When the S/H switch engages, the source will try to charge the cap, and the voltage will have a dip. The resulting ADC code might be slightly less than the source voltage DC level. The error will depend on rate of conversion.

The bottom line is that if you want just to see your output code changing and test your software behavior, the 10k trimpot is fine. However, if you need some real accuracy in conversion, with a real sensor, you need to use low-impedance source, at most of about 1k or less, or use a buffer amplifier in front of this ADC. See Figure 4-2 for the relationship between conversion error and source impedance at different conversion rates.

  • \$\begingroup\$ Thank you for the additional information. The kit I have has a couple of 10k trimpots. My electronics knowledge is poor and I am using the Raspberry Pi as a learning bench for practical, hobbyist electronics. The lesson used two fixed voltages and I wanted to add another, varying voltage, just to watch some numbers change as I fiddled with something and to approximate the kind of analog output sensor that I might use later. I have added another component to the project, reading the varying ADC output and using PWM with an LED to show corresponding brightness levels. \$\endgroup\$ Dec 9, 2017 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.