{"id":992412,"date":"2024-12-27T08:37:06","date_gmt":"2024-12-27T00:37:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/992412.html"},"modified":"2024-12-27T08:37:09","modified_gmt":"2024-12-27T00:37:09","slug":"python%e5%a6%82%e4%bd%95%e6%8e%a7%e5%88%b6%e7%94%b5%e6%9c%ba%e8%bd%ac%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/992412.html","title":{"rendered":"python\u5982\u4f55\u63a7\u5236\u7535\u673a\u8f6c\u52a8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070242\/2c1f3704-ba19-44bf-ad6f-dea32e168569.webp\" alt=\"python\u5982\u4f55\u63a7\u5236\u7535\u673a\u8f6c\u52a8\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u63a7\u5236\u7535\u673a\u8f6c\u52a8\uff0c\u53ef\u4ee5\u4f7f\u7528GPIO\u5e93\u3001PWM\u4fe1\u53f7\u63a7\u5236\u3001\u901a\u8fc7Arduino\u6216\u8005\u5176\u4ed6\u5fae\u63a7\u5236\u5668\u8fdb\u884c\u63a7\u5236\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528GPIO\u5e93\u7ed3\u5408\u6811\u8393\u6d3e\u8fdb\u884c\u63a7\u5236\u662f\u8f83\u4e3a\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u8fd9\u91cc\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u548c\u6811\u8393\u6d3e\u63a7\u5236\u76f4\u6d41\u7535\u673a\u8f6c\u52a8\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001GPIO\u5e93\u63a7\u5236\u7535\u673a<\/p>\n<\/p>\n<p><p>\u6811\u8393\u6d3e\u4e0a\u7684GPIO\uff08\u901a\u7528\u8f93\u5165\u8f93\u51fa\uff09\u5f15\u811a\u80fd\u591f\u76f4\u63a5\u63a7\u5236\u7535\u673a\u7684\u8f6c\u52a8\u3002\u901a\u8fc7Python\u7684RPi.GPIO\u5e93\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u64cd\u4f5c\u8fd9\u4e9b\u5f15\u811a\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5RPi.GPIO\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fddRPi.GPIO\u5e93\u5df2\u5b89\u88c5\u5728\u6811\u8393\u6d3e\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get install python3-rpi.gpio<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u7535\u673a<\/strong><\/p>\n<\/p>\n<p><p>\u8fde\u63a5\u7535\u673a\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u7535\u673a\u9a71\u52a8\u6a21\u5757\uff0c\u4f8b\u5982L298N\u6a21\u5757\u3002\u7535\u673a\u9a71\u52a8\u6a21\u5757\u53ef\u4ee5\u63d0\u4f9b\u7535\u673a\u6240\u9700\u7684\u7535\u6d41\u548c\u7535\u538b\uff0c\u5e76\u4fdd\u62a4GPIO\u5f15\u811a\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5c06\u7535\u673a\u8fde\u63a5\u5230L298N\u6a21\u5757\u7684\u8f93\u51fa\u5f15\u811a\u3002<\/li>\n<li>\u5c06L298N\u6a21\u5757\u7684\u8f93\u5165\u5f15\u811a\u8fde\u63a5\u5230\u6811\u8393\u6d3e\u7684GPIO\u5f15\u811a\u3002<\/li>\n<li>\u5c06\u6a21\u5757\u7684\u7535\u6e90\u5f15\u811a\u8fde\u63a5\u5230\u7535\u6e90\uff08\u59825V\u621612V\u7535\u6e90\uff0c\u4f9d\u636e\u7535\u673a\u89c4\u683c\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Python\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u63a7\u5236\u7535\u673a\u7684\u57fa\u672c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import RPi.GPIO as GPIO<\/p>\n<p>import time<\/p>\n<h2><strong>\u8bbe\u7f6eGPIO\u6a21\u5f0f<\/strong><\/h2>\n<p>GPIO.setmode(GPIO.BCM)<\/p>\n<h2><strong>\u5b9a\u4e49\u63a7\u5236\u7535\u673a\u7684\u5f15\u811a<\/strong><\/h2>\n<p>MotorPin1 = 17<\/p>\n<p>MotorPin2 = 18<\/p>\n<h2><strong>\u8bbe\u7f6e\u5f15\u811a\u4e3a\u8f93\u51fa\u6a21\u5f0f<\/strong><\/h2>\n<p>GPIO.setup(MotorPin1, GPIO.OUT)<\/p>\n<p>GPIO.setup(MotorPin2, GPIO.OUT)<\/p>\n<p>def motor_forward():<\/p>\n<p>    GPIO.output(MotorPin1, GPIO.HIGH)<\/p>\n<p>    GPIO.output(MotorPin2, GPIO.LOW)<\/p>\n<p>def motor_backward():<\/p>\n<p>    GPIO.output(MotorPin1, GPIO.LOW)<\/p>\n<p>    GPIO.output(MotorPin2, GPIO.HIGH)<\/p>\n<p>def motor_stop():<\/p>\n<p>    GPIO.output(MotorPin1, GPIO.LOW)<\/p>\n<p>    GPIO.output(MotorPin2, GPIO.LOW)<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        motor_forward()<\/p>\n<p>        time.sleep(5)<\/p>\n<p>        motor_backward()<\/p>\n<p>        time.sleep(5)<\/p>\n<p>        motor_stop()<\/p>\n<p>        time.sleep(5)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    GPIO.cleanup()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>motor_forward<\/code>\u51fd\u6570\u7528\u4e8e\u63a7\u5236\u7535\u673a\u6b63\u8f6c\uff0c<code>motor_backward<\/code>\u51fd\u6570\u7528\u4e8e\u53cd\u8f6c\uff0c\u800c<code>motor_stop<\/code>\u51fd\u6570\u7528\u4e8e\u505c\u6b62\u7535\u673a\u3002\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u7535\u673a\u5c06\u4e0d\u65ad\u5730\u6b63\u8f6c\u3001\u53cd\u8f6c\u3001\u505c\u6b62\uff0c\u76f4\u5230\u7528\u6237\u4e2d\u65ad\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001PWM\u4fe1\u53f7\u63a7\u5236<\/p>\n<\/p>\n<p><p>PWM\uff08\u8109\u5bbd\u8c03\u5236\uff09\u4fe1\u53f7\u662f\u63a7\u5236\u7535\u673a\u8f6c\u901f\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u901a\u8fc7\u8c03\u8282PWM\u4fe1\u53f7\u7684\u5360\u7a7a\u6bd4\uff0c\u53ef\u4ee5\u63a7\u5236\u7535\u673a\u7684\u901f\u5ea6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6ePWM<\/strong><\/p>\n<\/p>\n<p><p>\u5728RPi.GPIO\u5e93\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>GPIO.PWM<\/code>\u7c7b\u6765\u751f\u6210PWM\u4fe1\u53f7\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import RPi.GPIO as GPIO<\/p>\n<p>import time<\/p>\n<h2><strong>\u8bbe\u7f6eGPIO\u6a21\u5f0f<\/strong><\/h2>\n<p>GPIO.setmode(GPIO.BCM)<\/p>\n<h2><strong>\u5b9a\u4e49\u63a7\u5236\u7535\u673a\u7684\u5f15\u811a<\/strong><\/h2>\n<p>MotorPin = 17<\/p>\n<h2><strong>\u8bbe\u7f6e\u5f15\u811a\u4e3a\u8f93\u51fa\u6a21\u5f0f<\/strong><\/h2>\n<p>GPIO.setup(MotorPin, GPIO.OUT)<\/p>\n<h2><strong>\u8bbe\u7f6ePWM\u9891\u7387\u4e3a1kHz<\/strong><\/h2>\n<p>pwm = GPIO.PWM(MotorPin, 1000)<\/p>\n<h2><strong>\u542f\u52a8PWM\uff0c\u521d\u59cb\u5360\u7a7a\u6bd4\u4e3a0<\/strong><\/h2>\n<p>pwm.start(0)<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        # \u589e\u52a0\u5360\u7a7a\u6bd4\uff0c\u7535\u673a\u52a0\u901f<\/p>\n<p>        for dc in range(0, 101, 5):<\/p>\n<p>            pwm.ChangeDutyCycle(dc)<\/p>\n<p>            time.sleep(0.1)<\/p>\n<p>        # \u51cf\u5c0f\u5360\u7a7a\u6bd4\uff0c\u7535\u673a\u51cf\u901f<\/p>\n<p>        for dc in range(100, -1, -5):<\/p>\n<p>            pwm.ChangeDutyCycle(dc)<\/p>\n<p>            time.sleep(0.1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    pwm.stop()<\/p>\n<p>    GPIO.cleanup()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7<code>pwm.ChangeDutyCycle(dc)<\/code>\u6765\u6539\u53d8\u5360\u7a7a\u6bd4\uff0c\u4ece\u800c\u63a7\u5236\u7535\u673a\u7684\u8f6c\u901f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7Arduino\u63a7\u5236<\/p>\n<\/p>\n<p><p>Python\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4e32\u53e3\u4e0eArduino\u901a\u4fe1\u6765\u63a7\u5236\u7535\u673a\u3002Arduino\u53ef\u4ee5\u901a\u8fc7\u5176\u677f\u8f7d\u7684PWM\u529f\u80fd\u6765\u63a7\u5236\u7535\u673a\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Arduino\u7aef\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u5728Arduino\u7aef\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">const int MotorPin = 9; \/\/ \u7535\u673a\u8fde\u63a5\u5230\u6570\u5b579\u5f15\u811a<\/p>\n<p>void setup() {<\/p>\n<p>    pinMode(MotorPin, OUTPUT);<\/p>\n<p>}<\/p>\n<p>void loop() {<\/p>\n<p>    analogWrite(MotorPin, 255); \/\/ \u6700\u5927\u901f\u5ea6<\/p>\n<p>    delay(5000);<\/p>\n<p>    analogWrite(MotorPin, 0);   \/\/ \u505c\u6b62<\/p>\n<p>    delay(5000);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7535\u673a\u8fde\u63a5\u5230Arduino\u7684\u6570\u5b579\u5f15\u811a\uff0c\u901a\u8fc7<code>analogWrite<\/code>\u51fd\u6570\u63a7\u5236\u7535\u673a\u8f6c\u901f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Python\u7aef\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>Python\u7aef\u53ef\u4ee5\u4f7f\u7528<code>pySerial<\/code>\u5e93\u4e0eArduino\u901a\u4fe1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import serial<\/p>\n<p>import time<\/p>\n<h2><strong>\u6253\u5f00\u4e32\u53e3<\/strong><\/h2>\n<p>ser = serial.Serial(&#39;\/dev\/ttyACM0&#39;, 9600)<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        ser.write(b&#39;1&#39;)  # \u53d1\u9001\u6570\u636e<\/p>\n<p>        time.sleep(5)<\/p>\n<p>        ser.write(b&#39;0&#39;)  # \u53d1\u9001\u6570\u636e<\/p>\n<p>        time.sleep(5)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    ser.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cPython\u901a\u8fc7\u4e32\u53e3\u53d1\u9001\u6570\u636e\u7ed9Arduino\uff0c\u63a7\u5236\u7535\u673a\u7684\u5f00\u542f\u548c\u5173\u95ed\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u901a\u8fc7GPIO\u5e93\u63a7\u5236\u7535\u673a\u662f\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u4e8e\u6811\u8393\u6d3e\u7b49\u5355\u677f\u8ba1\u7b97\u673a\uff1b\u800cPWM\u4fe1\u53f7\u53ef\u4ee5\u6709\u6548\u63a7\u5236\u7535\u673a\u7684\u8f6c\u901f\uff0c\u901a\u8fc7Arduino\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u63a7\u5236\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u786e\u4fdd\u7535\u673a\u9a71\u52a8\u7535\u8def\u6b63\u786e\u8fde\u63a5\uff0c\u907f\u514d\u635f\u574f\u8bbe\u5907\u3002\u7535\u673a\u63a7\u5236\u662f\u7535\u5b50\u5de5\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u5e94\u7528\uff0c\u901a\u8fc7Python\u8fdb\u884c\u63a7\u5236\uff0c\u4e0d\u4ec5\u80fd\u63d0\u5347\u7f16\u7a0b\u6280\u80fd\uff0c\u8fd8\u80fd\u6df1\u5165\u7406\u89e3\u7535\u673a\u7684\u5de5\u4f5c\u539f\u7406\u548c\u63a7\u5236\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63a7\u5236\u7535\u673a\u7684\u8f6c\u52a8\u65b9\u5411\uff1f<\/strong><br \/>\u8981\u63a7\u5236\u7535\u673a\u7684\u8f6c\u52a8\u65b9\u5411\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7535\u673a\u9a71\u52a8\u5668\u7684\u5f15\u811a\u9ad8\u4f4e\u7535\u5e73\u6765\u5b9e\u73b0\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528GPIO\u5e93\uff08\u5982RPi.GPIO\u6216gpiozero\uff09\u53ef\u4ee5\u8f7b\u677e\u63a7\u5236\u6811\u8393\u6d3e\u7b49\u5355\u677f\u8ba1\u7b97\u673a\u4e0a\u7684\u7535\u673a\u3002\u53ea\u9700\u5c06\u4e00\u4e2a\u5f15\u811a\u8bbe\u4e3a\u9ad8\u7535\u5e73\uff0c\u53e6\u4e00\u4e2a\u8bbe\u4e3a\u4f4e\u7535\u5e73\uff0c\u4fbf\u53ef\u4ee5\u5b9e\u73b0\u987a\u65f6\u9488\u6216\u9006\u65f6\u9488\u7684\u8f6c\u52a8\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u7535\u673a\u8f6c\u52a8\u7684\u901f\u5ea6\uff1f<\/strong><br \/>\u7535\u673a\u7684\u8f6c\u901f\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7PWM\uff08\u8109\u5bbd\u8c03\u5236\uff09\u4fe1\u53f7\u6765\u63a7\u5236\u3002\u4f7f\u7528Python\u7684GPIO\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aPWM\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7\u8c03\u6574\u5360\u7a7a\u6bd4\u6765\u6539\u53d8\u7535\u673a\u7684\u901f\u5ea6\u3002\u5360\u7a7a\u6bd4\u8d8a\u9ad8\uff0c\u7535\u673a\u8f6c\u901f\u8d8a\u5feb\uff1b\u53cd\u4e4b\uff0c\u5360\u7a7a\u6bd4\u8d8a\u4f4e\uff0c\u7535\u673a\u8f6c\u901f\u8d8a\u6162\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u7535\u673a\u5728Python\u63a7\u5236\u4e0b\u7684\u5b89\u5168\u4f7f\u7528\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u63a7\u5236\u7535\u673a\u65f6\uff0c\u786e\u4fdd\u7535\u673a\u548c\u7535\u6e90\u7684\u89c4\u683c\u5339\u914d\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u8fc7\u6d41\u4fdd\u62a4\u3001\u6e29\u5ea6\u4f20\u611f\u5668\u7b49\u5b89\u5168\u63aa\u65bd\u6765\u907f\u514d\u7535\u673a\u8fc7\u8f7d\u6216\u8fc7\u70ed\u3002\u6b64\u5916\uff0c\u5728\u7f16\u5199\u63a7\u5236\u4ee3\u7801\u65f6\uff0c\u786e\u4fdd\u5728\u5f02\u5e38\u60c5\u51b5\u4e0b\u80fd\u591f\u53ca\u65f6\u505c\u6b62\u7535\u673a\u8fd0\u8f6c\uff0c\u4ee5\u4fdd\u969c\u8bbe\u5907\u53ca\u7528\u6237\u7684\u5b89\u5168\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63a7\u5236\u7535\u673a\u8f6c\u52a8\uff0c\u53ef\u4ee5\u4f7f\u7528GPIO\u5e93\u3001PWM\u4fe1\u53f7\u63a7\u5236\u3001\u901a\u8fc7Arduino\u6216\u8005\u5176\u4ed6\u5fae\u63a7\u5236\u5668\u8fdb\u884c\u63a7\u5236\u3002 [&hellip;]","protected":false},"author":3,"featured_media":992422,"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\/992412"}],"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=992412"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/992412\/revisions"}],"predecessor-version":[{"id":992423,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/992412\/revisions\/992423"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/992422"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=992412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=992412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=992412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}