{"id":165,"date":"2013-12-06T17:01:47","date_gmt":"2013-12-06T17:01:47","guid":{"rendered":"http:\/\/blog.cup-of-joe.me\/?p=165"},"modified":"2014-08-10T13:35:17","modified_gmt":"2014-08-10T13:35:17","slug":"mcp3002-raspberry-pi-python-driver","status":"publish","type":"post","link":"http:\/\/blog.cup-of-joe.me\/?p=165","title":{"rendered":"MCP3002 Raspberry Pi Python Driver"},"content":{"rendered":"<p>When I finished building my weather station it was on a large full size breadboard.  I didn&#8217;t like that it there was so much space on there that was just wasted and I had a half size sitting around that I thought would work well.  Unfortunately, the ADC wouldn&#8217;t let everything else fit properly.  I did some research and found that Microchip also produced a 2 channel ADC called the MCP3002.  Since I was using only 2 channels on the MCP3008, I thought this would work nicely and I counted all the slots on the half size breadboard and it would fit perfectly! So I ordered a couple from Newark&#8230;<\/p>\n<p>When they came in I had to test to make sure it was going to work, so I placed in the half size breadboard, wired up a TMP36 analog temp sensor, and connected the breadboard to my spare Pi.  Well, come to find out, the driver I had didn&#8217;t work.  Neither did the other one I found online nor the others I found.  I was a bit depressed because I really wanted it to work.<\/p>\n<p>My last hope laid on the Adafruit forums.  (Love Adafruit by the way!)  I was a bit worried as they don&#8217;t sell the MCP3002 and they have messages on there about only supporting their products.  So, reluctantly I posted a question on the forum asking if the driver they provide for the MCP3008 would work for the MCP3002.  At first their people informed me that it would work, but I told them I had tested it and it didn&#8217;t.  In one of my responses I made sure that I mentioned that I had wired it as per the datasheet and added a link.  <\/p>\n<p>Come to find out, the reason their driver wouldn&#8217;t work is because the 3002 only wanted 4 bits sent to it for the request and the 3008 wanted 5.  So, Rick (who I think works for Adafruit) helped with adapting that part of the driver.  When I added his piece to the 3008 driver, it still didn&#8217;t work.  I then took another look at the datasheet and found another difference in the 3008 vs the 3002.  The 3002 only provided a 11 bit response vs the 12 that the 3008 provided.  I then updated the code to account for that and voila!  it worked like a charm.<\/p>\n<p>After I got it working, I posted about having to make the other change.  Rick then asked me to post the full driver so that it could be added to their python library.     So yeah, that&#8217;s the story of writing my first driver&#8230;<\/p>\n<p>Here it is by the way if you need it:<\/p>\n<blockquote><p>\ndef readadc(adcnum, clockpin, mosipin, misopin, cspin):<br \/>\n        if ((adcnum > 1) or (adcnum < 0)):\n                return -1\n        GPIO.output(cspin, True)\n\n        GPIO.output(clockpin, False)  # start clock low\n        GPIO.output(cspin, False)     # bring CS low\n\n        commandout = adcnum << 1;\n        commandout |= 0x0D  # start bit + single-ended bit + MSBF bit\n        commandout <<= 4    # we only need to send 4 bits here\n\n        for i in range(4):\n                if (commandout &#038; 0x80):\n                        GPIO.output(mosipin, True)\n                else:\n                        GPIO.output(mosipin, False)\n                commandout <<= 1\n                GPIO.output(clockpin, True)\n                GPIO.output(clockpin, False)\n\n        adcout = 0\n\n        # read in one null bit and 10 ADC bits\n        for i in range(11):\n                GPIO.output(clockpin, True)\n                GPIO.output(clockpin, False)\n                adcout <<= 1\n                if (GPIO.input(misopin)):\n                        adcout |= 0x1\n        GPIO.output(cspin, True)\n\n        adcout \/= 2       # first bit is 'null' so drop it\n        return adcout\n\n# change these as desired - they're the pins connected from the\n# SPI port on the ADC to the Cobbler. There are 2 Chip Select pins. \n# one for each ADC\nSPICLK = 18\nSPIMISO = 23\nSPIMOSI = 24\nSPICS = 25\n\n# set up the SPI interface pins\nGPIO.setup(SPIMOSI, GPIO.OUT)\nGPIO.setup(SPIMISO, GPIO.IN)\nGPIO.setup(SPICLK, GPIO.OUT)\nGPIO.setup(SPICS, GPIO.OUT)<!--more--><!--more-->\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>When I finished building my weather station it was on a large full size breadboard. I didn&#8217;t like that it there was so much space on there that was just wasted and I had a half size sitting around that I thought would work well. Unfortunately, the ADC wouldn&#8217;t let everything else fit properly. I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/posts\/165"}],"collection":[{"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=165"}],"version-history":[{"count":4,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":169,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions\/169"}],"wp:attachment":[{"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}