Tested on:Raspberry Pi 3; Raspbian Jessie; Python 2.7
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:LED and fitting resistor (optional)
MCP3008
10k Ohm potentiometer
Breadboard and jumper wires
0

Reading an analog potentiometer with the A/D converter MCP3008 and SPI

An analog potentiometer is a manually adjustable resistor and part of many electronic circuits, e.g. speakers for volume control. For the Raspberry Pi, potentiometers are great to change a device behaviour with no screen attached. For example, the speed of a robot could be modified by the user with a potentiometer. Sadly, the Raspberry Pi does not have an analog input; that’s why an Analog-Digital-Converter (ADC) like the MCP3008 used herein is necessary.
The LED in this tutorial just makes the changes visible and could be left away with no changes.
This tutorial extends an Adafruit solution with a custom SPI protocol. That’s great if the official SPI channels are in use, so, Adafruit, thanks again!

Circuit
Here is the ciruit assembled on a breadboard. The resistor here has 100 Ohm, which is rather high. Less or a blue LED and no resistor would do as well.

Circuit potentiometer with the MCP3008 and an LED

Circuit potentiometer with the MCP3008 and an LED

Running the script
Turn on the Raspberry Pi, then download and run the script by entering

in the terminal. It might be necessary to fix the potentiometer with a finger if it does not sit optimal on the breadboard.
There is another tutorial which explains the software-PWM used for dimming the LED: Running motors with software PWM.
The custom SPI protocol is some non trivial bit-banging. Generally, SPI is a synchronous communication method. The clock pin is alternated to synchronize; a binary command requesting the first channels input is written to mosi, then the binary coded state of the potentiometer is transfered back on miso.
The custom SPI protocol can be used as module: simply import the function read_potentiometer.

The complete script
Click on Full Script to show all of the code.

Demo Video
The blue jumbo LED does not require a resistor for the 3.3V 20mA the RPi delivers.

Links
Software PWM tutorial: http://www.knight-of-pi.org/simple-dc-motor-board-for-the-raspberry-pi-with-ic-l293-and-software-pulse-width-modulation/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.

Comment

Time limit is exhausted. Please reload CAPTCHA.