{"id":1012928,"date":"2024-12-27T11:43:35","date_gmt":"2024-12-27T03:43:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012928.html"},"modified":"2024-12-27T11:43:40","modified_gmt":"2024-12-27T03:43:40","slug":"adc%e5%a6%82%e4%bd%95%e7%9c%8b%e5%87%ba%e6%b8%a9%e5%ba%a6python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1012928.html","title":{"rendered":"ADC\u5982\u4f55\u770b\u51fa\u6e29\u5ea6python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090525\/5216ee36-4fc5-4cc9-9c12-5c71e3141cba.webp\" alt=\"ADC\u5982\u4f55\u770b\u51fa\u6e29\u5ea6python\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u4f7f\u7528ADC\uff08\u6a21\u6570\u8f6c\u6362\u5668\uff09\u6765\u6d4b\u91cf\u6e29\u5ea6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u4e0e\u6e29\u5ea6\u4f20\u611f\u5668\u8fde\u63a5\u7684ADC\u5f15\u811a\u4e0a\u7684\u7535\u538b\u503c\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6e29\u5ea6\u8bfb\u6570\u3002\u4f7f\u7528Python\u5e93\u5982<code>Adafruit CircuitPython<\/code>\u6216<code>RPi.GPIO<\/code>\u53ef\u4ee5\u5e2e\u52a9\u4e0e\u786c\u4ef6\u8fdb\u884c\u4ea4\u4e92\u3001\u9009\u62e9\u5408\u9002\u7684\u4f20\u611f\u5668\u3001\u8fdb\u884c\u6570\u636e\u6821\u51c6\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528ADC\u6765\u83b7\u53d6\u6e29\u5ea6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684ADC\u548c\u6e29\u5ea6\u4f20\u611f\u5668<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u6d4b\u91cf\u6e29\u5ea6\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684ADC\u548c\u6e29\u5ea6\u4f20\u611f\u5668\u3002\u5e38\u7528\u7684\u6e29\u5ea6\u4f20\u611f\u5668\u5305\u62ecLM35\u3001DHT11\/DHT22\u3001\u4ee5\u53caDS18B20\u7b49\u3002\u6bcf\u79cd\u4f20\u611f\u5668\u90fd\u6709\u5176\u7279\u70b9\u548c\u6d4b\u91cf\u8303\u56f4\uff0c\u56e0\u6b64\u9009\u62e9\u9002\u5408\u60a8\u9879\u76ee\u9700\u6c42\u7684\u4f20\u611f\u5668\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>LM35\u4f20\u611f\u5668<\/strong><\/p>\n<\/p>\n<p><p>LM35\u662f\u5e38\u7528\u7684\u7ebf\u6027\u6e29\u5ea6\u4f20\u611f\u5668\uff0c\u5176\u8f93\u51fa\u7535\u538b\u4e0e\u6444\u6c0f\u6e29\u5ea6\u6210\u6b63\u6bd4\u3002\u5b83\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7cbe\u5ea6\u9ad8\uff0c\u4e0d\u9700\u8981\u5916\u90e8\u6821\u51c6\u3002LM35\u7684\u8f93\u51fa\u662f\u6a21\u62df\u4fe1\u53f7\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u63a5\u5165ADC\u8fdb\u884c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>DHT11\/DHT22\u4f20\u611f\u5668<\/strong><\/p>\n<\/p>\n<p><p>DHT\u7cfb\u5217\u4f20\u611f\u5668\u53ef\u4ee5\u6d4b\u91cf\u6e29\u5ea6\u548c\u6e7f\u5ea6\uff0c\u662f\u6570\u5b57\u4f20\u611f\u5668\uff0c\u9002\u5408\u521d\u5b66\u8005\u4f7f\u7528\u3002\u867d\u7136\u5b83\u4eec\u4e0d\u9700\u8981ADC\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u9700\u8981\u7ed3\u5408ADC\u8fdb\u884c\u5176\u4ed6\u6a21\u62df\u4fe1\u53f7\u7684\u6d4b\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>DS18B20\u4f20\u611f\u5668<\/strong><\/p>\n<\/p>\n<p><p>DS18B20\u662f\u4e00\u4e2a\u6570\u5b57\u6e29\u5ea6\u4f20\u611f\u5668\uff0c\u4f7f\u75281-Wire\u63a5\u53e3\u8fdb\u884c\u901a\u4fe1\u3002\u5b83\u975e\u5e38\u9002\u5408\u9700\u8981\u957f\u8ddd\u79bb\u6570\u636e\u4f20\u8f93\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u786c\u4ef6\u8fde\u63a5\u548c\u8f6f\u4ef6\u73af\u5883<\/p>\n<\/p>\n<p><p>\u5728\u9009\u5b9a\u4f20\u611f\u5668\u548cADC\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u8fdb\u884c\u786c\u4ef6\u8fde\u63a5\u548c\u8f6f\u4ef6\u8bbe\u7f6e\u3002\u4ee5Raspberry Pi\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528MCP3008 ADC\u82af\u7247\u6765\u8bfb\u53d6\u6a21\u62df\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786c\u4ef6\u8fde\u63a5<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8fde\u63a5\u4f20\u611f\u5668\u7684\u6a21\u62df\u8f93\u51fa\u5f15\u811a\u5230MCP3008\u7684\u8f93\u5165\u5f15\u811a\u3002<\/li>\n<li>\u5c06MCP3008\u7684VDD\u548cVREF\u5f15\u811a\u8fde\u63a5\u5230Raspberry Pi\u76843.3V\u5f15\u811a\uff0cGND\u5f15\u811a\u63a5\u5730\u3002<\/li>\n<li>\u4f7f\u7528SPI\u63a5\u53e3\u5c06MCP3008\u8fde\u63a5\u5230Raspberry Pi\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8f6f\u4ef6\u73af\u5883\u8bbe\u7f6e<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5Python\u5e93\uff1a\u786e\u4fdd\u5b89\u88c5\u4e86<code>spidev<\/code>\u548c<code>Adafruit_GPIO<\/code>\u7b49\u5e93\u3002<\/li>\n<li>\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff0c\u4f8b\u5982\uff1a<code>pip install spidev<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7f16\u5199Python\u4ee3\u7801\u8fdb\u884c\u6e29\u5ea6\u6d4b\u91cf<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528MCP3008\u548cLM35\u4f20\u611f\u5668\u8bfb\u53d6\u6e29\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import spidev<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efaSPI\u5bf9\u8c61<\/strong><\/h2>\n<p>spi = spidev.SpiDev()<\/p>\n<p>spi.open(0, 0)  # \u6253\u5f00SPI\u603b\u7ebf<\/p>\n<p>spi.max_speed_hz = 1350000<\/p>\n<h2><strong>\u4eceMCP3008\u8bfb\u53d6\u6570\u636e\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def read_adc(channel):<\/p>\n<p>    if channel &lt; 0 or channel &gt; 7:<\/p>\n<p>        return -1<\/p>\n<p>    adc = spi.xfer2([1, (8 + channel) &lt;&lt; 4, 0])<\/p>\n<p>    data = ((adc[1] &amp; 3) &lt;&lt; 8) + adc[2]<\/p>\n<p>    return data<\/p>\n<h2><strong>\u5c06ADC\u503c\u8f6c\u6362\u4e3a\u7535\u538b<\/strong><\/h2>\n<p>def convert_volts(data, places):<\/p>\n<p>    volts = (data * 3.3) \/ float(1023)<\/p>\n<p>    volts = round(volts, places)<\/p>\n<p>    return volts<\/p>\n<h2><strong>\u5c06\u7535\u538b\u8f6c\u6362\u4e3a\u6e29\u5ea6<\/strong><\/h2>\n<p>def convert_temp(volts, places):<\/p>\n<p>    temp = volts * 100  # \u6839\u636eLM35\u7684\u7279\u6027\uff0c\u6e29\u5ea6 = \u7535\u538b * 100<\/p>\n<p>    temp = round(temp, places)<\/p>\n<p>    return temp<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        adc_value = read_adc(0)<\/p>\n<p>        volts = convert_volts(adc_value, 2)<\/p>\n<p>        temp = convert_temp(volts, 2)<\/p>\n<p>        print(f&quot;ADC Value: {adc_value}, Voltage: {volts}V, Temperature: {temp}C&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    spi.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6570\u636e\u6821\u51c6\u548c\u7cbe\u5ea6\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u6e29\u5ea6\u6570\u636e\u540e\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u6821\u51c6\u548c\u4f18\u5316\u4ee5\u63d0\u9ad8\u6d4b\u91cf\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6821\u51c6<\/strong><\/p>\n<\/p>\n<p><p>\u6839\u636e\u4f20\u611f\u5668\u7684\u6570\u636e\u624b\u518c\u8fdb\u884c\u6821\u51c6\uff0c\u786e\u4fdd\u6d4b\u91cf\u503c\u4e0e\u5b9e\u9645\u6e29\u5ea6\u76f8\u7b26\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u6ee4\u6ce2<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u91c7\u96c6\u591a\u6b21\u6570\u636e\u5e76\u6c42\u5e73\u5747\u503c\u6765\u51cf\u5c11\u5076\u7136\u8bef\u5dee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u73af\u5883\u56e0\u7d20\u8c03\u6574<\/strong><\/p>\n<\/p>\n<p><p>\u8003\u8651\u73af\u5883\u6e29\u5ea6\u3001\u6e7f\u5ea6\u7b49\u56e0\u7d20\u5bf9\u6d4b\u91cf\u7cbe\u5ea6\u7684\u5f71\u54cd\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6269\u5c55\u5e94\u7528\u548c\u9879\u76ee\u6848\u4f8b<\/p>\n<\/p>\n<p><p>\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528ADC\u83b7\u53d6\u6e29\u5ea6\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u5e94\u7528\u5230\u66f4\u590d\u6742\u7684\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u667a\u80fd\u5bb6\u5c45<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u6e29\u5ea6\u6570\u636e\u4e0e\u5176\u4ed6\u4f20\u611f\u5668\u6570\u636e\u7ed3\u5408\uff0c\u6784\u5efa\u667a\u80fd\u5bb6\u5c45\u7cfb\u7edf\uff0c\u5b9e\u73b0\u81ea\u52a8\u7a7a\u8c03\u63a7\u5236\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u8bb0\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u5b9e\u73b0\u6570\u636e\u8bb0\u5f55\u529f\u80fd\uff0c\u5b58\u50a8\u6e29\u5ea6\u53d8\u5316\u6570\u636e\uff0c\u7528\u4e8e\u957f\u671f\u76d1\u6d4b\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u62a5\u8b66\u7cfb\u7edf<\/strong><\/p>\n<\/p>\n<p><p>\u7ed3\u5408\u6e29\u5ea6\u6570\u636e\uff0c\u8bbe\u7f6e\u62a5\u8b66\u7cfb\u7edf\uff0c\u5728\u6e29\u5ea6\u5f02\u5e38\u65f6\u89e6\u53d1\u8b66\u62a5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u6210\u529f\u5728Python\u4e2d\u4f7f\u7528ADC\u6765\u6d4b\u91cf\u6e29\u5ea6\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u9879\u76ee\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u5e94\u7528\u3002\u5e0c\u671b\u8fd9\u4e9b\u4fe1\u606f\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0ADC\u6e29\u5ea6\u6d4b\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6ADC\u7684\u6e29\u5ea6\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bfb\u53d6ADC\uff08\u6a21\u6570\u8f6c\u6362\u5668\uff09\u6e29\u5ea6\u6570\u636e\u901a\u5e38\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u548c\u786c\u4ef6\u63a5\u53e3\u3002\u5e38\u89c1\u7684\u5e93\u5305\u62ecAdafruit\u7684CircuitPython\u5e93\u6216PySerial\uff0c\u7528\u4e8e\u4e0e\u5916\u90e8\u6e29\u5ea6\u4f20\u611f\u5668\uff08\u5982LM35\u3001DS18B20\u7b49\uff09\u8fdb\u884c\u901a\u4fe1\u3002\u7528\u6237\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u5e76\u901a\u8fc7GPIO\u63a5\u53e3\u6216I2C\/SPI\u534f\u8bae\u8fde\u63a5\u4f20\u611f\u5668\u3002\u4ee3\u7801\u793a\u4f8b\u4f1a\u6d89\u53ca\u521d\u59cb\u5316ADC\u3001\u8bfb\u53d6\u6570\u503c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6e29\u5ea6\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bfb\u53d6ADC\u6e29\u5ea6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4f20\u611f\u5668\u7684\u8bef\u5dee\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6e29\u5ea6\u65f6\uff0c\u4f20\u611f\u5668\u53ef\u80fd\u5b58\u5728\u4e00\u5b9a\u7684\u8bef\u5dee\u3002\u53ef\u4ee5\u901a\u8fc7\u6821\u51c6\u4f20\u611f\u5668\u6765\u6539\u5584\u6d4b\u91cf\u7cbe\u5ea6\u3002\u6821\u51c6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5df2\u77e5\u6e29\u5ea6\u73af\u5883\u6765\u6bd4\u8f83\u548c\u8c03\u6574\u4f20\u611f\u5668\u8f93\u51fa\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5e73\u5747\u503c\u6ee4\u6ce2\u6216\u4e2d\u4f4d\u6570\u6ee4\u6ce2\u7b49\u6280\u672f\u4e5f\u53ef\u4ee5\u51cf\u5c11\u5076\u7136\u8bef\u5dee\u7684\u5f71\u54cd\uff0c\u4ece\u800c\u63d0\u9ad8\u6e29\u5ea6\u6d4b\u91cf\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06ADC\u7684\u6e29\u5ea6\u6570\u636e\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u53ef\u89c6\u5316\u6e29\u5ea6\u6570\u636e\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Seaborn\u7b49\u5e93\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u5b9e\u65f6\u56fe\u8868\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u76f4\u89c2\u5730\u5206\u6790\u6e29\u5ea6\u53d8\u5316\u3002\u7528\u6237\u53ef\u4ee5\u8bbe\u7f6e\u5b9a\u65f6\u8bfb\u53d6ADC\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u7ed8\u5236\u6210\u7ebf\u56fe\u6216\u67f1\u72b6\u56fe\uff0c\u751a\u81f3\u53ef\u4ee5\u5728\u56fe\u8868\u4e0a\u6dfb\u52a0\u6e29\u5ea6\u9608\u503c\uff0c\u4ee5\u4fbf\u4e8e\u76d1\u63a7\u548c\u9884\u8b66\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u4f7f\u7528ADC\uff08\u6a21\u6570\u8f6c\u6362\u5668\uff09\u6765\u6d4b\u91cf\u6e29\u5ea6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u4e0e\u6e29\u5ea6\u4f20\u611f\u5668\u8fde\u63a5\u7684ADC\u5f15\u811a\u4e0a\u7684\u7535\u538b\u503c\uff0c\u5e76\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1012939,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012928"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1012928"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012928\/revisions"}],"predecessor-version":[{"id":1012941,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012928\/revisions\/1012941"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012939"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1012928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1012928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1012928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}