0

I recently bought an ATmega32U4 based Arduino Pro micro. I am using the Keyboard library and interfacing with my PC via USB.

I am trying to create a serial communication between the Arduino Pro micro and a NodeMCU via Rx, Tx pins (0, 1), but it isn't working i.e. not giving any output with the code below on the Pro micro:

#include <Keyboard.h>

int incomingByte;
unsigned long lastTime;
    
void setup() {
  Serial.begin(115200);
  Keyboard.begin();
}
    
void loop() {
  if (Serial.available() > 0) {
    lastTime = millis();
    String payload = "";
    
    while (1) {
      if (millis() - lastTime > 1000)
        break;
    
      incomingByte = Serial.read();
      Serial.println(incomingByte);
    }
  }

On the NodeMCU there is a simple script flashed which only writes on serial some characters in a delayed loop.

When using the SoftwareSerial library with the Pro micro, it is working fine i.e. I can read data sent by the NodeMCU on custom Rx, Tx pins (9, 10). I tried this because I thought since Pro micro is communicating with a PC, the original Rx and Tx pins are busy. But after some research I got to know that unlike other Arduino boards like the UNO, the Pro micro doesn't use those for communication over USB.

Although custom serial using SoftwareSerial library is working, it is too slow and unreliable i.e. it takes 2-3 seconds to pass a string of length 10. Also its unreliable because on sending bigger length strings, the strings arrive incomplete or sometimes the data gets corrupted (characters shuffle).

Again this is not my main concern, if I can use original Rx, Tx pins as usual on any other Arduino boards, it solves my issue.

This is my circuit diagram:

Circuit

6
  • 1
    The problem is your janky serial reading method. You're assuming there's data to read when there isn't. Read this: majenko.co.uk/blog/reading-serial-arduino
    – Majenko
    Jan 12, 2021 at 15:00
  • I probably missed something but: why are you using SoftwareSerial on the pro micro at all?
    – timemage
    Jan 12, 2021 at 15:29
  • @timemage Yes, you missed that. He explained why - he assumed pins 0/1 were taken with the USB, then found out otherwise, and now is attempting to use those with Serial instead of SoftwareSerial.
    – Majenko
    Jan 12, 2021 at 15:34
  • The Serial device is usually the USB serial connection, whether or not that is controlled by pins 0/1. For pins 0/1 with a native USB connection you need to use Serial1. I think. It's been a while since I used an Arduino with native USB...
    – Majenko
    Jan 12, 2021 at 15:35
  • I'm not confused about how Serial and Serial1 work on the 32u4 based Arduinos. I don't see any indication that anything other than SoftwareSerial is being used.
    – timemage
    Jan 12, 2021 at 15:37

1 Answer 1

0

RX and TX pin of Arduino Micro are Serial1. Replace Serial in your sketch with Serial1 to communicate with esp8266 over RX and TX pin.

1
  • Luckily I found out that last night and that worked too. Thanks for your inputs @Juraj. The Rx and Tx (0 and 1) pins on Arduino Pro Micro are defined with Serial1 so replacing Serial with Serial1 worked out for me. Thanks to all for your interest.
    – Rishabh
    Jan 13, 2021 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.