5

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)

jfpoilpret
9,1627 gold badges38 silver badges54 bronze badges
asked Apr 18, 2015 at 11:17
1
  • 1
    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 Commented Apr 19, 2015 at 3:54

5 Answers 5

9

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.

answered Apr 18, 2015 at 17:28
1
  • Not sure what 'terminal window' user6569 meant, but closing the Serial Monitor window from my Arduino IDE did help. Commented Jan 27, 2020 at 18:33
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.

answered Apr 18, 2015 at 13:33
2

you must use this code

>>> import serial
>>> ser.close() # close port
sa_leinad
3,2182 gold badges23 silver badges51 bronze badges
answered Nov 8, 2020 at 9:24
1
  • Please add detail on how this solves the problem the OP is asking. Commented Nov 9, 2020 at 12:48
1

Either run your python program in "administrator mode" or close any other programs using the port intended for

answered Mar 27, 2018 at 21:54
1

Are you by any chance using Jupyter Notebok? I was having a similar error today and managed to solve it by opening Jupyter Notebook through Anaconda Navigator instead of opening it through Anaconda Prompt.

answered Aug 2, 2019 at 20:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.