Could not open port 'COM1': PermissionError(13, 'Access is denied.', None, 5)

I have an Arduino Uno. I wan to connect it with the laptop via cable to send the data from excel sheet. Excel sheet is having only one column having ten entries of numbers ranging from 1 to 10. I want to send the one digit from the column at a time to Arduino. Then after 5 seconds, the digit in the next cell will be sent to Arduino. Then Arduino will compare this number with the Threshold set in the program. If the digit sent from the excel is higher than the threshold, it will turn on the LED.

For this I have python code script to read data from excel and following IDE code:

Python Script
import serial
import time

Serial port configuration (replace with your Arduino's port)

ser = serial.Serial('COM3', 9600)

Open Excel sheet (replace with your actual file path)

import openpyxl

wb = openpyxl.load_workbook('data.xlsx')
sheet = wb['Sheet1'] # Assuming data is in Sheet1

Threshold value

threshold = 5

Loop through each cell in the first column (A)

for cell in sheet['A']:
# Get the value as an integer
value = int(cell.value)

# Send the value to Arduino
ser.write(str(value).encode())  # Convert to bytes for serial communication

# Delay for 5 seconds
time.sleep(5)

# Check if value is higher than threshold
if value > threshold:
    # Send "1" to turn on LED (replace with appropriate command for your LED control)
    ser.write(b'1')
else:
    # Send "0" to turn off LED (replace with appropriate command for your LED control)
    ser.write(b'0')

# Wait for 1 second before sending the next value
time.sleep(1)

Close serial connection

ser.close()

IDE Code:
#include <SoftwareSerial.h>

// Define RX and TX pins (adjust based on your connection)
#define RX_PIN 0
#define TX_PIN 1

// Define LED pin
#define LED_PIN 13

// Create software serial object
SoftwareSerial serial(RX_PIN, TX_PIN);

// Set the threshold
int threshold = 5;

void setup() {
// Initialize serial communication
serial.begin(9600);

// Set LED pin as output
pinMode(LED_PIN, OUTPUT);
}

void loop() {
// Check if data is available
if (serial.available()) {
// Read the received data
int received_value = serial.read() - '0';

// Compare with the threshold
if (received_value > threshold) {
  // Turn on the LED
  digitalWrite(LED_PIN, HIGH);
} else {
  // Turn off the LED
  digitalWrite(LED_PIN, LOW);
}

}
}

Now when I run python code, I am getting following error:

I have tried following solutions:
1.) Restarting computer
2.) Uninstalling and Installing drivers
3.) checking that only one application is using COM port at a time
4.) Changing COM ports

Also wanted to share that I can upload code to Arduino using IDE successfully. But in the python, the above error persists.

However error still persist. Any help greatly appreciated.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

#include <SoftwareSerial.h>

// Define RX and TX pins (adjust based on your connection)
#define RX_PIN 0
#define TX_PIN 1

Do not use pins 0 and 1 for SoftSerial. They are used by the hardware Serial interface

Please do not post in "Uncategorized"; see the sticky topics in https://forum.arduino.cc/c/using-arduino/uncategorized/184.

Topic has been moved.

Please edit your post and apply code tags to the code as described in How to get the best out of this forum.

Some info:

  1. COM1 is hardly ever the Uno; does it disappear from device manager when you disconnect the board.
  2. Only one application can use a COM port at a time; so if the IDE has the port open (e.g. Serial Monitor), your python can't open it; and vice versa.

Okay thanks. So shall I load upload the code from IDE to Arduino and then close IDE. And then run Python script?

Noted. Thanks will change.

Closing the Serial Monitor (if it's open) should be sufficient.

I tried the same. Its not working. Still error persists.

Hi @aditya231. Please answer the question asked by @sterretje:

I'll provide detailed instructions you can follow to check that:


:exclamation: This procedure is not intended to solve the problem. The purpose is to gather more information.


  1. Open the Windows Device Manager.
  2. Select View > Devices by type from the Device Manager menus.
  3. Open the "View" menu.
  4. If there is a to the left of the "Show hidden devices" menu item, click on "Show hidden devices" to disable it.
  5. Disconnect the USB cable of the Arduino board from your computer.
  6. Take note of the contents of the "Other devices" and "Ports (COM & LPT)" sections of the Device Manager tree.
  7. Connect the Arduino board to your computer with a USB cable.
  8. Select Action > Scan for hardware changes" from the Device Manager menus.

Did you see any new device appear in the Device Manager tree after doing this? If so, please tell us where it is located in the tree and what it is named.

You can repeat steps 5-8 multiple times if you are not sure.

@sterretje . Initially it was COM7. Then I changed it to COM1. And yes even if its COM1, it disappears from device manager when I disconnect the board.

Screenshot before connecting board:

image

Screenshot after connecting the board:

image

Was the problem of the "Could not open port 'COM7': PermissionError(13, 'Access is denied.', None, 5)" error occurring before you did that?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.