Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I am new to programming and am trying to communicate with my arduino using python through serial communication. I am using the following code:

Arduino code:

int ledPin = 11;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}

void loop() {
  digitalWrite(ledPin,LOW);
    if (Serial.read() == 'M'){
      digitalWrite(ledPin,HIGH);}
}

Python code:

import imaplib
import serial
ser = serial.Serial("COM3",9600)
ser.write(b'T')
ser.close()

I get the following error on running the python script:

serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)

share|improve this question
    
After you figure out why you get that error (based on the answers below) there is another potential problem in your python code - when opening the serial port the Arduino will reset, so anything sent immediately after connection will not get to your sketch. Using a delay after connection will help –  rslite Apr 19 at 3:54

2 Answers 2

There can be several reasons as highlighted in these questions:

They include:

  • port is already in use by another application
  • permissions are set to deny access to normal users
  • problems in the code (top answer in first link)

Try to run with administrator priviledges ("run as administrator"). Others claim that a simple retry might help.

share|improve this answer

More than likely you ran the Arduino program from the Arduino IDE, and left the terminal window open. You must close it before you run the python program, as it will already 'own' the port until it closes. You dont have to quit Arduino IDE, just close the terminal window.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.