Easy Project 19k Raspberry PI Zero W board - Potentiometer with LED

of Acoptex.com in Raspberry Pi Zero W

Raspberry basics: Project 19k

Project name: Raspberry PI Zero W board - Potentiometer with LED

Tags: Raspberry, Raspberry PI Zero W board, vers 1.1, v 1.1, LED, dimmer switch, potentiometer, ADC, Pulse-Width Modulation, PWM, analog signals, digital signals, Analog-to-Digital, analog to digital converter, MCP 3008 ADC chip, MCP3008 IC, gpiozero library, time library

Attachments: potentiometer.py

Raspberry Pi
Monitor or TV
HDMI cable
USB keyboard
USB mouse
Power supply
8GB SD card

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1. Raspberry PI Zero W board 1 pc

2. Micro SD card with NOOBS and SD card adapter 1 pc

3. Micro USB power supply (2 A 5V or 5V 3A) 1 pc

4. USB keyboard 1 pc

5. USB mouse 1 pc

6. TV or PC monitor 1 pc

7. HDMI cable 1 pc

8. T-Cobbler Breakout and GPIO Cable 1 pc

9. Micro USB 2.0 OTG Cable 1 pc

10. Mini HDMI to HDMI Adapter (HDMI to Mini HDMI Adapter) 1 pc

11. 4-Port USB 2.0 Hub 1 pc

12. MCP 3008 ADC chip 1 pc

13. Jumper cables F-M, M-M

14. LED 1 pc (any color)

15. Resistor 1 pc (220 Ohm)

16. Breadboard  1 pc

17. 10 KOhm potentiometer 1 pc


We will learn how to make a dimmer switch to control the brightness of an LED using a potentiometer with Raspberry PI Zero W board. 

We will learn how to read analog inputs with the Raspberry Pi and output pulse-width modulation signals.

How to read analog signals with Raspberry Pi

The Raspberry Pi GPIOs can read only digital signals - they can read HIGH (3.3 V) or LOW (0 V) only. However, the potentiometer is an analog input, and rotating the knob changes its output voltage from 0 V up to 3.3 V. We need  the Pi to be able to read all the values between 0 V and 3.3 V. For this, you need to convert the analog signal to digital with an analog-to-digital (ADC) converter chip, and then create imitation analog signals using pulse-width modulation. 

Understanding the Pulse-Width Modulation (PWM)

You can read more about it here.

Understanding the potentiometer

You can read more about it here.

Understanding the MCP3008 chip

The Microchip Technology Inc. MCP3008 devices are successive approximation 10-bit Analog-to-Digital (A/D) converters with on-board sample and hold circuitry. The MCP3008 is programmable to provide four pseudo-differential input pairs or eight single-ended inputs. Differential Nonlinearity (DNL) and Integral Nonlinearity (INL) are specified at ±1 LSB. Communication with the devices is accomplished using a simple serial interface compatible with the SPI protocol. The devices are capable of conversion rates of up to 200 ksps. The MCP3008 devices operate over a broad voltage range (2.7V - 5.5V). Low-current design permits operation with typical standby currents of only 5 nA and typical active currents of 320 µA.  


  • Sensor Interface
  • Process Control
  • Data Acquisition
  • Battery Operated Systems

You can find specification here.

Understanding the Raspberry PI Zero W board

You can read more about it here.

Signals and connections of the MCP3008 chip

MCP3008 ADC will convert the potentiometer's analog signal to to digital signal.

MCP3008 ADC has a total of 16 pins out of which 8 pins are for taking the analog input. The analog input pins are from CH0-CH7 (Pins 1-8). On the other side, we have different pins which are as follows:

DGND is digital ground pin for the chip.

CS is the chip select pin.

DIN is the data input pin from the Raspberry Pi.

DOUT is the data output pin.

CLK is the clock pin.

AGND is the analog ground pin.

VREF is the analog reference voltage. Connect to 3.3V. You can change it if you want to change the scale.

VDD is the power pin for the chip (+2.7V to 5.5V)

Signals and connections of the potentiometer

 Signals and connections of the Raspberry PI Zero W board


Before applying power to the circuit make sure that you have connected the MCP3008 chip correctly or you can damage the chip.

To identify the MCP3008 pins, orient the chip so it’s facing you with the half-circle cutout at the top. The first pin is the top left and the last pin is the top right.

MCP3008 ADC chip pins | Raspberry Pi Zero W board

1 Potentiometer middle lead

9     GND

10   GPIO 8

11   GPIO 10

12   GPIO 9

13   GPIO 11

14   GND

15   3.3 V

16   3.3 V

Step by Step instruction

We recommend using a high-performance SD card for increased stability as well as plugging your device into an external display to see the default application booting up.

1. Setup and preparation

We assume that you have Windows 10 installed on your PC and Raspbian OS installed on your Raspberry Pi Zero W board. 

  1. Do wiring.
  2. Insert your micro SD card with Raspbian OS into the TF card slot on the Raspberry Pi Zero W board. It will only fit one way.
  3. Connect Raspberry PI Zero W board mini HDMI port to your TV or Monitor HDMI (DVI) port (use HDMI cable and mini HDMI to HDMI adapter and/or HDMI to DVI adapter).
  4. Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI/DVI,  etc).
  5. Plug in micro USB 2.0 OTG Cable to USB data port of Pi Zero and 4-Port USB 2.0 Hub to micro USB 2.0 OTG Cable. 
  6. Plug in your USB mouse and USB keyboard to 4-Port USB 2.0 Hub.
  7. If you intend to connect your Raspberry Pi Zero vers 1.2 or vers 1.3 to the internet, connect a WiFi dongle to one of the 4-Port USB 2.0 Hub ports.
  8. Connect Micro USB power supply to Raspberry PI Zero board micro USB input.
  9. The Raspberry PI desktop will start up.
  10. Open Terminal window and type the command: sudo apt-get update
  11. Then type the command: sudo apt-get install 

2. Making the program

  1. The Raspberry Pi reads the analog values from the MCP3008 ADC chip using SPI communication, so we need to enable that first. Open the Raspberry Pi Configuration tool from the main menu. Go to Raspberry icon-> Preferences -> Raspberry PI configuration

  2. Select Interfaces and make sure that the SPI is enabled. If it’s not enabled, enable it and reboot your Raspberry PI 3 board to begin. We suggest to enable all settings and you will not need to come back to these configuration tool again.

  3. Click on OK button.
  4. We need to reboot our Raspberry PI. Go to Terminal. Type this command in the Terminal: sudo reboot
  5. After restart of Raspberry Pi go to Terminal. Type this command in the Terminal: sudo nano potentiometer.py

  6. Copy and paste the code from potentiometer.py to opened window. Since it is a Python code, you need to be careful with the Tab characters as it is important to group the instruction as blocks in Python.

  7. Press Ctrl+X, Y, Enter buttons to save the file.
  8. Type the command in the Terminal: sudo python potentiometer.py


With our script we will be able to:

  • Read an analog input value from the potentiometer through the MCP3008 chip.
  • Control the brightness of the LED with PWM.
  • Change the duty cycle of the PWM according to the input value read from the potentiometer.

First we start our code by importing the required libraries. Then we create an object called pot that refers to MCP3008 channel 0 as the potentiometer attached to this channel (CH0). MCP3008 ADC chip: pin 1 (channel 0), pin 2 (channel 1) and so on.

The gpiozero library allows you to control an LED’s brightness with PWM by using a PWMLED object.  We create the PWMLED object called led that refers to GPIO17, pin the LED attached to.

To read an analog value using the gpiozero library, you simply get the potentiometer value with pot.value. Remember that pot refers to MCP3008 channel 0. You’ll get decimal values between 0 and 1.

To adjust the LED brightness using PWM, you need to change its duty cycle. To adjust the duty cycle, you simply have to attribute a value between 0 and 1 to led.value, in which 0 corresponds to the LED OFF and 1 to the LED ON (100%).

In this script, a while loop that is always true keeps the program running. This loop is constantly checking the potentiometer values: if the value read from the potentiometer is below 0.001 , the duty cycle is set to 0 , which turns your LED off. Otherwise, the code enters the else block, in which the duty cycle changes according to the value read from the potentiometer.


We have learnt how to make a dimmer switch to control the brightness of an LED using a potentiometer with Raspberry PI Zero W board and how to read analog inputs with the Raspberry Pi and output pulse-width modulation signals.

We can suggest you to build another DIY projects with LEDs and potentiometer using the knowledge which you gained now - for example, control several LEDs, build a bar graph of LEDs, adjusting the blinking speed of a LED.

Libraries in use

  • gpiozero
  • time


  • Look for attached code on the begining of this project

Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 29-12-2018
Viewed: 2910 times