{"id":129784,"date":"2023-06-14T20:07:13","date_gmt":"2023-06-14T20:07:13","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=129784"},"modified":"2023-07-19T11:04:07","modified_gmt":"2023-07-19T11:04:07","slug":"raspberry-pi-analog-inputs-python-mcp3008","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-analog-inputs-python-mcp3008\/","title":{"rendered":"Raspberry Pi: Read Analog Inputs with Python (MCP3008)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to read analog signals on the Raspberry Pi GPIOs using a Python program. The Raspberry Pi can only read digital signals. To read analog signals, we need to use an analog-to-digital converter like the MCP3008, for example. As an example, we&#8217;ll read the values from a potentiometer, but the example can be applied to any analog sensor. We&#8217;ll use the <span class=\"rnthl rntliteral\">gpiozero<\/span> interface.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Analog-Input.png?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Read Raspberry Pi Analog Inputs with Python MCP3008\" class=\"wp-image-129886\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Analog-Input.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Analog-Input.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Analog-Input.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Analog-Input.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<p>Throughout this tutorial, we\u2019ll cover the following main topics:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#raspberry-pi-gpios-introduction\" title=\"\">Introducing the Raspberry Pi GPIOs<\/a><\/li>\n\n\n\n<li><a href=\"#introducing-mcp3008\" title=\"\">Introducing the MCP3008 Analog-to-Digital Converter<\/a><\/li>\n\n\n\n<li><a href=\"#wire-mcp3008-raspberry-pi\" title=\"\">Wire the Raspberry Pi to the MCP3008<\/a><\/li>\n\n\n\n<li><a href=\"#enable-spi\" title=\"\">Enabling SPI on the Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"#raspberry-pi-read-analog-signals-python-script\" title=\"\">Reading Analog Signals with the Raspberry Pi &#8211; Python Script<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before continuing with this tutorial, check the following prerequisites.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Get familiar with the Raspberry Pi board\u2014if you&#8217;re not familiar with the Raspberry Pi, you can read our <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\" title=\"\"><strong>Raspberry Pi Getting Started Guide here<\/strong>.<\/a><\/li>\n\n\n\n<li>You must know how to run and create Python files on your Raspberry Pi. We like to program our Raspberry Pi via SSH using an extension on VS Code. We have a detailed tutorial about that subject: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-remote-ssh-vs-code\/\" title=\"\"><strong>Programming Raspberry Pi Remotely using VS Code (Remote-SSH)<\/strong><\/a>.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"raspberry-pi-gpios-introduction\">Introducing the Raspberry Pi GPIOs<\/h2>\n\n\n\n<p>GPIO stands for <em>General Purpose Input Output<\/em> pins and those allow you to connect and control electronic hardware, like LEDs, motors, and sensors to your Raspberry Pi.<\/p>\n\n\n\n<p>Most models of Raspberry Pi boards have a double row of 40 GPIO pins. The layout of the pins is usually the same for most Raspberry Pi models.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=1024%2C576&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Random Nerd Tutorials\" class=\"wp-image-129396\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>If you&#8217;re not familiar with the Raspberry Pi GPIOs, we recommend taking a look at our Raspberry Pi Pinout Guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\">Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>To show you how to read analog signals with the Raspberry Pi GPIOs, we&#8217;ll build a simple project to control the brightness of an LED using a potentiometer. So, you&#8217;ll learn how to read analog signals with the Raspberry Pi and output PWM signals.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-analog-Input-Potentiometer.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi read Analog Inputs with MCP3008 Potentiometer\" class=\"wp-image-129888\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-analog-Input-Potentiometer.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-analog-Input-Potentiometer.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>For an introduction to PWM signals with the Raspberry Pi, read the following guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pwm-python\/\">Raspberry Pi: PWM Outputs with Python (Fading LED)<\/a><\/li>\n<\/ul>\n\n\n\n<p>To learn how potentiometers work, you can read this guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/electronics-basics-how-a-potentiometer-works\/\" title=\"\">Electronics Basics \u2013 How a Potentiometer Works<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>For this project, you&#8217;ll need the following parts:<\/p>\n\n\n\n<p>Here&#8217;s a list of components you need:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Board<\/a> &#8211; read <a href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-starter-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Best Raspberry Pi Starter Kits<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/potentiometer-assortment-kit\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Potentiometer<\/a> (we&#8217;ll be using a 10KOhm, but any other value should work) or any analog sensor<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mcp3008-analog-to-digital-converter\/\" target=\"_blank\" rel=\"noopener\" title=\"\">MCP3008 chip<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener\" title=\"\">LED<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">220 Ohm resistor<\/a> (or similar)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reading Analog Signals with the Raspberry Pi<\/h2>\n\n\n\n<p>The Raspberry Pi GPIOs can only read digital signals\u2014they can read either HIGH (3.3V) or LOW (0V), but they can&#8217;t read any voltages in between. To be able to read varying voltage levels, we need to use an analog-to-digital converter chip like the MCP3008.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"introducing-mcp3008\">Introducing the MCP3008 Analog-to-Digital Converter<\/h3>\n\n\n\n<p>The figure below shows an MCP3008 analog-to-digital converter chip.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"281\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/MCP3008-f.jpg?resize=350%2C281&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MCP3008 chip analog to digital converter\" class=\"wp-image-129873\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/MCP3008-f.jpg?w=350&amp;quality=100&amp;strip=all&amp;ssl=1 350w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/MCP3008-f.jpg?resize=300%2C241&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/figure><\/div>\n\n\n<p>The MCP3008 chip is a 10-bit digital-to-analog converter that reads analog signals and sends them to a microcontroller via SPI communication protocol (SPI signals that the Raspberry Pi can read).<\/p>\n\n\n\n<p>The MCP3008 comes with 16 pins. Half of those pins are analog inputs that you can use to connect to analog devices. The other 8 pins connect to the Raspberry Pi GPIOs.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"206\" height=\"300\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/MCP3008-pin-numbering.png?resize=206%2C300&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MCP3008 pin numbering\" class=\"wp-image-129874\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/MCP3008-pin-numbering.png?resize=206%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 206w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/MCP3008-pin-numbering.png?w=424&amp;quality=100&amp;strip=all&amp;ssl=1 424w\" sizes=\"(max-width: 206px) 100vw, 206px\" \/><\/figure><\/div>\n\n\n<p>The following table shows the pinout of the MCP3008 chip. To correctly identify each pin, place the chip with the half-circle mark at the top and follow the notation of the picture above.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Pin<\/strong><\/td><td><strong>Symbol<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>CH0<\/td><td>Analog input (channel 0)<\/td><\/tr><tr><td><strong>2<\/strong><\/td><td>CH1<\/td><td>Analog input (channel 1)<\/td><\/tr><tr><td><strong>3<\/strong><\/td><td>CH2<\/td><td>Analog input (channel 2)<\/td><\/tr><tr><td><strong>4<\/strong><\/td><td>CH3<\/td><td>Analog input (channel 3)<\/td><\/tr><tr><td><strong>5<\/strong><\/td><td>CH4<\/td><td>Analog input (channel 4)<\/td><\/tr><tr><td><strong>6<\/strong><\/td><td>CH5<\/td><td>Analog input (channel 5)<\/td><\/tr><tr><td><strong>7<\/strong><\/td><td>CH6<\/td><td>Analog input (channel 6)<\/td><\/tr><tr><td><strong>8<\/strong><\/td><td>CH7<\/td><td>Analog input (channel 7)<\/td><\/tr><tr><td><strong>9<\/strong><\/td><td>DGND<\/td><td>Digital ground<\/td><\/tr><tr><td><strong>10<\/strong><\/td><td>CS\/SHDN<\/td><td>Chip select\/shutdown pin<\/td><\/tr><tr><td><strong>11<\/strong><\/td><td>Din<\/td><td>Serial data in<\/td><\/tr><tr><td><strong>12<\/strong><\/td><td>Dout<\/td><td>Serial data out<\/td><\/tr><tr><td><strong>13<\/strong><\/td><td>CLK<\/td><td>Serial clock<\/td><\/tr><tr><td><strong>14<\/strong><\/td><td>AGND<\/td><td>Analog ground<\/td><\/tr><tr><td><strong>15<\/strong><\/td><td>VRef<\/td><td>Reference voltage input<\/td><\/tr><tr><td><strong>16<\/strong><\/td><td>VDD<\/td><td>+2.7V to 5.5V power supply<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wire-mcp3008-raspberry-pi\">Wiring the MCP3008 Chip to the Raspberry Pi<\/h2>\n\n\n\n<p>The 8 pins at the left side of the MCP3008 are eight different analog channels. You can connect up to 8 analog devices using those channels. The pins on the right side connect to the Raspberry Pi.<\/p>\n\n\n\n<p>The easiest way to wire an MCP3008 chip to the Raspberry Pi is using a breadboard. Place the MCP3008 chip in the middle of the breadboard. Then, connect it as shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>MCP3008 Pin<\/strong><\/td><td><strong>Raspberry Pi<\/strong><\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>Analog Output<br>(potentiometer or analog sensor)<\/td><\/tr><tr><td><strong>9<\/strong><\/td><td>GND<\/td><\/tr><tr><td><strong>10<\/strong><\/td><td>GPIO 8<\/td><\/tr><tr><td><strong>11<\/strong><\/td><td>GPIO 10<\/td><\/tr><tr><td><strong>12<\/strong><\/td><td>GPIO 9<\/td><\/tr><tr><td><strong>13<\/strong><\/td><td>GPIO 11<\/td><\/tr><tr><td><strong>14<\/strong><\/td><td>GND<\/td><\/tr><tr><td><strong>15<\/strong><\/td><td>3.3V<\/td><\/tr><tr><td><strong>16<\/strong><\/td><td>3.3V<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>You can also use the following diagram as a reference. We&#8217;re also wiring an LED to GPIO 14.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-wiring_potentiometer_MCP3008.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"514\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-wiring_potentiometer_MCP3008.png?resize=750%2C514&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wire potentiometer to Raspberry Pi using MCP3008\" class=\"wp-image-129882\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-wiring_potentiometer_MCP3008.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-wiring_potentiometer_MCP3008.png?resize=300%2C206&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"enable-spi\">Enabling SPI on the Raspberry Pi<\/h2>\n\n\n\n<p>By default, SPI communication is not enabled on the Raspberry Pi. Run the following command on the Raspberry Pi terminal window to enable SPI:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo raspi-config nonint do_spi 0<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"raspberry-pi-read-analog-signals-python-script\">Reading Analog Signals with the Raspberry Pi &#8211; Python Script<\/h2>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">gpiozero<\/span> library comes with an <span class=\"rnthl rntliteral\">MCP3008<\/span> class, specially developed to read analog signals from the MCP3008 chip.<\/p>\n\n\n\n<p>The following example reads the value from the potentiometer and adjusts the LED brightness accordingly:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>read the analog input value from the potentiometer through the MCP3008 chip;<\/li>\n\n\n\n<li>control the LED brightness using PWM;<\/li>\n\n\n\n<li>change the PWM duty cycle accordingly to the input value from the potentiometer.<\/li>\n<\/ul>\n\n\n\n<p>Create a new Python file called <em>potentiometer_led.py<\/em> and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-analog-inputs-python-mcp3008\/\r\n\r\nfrom gpiozero import PWMLED, MCP3008\r\nfrom time import sleep\r\n\r\n#create an object called pot that refers to MCP3008 channel 0\r\npot = MCP3008(0)\r\n\r\n#create a PWMLED object called led that refers to GPIO 14\r\nled = PWMLED(14)\r\n\r\nwhile True:\r\n    if(pot.value &lt; 0.001):\r\n        led.value = 0\r\n    else:\r\n        led.value = pot.value\r\n    print(pot.value)\r\n    sleep(0.1)<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi\/inputs\/potentiometer_led.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to Read Analog Inputs on the Raspberry Pi<\/h3>\n\n\n\n<p>Reading analog inputs on the Raspberry Pi using the <span class=\"rnthl rntliteral\">MCP3008<\/span> class is very straightforward.<\/p>\n\n\n\n<p>1. Import the <span class=\"rnthl rntliteral\">MCP3008<\/span> class.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import MCP3008<\/code><\/pre>\n\n\n\n<p>2. Create an <span class=\"rnthl rntliteral\">MCP3008<\/span> object and pass as an argument the analog channel you&#8217;re using. You can create different MCP3008 objects on different channels if you&#8217;re using multiple analog peripherals.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot = MCP3008(<strong>ANALOG_CHANNEL<\/strong>)<\/code><\/pre>\n\n\n\n<p>3. Read the value of the analog input using the <span class=\"rnthl rntliteral\">value<\/span> property.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot.value<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Now that you know how to read analog inputs on the Raspberry Pi GPIOs, it&#8217;s easy to understand the code.<\/p>\n\n\n\n<p>You start by importing the required libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import PWMLED, MCP3008\nfrom time import sleep<\/code><\/pre>\n\n\n\n<p>Then, you create an object called <span class=\"rnthl rntliteral\">pot<\/span> that refers to MCP3008 channel 0, which is the channel that the potentiometer is connected to. Remember that channel 0 corresponds to the MCP3008 pin 1, channel 1 to pin2, and so on.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot = MCP3008(0)<\/code><\/pre>\n\n\n\n<p>You also create a <span class=\"rnthl rntliteral\">PMWLED<\/span> object called <span class=\"rnthl rntliteral\">led<\/span> to control the LED using PWM. To learn how to use PWM on the Raspberry Pi pins <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pwm-python\/\">follow this tutorial<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = PWMLED(14)<\/code><\/pre>\n\n\n\n<p>To read the analog value on the GPIO that the potentiometer is connected to, you simply use <span class=\"rnthl rntliteral\">pot.value<\/span>.<\/p>\n\n\n\n<p>To adjust the LED brightness using PWM, you need to change its duty cycle &#8211; you just need to attribute a value between 0 and 1 to the <span class=\"rnthl rntliteral\">led.value<\/span>, in which <span class=\"rnthl rntliteral\">0<\/span> corresponds to a fully off LED and <span class=\"rnthl rntliteral\">1<\/span> to a fully on LED.<\/p>\n\n\n\n<p>In this example, we&#8217;re using a while loop that is always <span class=\"rnthl rntliteral\">True<\/span> to keep the program running forever.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>while True:<\/code><\/pre>\n\n\n\n<p>We&#8217;re constantly checking the value of the potentiometer. If the value is below 0.001, we set the duty cycle to zero to turn the LED off.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>if(pot.value &lt; 0.001):\n    led.value = 0<\/code><\/pre>\n\n\n\n<p>Otheriwse, the code will run the else statement in which the duty cycle changes accordingly to the value read from the potentiometer.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>else:\n    led.value = pot.value\n    print(pot.value)<\/code><\/pre>\n\n\n\n<p>Finally, we print the analog value of the potentiometer and wait 0.1 seconds before checking the potentiometer value again.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print(pot.value)\nsleep(0.1)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Save your python file. Then run it on your Raspberry Pi. Run the following command on the directory of your project file (use the name of your file):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python potentiometer_led.py<\/pre>\n\n\n\n<p>After running the script, rotate the potentiometer and see the LED brightness adjusting accordingly.<\/p>\n\n\n\n<div class=\"wp-block-jetpack-tiled-gallery aligncenter is-style-square\"><div class=\"tiled-gallery__gallery\"><div class=\"tiled-gallery__row columns-2\"><div class=\"tiled-gallery__col\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-control-LED-with-Potentiometer.jpg?resize=422%2C422&#038;strip=info&#038;ssl=1 422w\" alt=\"Raspberry Pi Control LED with Potentiometer\" data-height=\"422\" data-id=\"129889\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=129889#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-control-LED-with-Potentiometer.jpg\" data-width=\"750\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-control-LED-with-Potentiometer.jpg?ssl=1&amp;resize=422%2C422\" data-amp-layout=\"responsive\"\/><\/figure><\/div><div class=\"tiled-gallery__col\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i2.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-control-LED-with-Potentiometer-1.jpg?resize=422%2C422&#038;strip=info&#038;ssl=1 422w\" alt=\"Raspberry Pi Control LED with Potentiometer\" data-height=\"422\" data-id=\"129890\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=129890#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-control-LED-with-Potentiometer-1.jpg\" data-width=\"750\" src=\"https:\/\/i2.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-control-LED-with-Potentiometer-1.jpg?ssl=1&amp;resize=422%2C422\" data-amp-layout=\"responsive\"\/><\/figure><\/div><\/div><\/div><\/div>\n\n\n\n<p>You can stop the execution of the program by pressing&nbsp;<strong>CTRL<\/strong>+<strong>C<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to read analog values on the Raspberry Pi GPIOs using a Python script. The Raspberry Pi GPIOs can&#8217;t read analog signals, so we need to use an analog-to-digital converter chip like the MCP3008 chip that sends the analog signals via SPI communication protocol that the Pi can read.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">gpiozero<\/span> library provides an easy interface called <span class=\"rnthl rntliteral\">MCP3008<\/span> that allows us to easily get the analog values using the <span class=\"rnthl rntliteral\">value<\/span> property. As an example, we&#8217;ve shown you how to use a potentiometer, but you can use the same methods for any analog sensor that outputs a maximum of 3.3V.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. If you want to learn more about the basics of controlling the Raspberry Pi GPIOs using Python programming language, take a look at our Raspberry Pi tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pwm-python\/\">Raspberry Pi: PWM Outputs with Python (Fading LED)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi\/\">Control Raspberry Pi Digital Outputs with Python (LED)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-digital-outputs-python\/\">More Raspberry Pi Guides and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can check all our Raspberry Pi projects on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi\/\" title=\"\">Free Raspberry Pi Projects and Tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to read analog signals on the Raspberry Pi GPIOs using a Python program. The Raspberry Pi can only read digital signals. To read analog &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi: Read Analog Inputs with Python (MCP3008)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-analog-inputs-python-mcp3008\/#more-129784\" aria-label=\"Read more about Raspberry Pi: Read Analog Inputs with Python (MCP3008)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":129886,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301,264,190,268],"tags":[],"class_list":["post-129784","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-raspberrypi","category-project","category-raspberry-pi","category-raspberry-pi-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Analog-Input.png?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/129784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=129784"}],"version-history":[{"count":23,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/129784\/revisions"}],"predecessor-version":[{"id":133371,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/129784\/revisions\/133371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/129886"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=129784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=129784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=129784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}