Tx and rx blinks constantly when connected to the breadboard

When my nano is connected to the breadboard, the tx and rx lights blinks constantly. Then my computer wouldn't recognize the nano and won't give me a port to upload. I've tried resetting the nano but the ports will only show up for a few seconds, then it would go back what it was before. When not connected to the breadboard, I could upload the code fine. Did I break the Nano?

I don't think so, but assuming you don't have anything else inserted on that breadboard (if so, take everything out), my best guess you have a faulty breadboard, with some rows somehow connected.

Try with a different breadboard or, if you don't have another one, try to open it from below or use a couple of wires and a tester to check the holes are properly connected together in rows.

Post a picture of your hardware setup.

Hi, @unclemooze
Welcome to the forum.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Ok so I think the problem is with the code, I uploaded another code and it was working fine. But as soon as I've uploaded the original code, it would upload once, then the port would be gone and the tx/rx lights would blinks constantly.

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
int motorSwitch = 1;
void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(2,OUTPUT);
 pinMode(3,OUTPUT);
 Serial.begin(9600); // Starts the serial communication
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
}
void loop() {
 // Clears the trigPin
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 // Sets the trigPin on HIGH state for 10 micro seconds
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(15);
 digitalWrite(trigPin, LOW);
 // Reads the echoPin, returns the sound wave travel time in microseconds
 duration = pulseIn(echoPin, HIGH);
 // Calculating the distance
 distance = duration * 0.034 / 2;
 // Prints the distance on the Serial Monitor
 Serial.print("Distance: ");
 Serial.println(distance);
 Serial.println(motorSwitch);
 motorSwitch = 1;
 if (distance <= 20 && distance > 0){
   motorSwitch = 2;
 }
 switch (motorSwitch){
   case 1: 
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    motorSwitch = 3;
    break;
   case 2:
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    motorSwitch = 1;
    break;
   case 3:
    break;
  }
 }

I see your loop() continuously writes to the serial the distance and "motorSwitch" value (it seems basically a "motor status" variable) without any delay, and a 9600 baud I imagine the serial is pretty "busy". As first try comment the Serial prints out and see what happens.
But as I think you don't need thousands of motor adjustment per second, add a delay to the loop or, better, read the distance at a given rate using millis() and let the "motorSwitch" variable do its job.

Apart from the problem you mention, I see another thing about "motorSwitch": if you set it to 1 on every "loop" and to 2 if the distance is less than 20, it will ever be either 1 or 2, but never 3! Set it to 1 during the setup(), then let the loop() manage that state change.

2 Likes

The Tx LED blinks when data is being sent out, the Rx blinks when data is being received. When indoubt you can always load the blink sketch and see if things settle down and the Rx and Tx LEDs stop blinking.

I've changed the code based on what you said, but it would still upload once then it wouldn't upload anymore. So I changed a Nano and it would upload again, so my problem was probably my Nano. However there is another problem, when I connect two motors the motors wouldn't run. If I disconnect one of them, they would run again. Could it be that there isn't enough power? Will it be safe to connect two power sources to the breadboard? Thank you.

That's not normal, but I'd like to understand why (if there's a problem with that specific Nano).
First of all, the code you posted now hasn't the changes I suggested, so please always post the exact code you are referencing to.
I meant something like this (see the changes I made, but be advised I never tested that code):

// defines pins numbers
const byte P_TRIG = 9;
const byte P_ECHO = 10;
const byte P_MOTOR1 = 2;
const byte P_MOTOR2 = 3;

// Motor states
const byte MOTOR_FORWARD = 1;
const byte MOTOR_BACKWARDS = 2;
const byte MOTOR_IDLE = 3;


// defines variables
long duration;
int distance;
byte motorSwitch = MOTOR_FORWARD;
// Ultrasonic timer
unsigned long t0 = 0;

void setup() {
 pinMode(P_TRIG, OUTPUT);
 pinMode(P_ECHO, INPUT);
 pinMode(P_MOTOR1,OUTPUT);
 pinMode(P_MOTOR2,OUTPUT);
 Serial.begin(9600); // Starts the serial communication
 digitalWrite(P_MOTOR1,HIGH);
 digitalWrite(P_MOTOR2,HIGH);
}

void loop() {
  // ---------------------------------------
  // Read obstacle distance (twice per second)
  if (millis() - t0 > 500) { 
    digitalWrite(P_TRIG, LOW);
    delayMicroseconds(5);
    digitalWrite(P_TRIG, HIGH);
    delayMicroseconds(15);
    digitalWrite(P_TRIG, LOW);
    duration = pulseIn(P_ECHO, HIGH);
    distance = duration * 0.034 / 2;
    t0 = millis();
    // Prints the distance on the Serial Monitor
    Serial.print("Distance: ");
    Serial.println(distance);
  }
  
  // ---------------------------------------
  // Motor state change logics
  // If there are no more obstacles nearby, switch to forward
  if (distance > 80 && motorSwitch == MOTOR_BACKWARDS) {
    // Resume forward movement
    motorSwitch = MOTOR_FORWARD;
  }
  if (distance <= 20 && distance > 0){
    motorSwitch = MOTOR_BACKWARDS; // Go back!
  }

  // ---------------------------------------
  // Activate the motors based on the state
  switch (motorSwitch){
    case MOTOR_FORWARD: 
      digitalWrite(P_MOTOR1,HIGH);
      digitalWrite(P_MOTOR2,HIGH);
      motorSwitch = 3;
      break;
    case MOTOR_BACKWARDS:
      digitalWrite(P_MOTOR1,LOW);
      digitalWrite(P_MOTOR2,LOW);
      motorSwitch = 3;
      break;
    case MOTOR_IDLE: // Keep moving, do nothing!
      break;
  }
}

But if this doesn't work, why don't you start back from scratch? First, make sure you have nothing connected to arduino when uploading. Second, if the previous test fails, get totally back: write a simple onbloard led (pin 13) blink sketch, with nothing connected to and see if you can upload it more than once?
If everything fails, try doing the same exact steps with the second Nano you have.

That depends on your connections, i.e. how you power the motors and from what, but almost everytime the answer is yes, sounds like a power issue (I was thinking now that might even be the cause of the above upload problems, if you don't unplug the motors when uploading!).

Post a diagram of your connections. If you can't use a circuit design software or site like "WokWi" or "TinkerCad circuits", make a diagram on paper and send here the picture).

1 Like

Since I'm a new user, I cannot upload files but here is a link to the design. (Design)
I've added another power source and now it is working, and your code worked beautifully. Thank you very much. :slight_smile:

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