COM disconnects then reconnects. Rinse and repeat

About me and the hardware/software I use:
I am brand new to Arduino. I am taking a college course to get me oriented. I use a custom-built PC that runs on Windows 10. I am using the 1.8.14 version of the code editor to work with an Elegoo Mega 2560. I connect my microcontroller to my PC via a USB port on my desktop tower whenever I am uploading or running code.

About the situation:
When I try to run the code given below it successfully uploads and runs, but I cannot use the Serial Monitor. If I click to open the serial monitor within the first few moments of uploading the code it brings the window up, but I cannot interact with it and I get the error message, “Error while setting serial port parameters: 9,600 N 8 1”. If I wait a few seconds after the code is uploaded/running and then try to open the serial monitor it won’t pull the window up and gives me the error message, “Error opening serial port COM7 (port not found)”.

The other thing is that while the code is running my operating system goes back and forth between recognizing my Arduino Mega and not recognizing it. I will hear the little chime sound that signifies something has been plugged in/out over and over again even though the USB connection is physically secure. When I open my device manager I can watch the Arduino bounce back and forth between being recognized as being part of COM7 and being an unrecognized device.

All of this only happens with this specific sketch and wiring. Every other sketch that I have written and run hasn’t had this problem. I can even go back and upload a previous code that I’ve written that uses the serial monitor and it works just fine!

I am genuinely stumped as to what could be going on. Part of me wonders if it has to do with the physical setup. that I have attached to the Arduino? IDK if I’m pumping too much current/voltage through it and that’s what’s causing it or what. Just in case I included a (very crude) circuit diagram of how things are wired up as an attachment to this post.

int motor_pin = 9;
static int motor_speed = 125;

void setup() {
  pinMode(motor_pin, OUTPUT);
  Serial.println("Read in from serial monitor and change motor speed ...");
  Serial.println("Enter 'U' to increase motor speed by 10");
  Serial.println("Enter 'u' to increase motor speed by 1");
  Serial.println("Enter 'D' to decrease motor speed by 10");
  Serial.println("Enter 'd' to decrease motor speed by 1");

void loop() {
  char input =;
  if (input != -1) {
    //look at input and change times if appropriate
    if (input == 'U') motor_speed = motor_speed + 10;
    if (input == 'u') motor_speed = motor_speed + 1;
    if (input == 'D') motor_speed = motor_speed - 10;
    if (input == 'd') motor_speed = motor_speed + 1;
    // Display current speed
    Serial.print("Current motor speed: ");

CCF02182021.pdf (163 KB)

It's advisable to attach an image instead of a PDF; that will (hopefully) end up automatically in your post.

I'm not sure about your problem but some points.

You seem to be missing a common ground between the GND (-) of the battery and the GND of the Arduino.
You don't have a flyback diode over the motor (see e.g.
Is the Arduino also powered from the 6V battery? It actually can't so I doubt that you did that.

OP's pdf content

Please see THIS TOPIC

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