{"id":1800,"date":"2019-10-12T14:59:16","date_gmt":"2019-10-12T06:59:16","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1800"},"modified":"2020-05-12T13:41:29","modified_gmt":"2020-05-12T05:41:29","slug":"how-to-use-mcp3008-i-p-chip-to-help-your-raspberry-pi-read-digital-data-from-analog-sensors","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-mcp3008-i-p-chip-to-help-your-raspberry-pi-read-digital-data-from-analog-sensors\/","title":{"rendered":"How to use the MCP3008-I\/P chip to help your Raspberry Pi read digital data from analog sensors"},"content":{"rendered":"<p>Although your <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=Raspberry+Pi&ref=nb_sb_noss&linkCode=ll2&tag=clivsperswebs-20&linkId=0411b8c4a030fdb528632ff8768c527a&language=en_US\" rel=\"noopener\" target=\"_blank\">Raspberry Pi<\/a> is unable to read analog input out of the box, you can get a <a href=\"https:\/\/www.amazon.com\/Microchip-MCP3008-I-10-Bit-ADC-Pack\/dp\/B01HGCSGXM\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=73a13c6c57a5f69eb6878431d6710ce1\" rel=\"noopener\" target=\"_blank\">MCP3008 I\/P ADC chip<\/a> to help your Raspberry Pi do so.<\/p>\n<p><a href=\"https:\/\/www.amazon.com\/Microchip-MCP3008-I-10-Bit-ADC-Pack\/dp\/B01HGCSGXM\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=73a13c6c57a5f69eb6878431d6710ce1\" rel=\"noopener\" target=\"_blank\"><img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/MCP3008-IP-ADC-chip-up-close.jpg\" class=\"aligncenter size-full wp-image-1809\" alt=\"MCP3008 IP ADC chip up close\"><\/a><\/p>\n<p>When you connect a MCP3008 I\/P chip to your Raspberry Pi, you can read up to 8 analog inputs with SPI.<\/p>\n<p>Given these points, this post will show you how you can use the MCP3008 I\/P chip to help your Raspberry Pi read digital data from analog sensors.<\/p>\n<h2>Parts used in this tutorial<\/h2>\n<p>In case you need it, these are the items that I had used for creating the content in this article:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.amazon.com\/seeed-studio-Raspberry-Computer-Model\/dp\/B07WBZM4K9\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=65a1be8909256b70ba609b99195705d8&language=en_US\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 4 Model B board<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/SanDisk-Ultra-microSDXC-Memory-Adapter\/dp\/B073JYC4XM\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=d0cdc1986a4ef589f65ce48617bf42d9&language=en_US\" rel=\"noopener\" target=\"_blank\">SanDisk 128GB Ultra microSDXC UHS-I Memory Card<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll2&tag=clivsperswebs-20&linkId=ce5551c1e1fb9b638d1dd018640bd87b&language=en_US\" rel=\"noopener\" target=\"_blank\">Solderless Prototype Breadboard<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/EDGELEC-Breadboard-Optional-Assorted-Multicolored\/dp\/B07GD2BWPY\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=ed1cd66ecc676e5369d92c76673bff51&language=en_US\" rel=\"noopener\" target=\"_blank\">Breadboard Jumper Wires<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/Microchip-MCP3008-I-10-Bit-ADC-Pack\/dp\/B01HGCSGXM\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=cabe336d1363c9835a0f31469c60a926&language=en_US\" rel=\"noopener\" target=\"_blank\">MCP3008 I\/P ADC chip<\/a><\/li>\n<\/ul>\n<h2>Wiring your Raspberry Pi with the MCP3008 I\/P chip<\/h2>\n<p>First of all, let's take a look at how we can wire the MCP3008 I\/P chip with your Raspberry Pi.<\/p>\n<p>So what is this black piece of plastic with 16 pins?<\/p>\n<p>In order to understand how we can connect the MCP3008 pins with our Raspberry Pi, let's look at the pin reference:<br \/>\n<img decoding=\"async\" width=\"741\" height=\"594\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/MCP3008-IP-chip-pin-reference.gif\" class=\"aligncenter size-full wp-image-1810\" alt=\"MCP3008 IP chip pin reference\"><\/p>\n<p>As shown above, with the semi-circle positioned at the top, we have 8 pins to the left and right of the chip.<\/p>\n<p>The 8 pins to the left takes the analog inputs that we wish to read with our Raspberry Pi. You will connect the analog output of the sensor that you wish to readf to one of this pins.<\/p>\n<p>The 8 pins to the right are what we will connect to our Raspberry Pi:<\/p>\n<ul>\n<li><strong>VDD<\/strong> (Pin 16) to <strong>3.3V<\/strong> pin on Raspberry Pi<\/li>\n<li><strong>VREF<\/strong> (Pin 15) to <strong>3.3V<\/strong> pin on Raspberry Pi<\/li>\n<li><strong>AGND<\/strong> (Pin 14) to <strong>GND<\/strong> pin on Raspberry Pi<\/li>\n<li><strong>CLK<\/strong> (Pin 13) to <strong>Pin 23\/SCLK<\/strong> on Raspberry Pi<\/li>\n<li><strong>DOUT<\/strong> (Pin 12) to <strong>Pin 21\/MISO<\/strong> on Raspberry Pi<\/li>\n<li><strong>DIN<\/strong> (Pin 11) to <strong>Pin 19\/MOSI<\/strong> on Raspberry Pi<\/li>\n<li><strong>CS<\/strong> (Pin 10) to <strong>Pin 24\/CE0<\/strong> on Raspberry Pi<\/li>\n<li><strong>DGND<\/strong> (Pin 9) to <strong>GND<\/strong> on Raspberry Pi<\/li>\n<\/ul>\n<p>Given these points, the following diagram depicts how you can connect these 8 pins to your Raspberry Pi:<br \/>\n<img decoding=\"async\" width=\"902\" height=\"565\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/fritzing-screenshot-of-MCP3008-wired-to-Raspberry-Pi-3B-via-breadboard.gif\" alt=\"Fritzing screenshot of MCP3008 wired to Raspberry Pi 3B via breadboard\" class=\"aligncenter size-full wp-image-1814\" \/><\/p>\n<p>After you have connected the MCP3008 I\/P chip to your Raspberry Pi, your setup may look like the following:<br \/>\n<img decoding=\"async\" width=\"800\" height=\"820\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/real-world-example-of-MCP3008-chip-wired-to-a-Raspberry-Pi-4-Model-B.jpg\" alt=\"real-world example of MCP3008 chip wired to a Raspberry Pi 4 Model B\" class=\"aligncenter size-full wp-image-1813\" \/><\/p>\n<h2>Setting up Raspbian to run your Raspberry Pi<\/h2>\n<p>At this point in time, you should have already setup a version of Raspbian to run your Raspberry Pi.<\/p>\n<p>If you have not done so, you can follow one of the following articles to help you do so:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-raspbian-buster-lite-for-raspberry-pi-server-projects\/\" rel=\"noopener\" target=\"_blank\">How to setup Raspbian Buster Lite for Raspberry Pi server projects<\/a><\/li>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-raspbian-buster-for-your-raspberry-pi-4-model-b\/\" rel=\"noopener\" target=\"_blank\">How to setup Raspbian Buster for your Raspberry Pi 4 Model B<\/a><\/li>\n<\/ul>\n<h2>Enabling the SPI master driver with raspi-config<\/h2>\n<p>Once you had setup Raspbian on your Raspberry Pi, <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-enable-spi-master-driver-in-raspbian-using-raspi-config\/\" rel=\"noopener\" target=\"_blank\">enable SPI master driver with <\/a><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-enable-spi-master-driver-in-raspbian-using-raspi-config\/\" rel=\"noopener\" target=\"_blank\"><code>raspi-config<\/code><\/a>.<\/p>\n<p>When you had done so, your Raspberry Pi will be able to communicate with the MCP3008 I\/P chip.<\/p>\n<h2>Setting up a Python 3 virtual environment for reading analog inputs through the MCP3008 I\/P chip<\/h2>\n<p>Since I am a fan of <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-python-3-virtual-environments-to-run-python-3-applications-on-your-raspberry-pi\/\">using Python 3 virtual environments to run applications on a Raspberry Pi<\/a>, I will show you how to setup the virtual environment for the Python 3 application that we will run later.<\/p>\n<p>With this in mind, let's create the virtual environment with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 -m venv ~\/mcp3008-demo-venv\r\n<\/pre>\n<p>After the command complete, we will have the Python 3 virtual environment located in the ~\/mcp3008-demo-venv directory.<\/p>\n<p>Next up, let's activate the virtual environment with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsource ~\/mcp3008-demo-venv\/bin\/activate\r\n<\/pre>\n<p><img decoding=\"async\" width=\"660\" height=\"450\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Screenshot-of-Raspbian-terminal-after-creating-and-activating-mcp3008-demo-venv.gif\" alt=\"Screenshot of Raspbian terminal after creating and activating mcp3008-demo-venv\" class=\"aligncenter size-full wp-image-1815\" \/><\/p>\n<p>Given that, we will be able to install the dependencies that we need for our Python 3 application.<\/p>\n<p>When we wish to read analog inputs through a MCP3008 I\/P chip with Python 3, we can use <a href=\"https:\/\/github.com\/adafruit\/Adafruit_CircuitPython_MCP3xxx\" target=\"_blank\" rel=\"noopener\">Adafruit's CircuitPython library for MCP300x SPI ADC<\/a>.<\/p>\n<p>Given that, let's install Adafruit's CircuitPython Library for MCP300x SPI ADC with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install adafruit-circuitpython-mcp3xxx\r\n<\/pre>\n<p><img decoding=\"async\" width=\"660\" height=\"450\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Screenshot-of-Raspbian-terminal-after-installing-Adafruits-CircuitPython-library-for-MCP300x-SPI-ADC-mcp3008-demo-venv.gif\" alt=\"Screenshot of Raspbian terminal after installing Adafruit&#039;s CircuitPython library for MCP300x SPI ADC mcp3008-demo-venv\"  class=\"aligncenter size-full wp-image-1816\" \/><\/p>\n<p>When the pip installation is successful, you will be able to read analog inputs from your Python 3 script inside that virtual environment.<\/p>\n<h2>Sample script to read raw ADC and ADC voltage from analog input connected to first channel of chip<\/h2>\n<p>After we had setup the Python 3 virtual environment, let's look at how we can read from one of the analog input with Python.<\/p>\n<p>For example, if we want to read the analog input connected to the first input channel (CH0) of the MCP3008 I\/P pin, we can run the following script:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport busio\r\nimport digitalio\r\nimport board\r\nimport time\r\nimport adafruit_mcp3xxx.mcp3008 as MCP\r\nfrom adafruit_mcp3xxx.analog_in import AnalogIn\r\n\r\n# create the spi bus\r\nspi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)\r\n\r\n# create the cs (chip select)\r\ncs = digitalio.DigitalInOut(board.CE0)\r\n\r\n# create the mcp object\r\nmcp = MCP.MCP3008(spi, cs)\r\n\r\n# create an analog input channel on pin 0\r\nchan = AnalogIn(mcp, MCP.P0)\r\n\r\nwhile True:\r\n    print('Raw ADC Value: ', chan.value)\r\n    print('ADC Voltage: ' + str(chan.voltage) + 'V')\r\n    time.sleep(2)\r\n<\/pre>\n<p>When you run this script, a MCP object is created from the SPI bus that points to the SCLK, MISO, MOSI and CE0 pins of your Raspberry Pi. It then creates an analog input channel that can be used to read the analog input channel on pin 0 of the MCP3008 chip. <\/p>\n<p>After it had setup the channel, it then reads and prints the Raw ADC value and ADC voltage detected at pin 0 every two seconds. <\/p>\n<p>If you wish to read analog inputs connected to other pins on the MCP3008 chip, simply change the second input parameter to <code>AnalogIn<\/code>.<\/p>\n<p>For example, if I want to read from the analog input channel on pin 7, I will change:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nchan = AnalogIn(mcp, MCP.P0)\r\n<\/pre>\n<p>to<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nchan = AnalogIn(mcp, MCP.P7)\r\n<\/pre>\n<p>When I run this script with an analog input connected to the MCP3008 I\/P chip, I get the following output:<br \/>\n<img decoding=\"async\" width=\"660\" height=\"450\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Screenshot-of-Raspbian-terminal-after-running-sample-analog-input-reading-script-inside-mcp3008-demo-venv.gif\" alt=\"Screenshot of Raspbian terminal after running sample analog input reading script inside mcp3008-demo-venv\" class=\"aligncenter size-full wp-image-1818\" \/><\/p>\n<p>At this point in time, you should be able to use this setup to read up to 8 analog inputs from your Raspberry Pi. Typically, an analog sensor will have at least 3 pins: <strong>+<\/strong>, <strong>-<\/strong> and <strong>AO<\/strong>. You will attach the <strong>+<\/strong> pin to either <strong>5V<\/strong> or <strong>3.3V<\/strong> power source, the <strong>-<\/strong> pin to <strong>ground<\/strong> and the <strong>AO<\/strong> pin to one of the <strong>CH0<\/strong> to <strong>CH7<\/strong> pins on the MCP3008 chip:<br \/>\n<img decoding=\"async\" width=\"917\" height=\"560\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Screenshot-of-Fritzing-with-an-analog-sensor-connected-to-MCP3008-and-Raspberry-Pi-3.gif\" alt=\"Screenshot of Fritzing with an analog sensor connected to MCP3008 and Raspberry Pi 3\" class=\"aligncenter size-full wp-image-1819\" \/><\/p>\n<p><img decoding=\"async\" width=\"600\" height=\"900\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-use-the-MCP3008-IP-chip-to-help-your-Raspberry-Pi-read-digital-data-from-analog-sensors.jpg\" alt=\"How to use the MCP3008-IP chip to help your Raspberry Pi read digital data from analog sensors\" class=\"aligncenter size-full wp-image-1820\" \/><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-t2\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-t2&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-t2&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1800&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Although your <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=Raspberry+Pi&#038;ref=nb_sb_noss&#038;linkCode=ll2&#038;tag=clivsperswebs-20&#038;linkId=0411b8c4a030fdb528632ff8768c527a&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">Raspberry Pi<\/a> is unable to read analog input out of the box, you can get a <a href=\"https:\/\/www.amazon.com\/Microchip-MCP3008-I-10-Bit-ADC-Pack\/dp\/B01HGCSGXM\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=73a13c6c57a5f69eb6878431d6710ce1\" rel=\"noopener\" target=\"_blank\">MCP3008 I\/P ADC chip<\/a> to help your Raspberry Pi do so.<\/p>\n<p><a href=\"https:\/\/www.amazon.com\/Microchip-MCP3008-I-10-Bit-ADC-Pack\/dp\/B01HGCSGXM\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=73a13c6c57a5f69eb6878431d6710ce1\" rel=\"noopener\" target=\"_blank\"><img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/MCP3008-IP-ADC-chip-up-close.jpg\" class=\"aligncenter size-full wp-image-1809\" alt=\"MCP3008 IP ADC chip up close\"><\/a><\/p>\n<p>When you connect a MCP3008 I\/P chip to your Raspberry Pi, you can read up to 8 analog inputs with SPI.<\/p>\n<p>Given these points, this post will show you how you can use the MCP3008 I\/P chip to help your Raspberry Pi read digital data from analog sensors.<\/p>\n","protected":false},"author":1,"featured_media":1814,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[690,484,689,226,233,240,412,195],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/fritzing-screenshot-of-MCP3008-wired-to-Raspberry-Pi-3B-via-breadboard.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-t2","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1800"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1800"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1800\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1814"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}