You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法向Arduino串口写入1或0:Python控制Uno板载LED求助

Troubleshooting Python Control of Arduino Onboard LED

Hey there! As someone who fumbled through this exact setup when I was starting out with Python 3 and Arduino Uno, let’s tackle the most common issues that pop up when trying to control the onboard LED via the command line. I’ll walk you through fixes for the usual culprits:

Permission Denied Errors (Linux/macOS)

If you’re seeing something like PermissionError: [Errno 13] Permission denied: '/dev/ttyACM0', this is a classic serial port permissions issue.

  • On Linux, add your user to the dialout group with this command:
    sudo usermod -a -G dialout $USER
    
    Log out and back in (or restart your terminal) for the changes to take effect.
  • On macOS, add your user to the staff group, or temporarily run your script with sudo (though the group fix is better for long-term use).

Incorrect Serial Port

If Python throws SerialException: could not open port [port]: No such file or directory, you’re using the wrong port path:

  • Open the Arduino IDE, go to Tools > Port to find the correct port for your Uno.
    • Windows will show something like COM3
    • Linux uses /dev/ttyACM0 (or similar)
    • macOS uses /dev/tty.usbmodemXXXX
  • Update your Python code to use this exact port string.

Arduino Code Issues

If you’re sending commands but the LED doesn’t react, double-check your Arduino sketch. Here’s a reliable base sketch to use:

const int ledPin = 13;
String input;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  while (!Serial); // Wait for serial connection (critical for some boards like Leonardo)
}

void loop() {
  if (Serial.available() > 0) {
    input = Serial.readStringUntil('\n');
    input.trim(); // Remove extra whitespace/newlines
    
    if (input == "ON") {
      digitalWrite(ledPin, HIGH);
    } else if (input == "OFF") {
      digitalWrite(ledPin, LOW);
    }
  }
}
  • Upload this sketch to your Uno, and test it first with the Arduino Serial Monitor (set to 9600 baud) to make sure sending "ON"/"OFF" toggles the LED.

Mismatched Baud Rate

If your Python code sends commands but the Arduino doesn’t respond (or gets garbage data), your baud rates don’t match:

  • Make sure your Python code uses the same baud rate as Serial.begin(9600) in the Arduino sketch. Here’s a working Python example with pyserial:
import serial
import time

# Replace with your correct port
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
time.sleep(2) # Give Arduino time to initialize after connection

print("Enter 'ON' to turn LED on, 'OFF' to turn off, or 'quit' to exit:")
while True:
    cmd = input("> ")
    if cmd.lower() == "quit":
        break
    # Send command with newline (matches Arduino's readStringUntil('\n'))
    ser.write((cmd + '\n').encode())

ser.close()

Missing pyserial Library

If you see ModuleNotFoundError: No module named 'serial', you haven’t installed the required library. Install it with:

pip install pyserial

(Use pip3 instead if you’re running multiple Python versions.)

内容的提问来源于stack exchange,提问作者Narayana Bhosle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:00:25