--SOLVED-- RX and TX on Micro

I have a program (posted below) that uses a Bluetooth HC-05 dongle and connects to my S8+ and communicates through an app created in Thunkable (very similar to MIT App Inventor). The intent of the program is that when a "button" in the app is pressed and held, the dongle tells the arduino to turn pin 11 HIGH -- on release the pin goes LOW.

The program works wonderfully on an UNO and a MEGA. When I try to use it on a micro, it doesn't work. I've verified the program on 2 different UNOs and 3 different MEGAs because....i dunno....just wanted to make sure. I know the app works and the connection is successful but i don't understand why it won't work on the 2 separate MICROs I've tried.

  • Verified that both MICROs work by uploading simpler programs that didn't require RX/TX.

The RX/TX pins on the micro seem pretty straight forward to me. They are, i think, the 3rd and 4th pins from the end of the MICRO next to the reset button. Is there a different way i need to hook them up when using the MICRO with the HC-05?

int lamp = 11;
int vent = 12;
int light = 8;
int pc = 7;

int Received = 0;
int light_state = 0;
int vent_state = 0;
int pc_state = 0;

void setup() {
  Serial.begin(9600);
  pinMode(lamp, OUTPUT);
  pinMode(vent, OUTPUT);
  pinMode(light, OUTPUT);
  pinMode(pc, OUTPUT);
}

void loop() {
  if (Serial.available() > 0)
  {
    Received = Serial.read();
    char Rec = char(Received);
    if (Rec != '0')
    {
      Serial.println(Rec); //This is to visualise the received character
    }
  }
  ////////////////LIGHT/////////////////////
  if (light_state == 0 && Received == '1')
  {
    digitalWrite(light, HIGH);
    light_state = 1;
    Received = 0;
  }
  if (light_state == 1 && Received == '1')
  {
    digitalWrite(light, LOW);
    light_state = 0;
    Received = 0;
  }

  ////////////////VENT/////////////////////
  if (vent_state == 0 && Received == 'a')
  {
    digitalWrite(vent, HIGH);
    vent_state = 1;
    Received = 0;
  }
  if (vent_state == 1 && Received == 'a')
  {
    digitalWrite(vent, LOW);
    vent_state = 0;
    Received = 0;
  }

  ////////////////PC/////////////////////
  if (pc_state == 0 && Received == '2')
  {
    digitalWrite(pc, HIGH);
    pc_state = 1;
    Received = 0;
  }
  if (pc_state == 1 && Received == '2')
  {
    digitalWrite(pc, LOW);
    pc_state = 0;
    Received = 0;
  }

  ////////////////LAMP/////////////////////
  if (Received == '8') {
    digitalWrite(lamp, HIGH);
  }
  if (Received == '9') {
    digitalWrite(lamp, LOW);
  }
}

And you tried swapping them over just in case ?

Yes, no success.

For my current project, i have enough room to use an UNO but i'd like to have the possibility of using the smaller form factor of the MICRO for future projects.

Is there something that i have to enable or turn on for the MICRO?

After doing some further searching, i found this case here suggesting that I change the "Serial" to "Serial1" in the code. I tried it and it still doesn't work. What else am I missing?

https://forums.adafruit.com/viewtopic.php?f=25&t=35649

On a micro, if you use Serial, you need to wait for the connection to be established.

From the man page:

void setup() {
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
}

@arduin_ologist
You might want to wait but you don't need to wait.

@Vampyrewolf
Please post your complete updated code after the change to Serial1.
Did you try the swap of the pins again after that change?

sterretje:
@arduin_ologist
You might want to wait but you don’t need to wait.

Welllllll… if you want it work, you need to wait; and wanting it to work is implicit in the inclusion of Serial.begin().

arduin_ologist:
On a micro, if you use Serial, you need to wait

You might have a stand-alone setup where you only connect a PC when you want to change e.g. a configuration.

With while(!Serial) in setup, you're loop will never be executed after a reset if there is no initial connection to the PC.

sterretje:
You might have a stand-alone setup where you only connect a PC when you want to change e.g. a configuration.

With while(!Serial) in setup, you're loop will never be executed after a reset if there is no initial connection to the PC.

That probably makes sense to you.

Does your board come in 3v and 5v variants? If so the 3v is usually clocked at 8Mz and the the 5v at 16Mz and either board can be selected in the IDE. If the code is uploaded at the wrong setting the baud rate will be incorrect and serial won't communicate properly. (The UNO and mega are normally only run at 16Mz)
Just a thought

Vampyrewolf:
After doing some further searching, i found this case here suggesting that I change the "Serial" to "Serial1" in the code. I tried it and it still doesn't work. What else am I missing?

Yes, the arduino micro has a built-in usb controller, which is mapped to Serial. To use the RX/TX (USART) you need to use Serial1. Did you change all your calls from Serial to Serial1? Can you post the code?

arduin_ologist:
Welllllll... if you want it work, you need to wait; and wanting it to work is implicit in the inclusion of Serial.begin().

Yes, if you are using USB CDC Serial. But since he wants to use the hardware USART (RX, TX), which is Serial1, waiting for the Serial is not necessary as it will just return true.

LightuC:
Yes, the arduino micro has a built-in usb controller, which is mapped to Serial. To use the RX/TX (USART) you need to use Serial1. Did you change all your calls from Serial to Serial1? Can you post the code?

Yes, if you are using USB CDC Serial. But since he wants to use the hardware USART (RX, TX), which is Serial1, waiting for the Serial is not necessary as it will just return true.

Yes, I added a '1' to any serial i found. Should one of the serials have been omitted from this?

int lamp = 11;
int vent = 12;
int light = 8;
int pc = 7;

int Received = 0;
int light_state = 0;
int vent_state = 0;
int pc_state = 0;

void setup() {
  Serial1.begin(9600);
  pinMode(lamp, OUTPUT);
  pinMode(vent, OUTPUT);
  pinMode(light, OUTPUT);
  pinMode(pc, OUTPUT);
}

void loop() {
  if (Serial1.available() > 0)
  {
    Received = Serial.read();
    char Rec = char(Received);
    if (Rec != '0')
    {
      Serial1.println(Rec); //This is to visualise the received character
    }
  }
  ////////////////LIGHT/////////////////////
  if (light_state == 0 && Received == '1')
  {
    digitalWrite(light, HIGH);
    light_state = 1;
    Received = 0;
  }
  if (light_state == 1 && Received == '1')
  {
    digitalWrite(light, LOW);
    light_state = 0;
    Received = 0;
  }

  ////////////////VENT/////////////////////
  if (vent_state == 0 && Received == 'a')
  {
    digitalWrite(vent, HIGH);
    vent_state = 1;
    Received = 0;
  }
  if (vent_state == 1 && Received == 'a')
  {
    digitalWrite(vent, LOW);
    vent_state = 0;
    Received = 0;
  }

  ////////////////PC/////////////////////
  if (pc_state == 0 && Received == '2')
  {
    digitalWrite(pc, HIGH);
    pc_state = 1;
    Received = 0;
  }
  if (pc_state == 1 && Received == '2')
  {
    digitalWrite(pc, LOW);
    pc_state = 0;
    Received = 0;
  }

  ////////////////LAMP/////////////////////
  if (Received == '8') {
    digitalWrite(lamp, HIGH);
  }
  if (Received == '9') {
    digitalWrite(lamp, LOW);
  }
}

Just looking you've missed one.

Received = Serial1.read();

tasmod:
Just looking you've missed one.

Received = Serial1.read();

Yup, that fixed it! Works like a dream.
Final code.

int lamp = 11;
int vent = 12;
int light = 8;
int pc = 7;

int Received = 0;
int light_state = 0;
int vent_state = 0;
int pc_state = 0;

void setup() {
  Serial1.begin(9600);
  pinMode(lamp, OUTPUT);
  pinMode(vent, OUTPUT);
  pinMode(light, OUTPUT);
  pinMode(pc, OUTPUT);
}

void loop() {
  if (Serial1.available() > 0)
  {
    Received = Serial1.read();
    char Rec = char(Received);
    if (Rec != '0')
    {
      Serial1.println(Rec); //This is to visualise the received character
    }
  }
  ////////////////LIGHT/////////////////////
  if (light_state == 0 && Received == '1')
  {
    digitalWrite(light, HIGH);
    light_state = 1;
    Received = 0;
  }
  if (light_state == 1 && Received == '1')
  {
    digitalWrite(light, LOW);
    light_state = 0;
    Received = 0;
  }

  ////////////////VENT/////////////////////
  if (vent_state == 0 && Received == 'a')
  {
    digitalWrite(vent, HIGH);
    vent_state = 1;
    Received = 0;
  }
  if (vent_state == 1 && Received == 'a')
  {
    digitalWrite(vent, LOW);
    vent_state = 0;
    Received = 0;
  }

  ////////////////PC/////////////////////
  if (pc_state == 0 && Received == '2')
  {
    digitalWrite(pc, HIGH);
    pc_state = 1;
    Received = 0;
  }
  if (pc_state == 1 && Received == '2')
  {
    digitalWrite(pc, LOW);
    pc_state = 0;
    Received = 0;
  }

  ////////////////LAMP/////////////////////
  if (Received == '8') {
    digitalWrite(lamp, HIGH);
  }
  if (Received == '9') {
    digitalWrite(lamp, LOW);
  }
}

Vampyrewolf:
Yup, that fixed it! Works like a dream.
Final code.

Good to know that it worked for you. Btw you should use Arduino Mini or Pro Mini, they follow Arduino UNO and are quite small in size. Arduino Micro follows Arduino Leonardo and it has some problems dealing with Serial Port.

It does not have problems. One must just understand the possible pitfalls which are slightly different from the pitfalls that can be experienced with 328P based boards.

I'm definitely curious to explore more of these boards and what their respective pros/cons are.

I received a MKR1000 for my birthday and I'm already drowning in what I (think) i could do with it.

Thanks again for all your help guys/gals.

Not sure how to add "SOLVED" to the posting name but it can definitely be changed.

Vampyrewolf:
Not sure how to add "SOLVED" to the posting name but it can definitely be changed.

Edit your opening post using more -> modify; there you can modify the title of your thread and add "SOLVED" to it.