Serial communication stops

Hello guys, need help with Leonardo's serial communication. I made a windows forms GUI application to read Arduino's serial data of 3 load cell sensors.

Everything works fine when using atmega328 based Arduinos, the GUI reads serial data without any problems, but when I use Leonardo or Pro micro (which are based on atmega32u4 microprocessors) I get some problems.

So, using nano with my GUI application works fine, my app opens appropriate COM PORT, reads and displays data with no problems. However, when I switched to Leonardo, the GUI app was not able to read the serial data. when I open Leonardo's port in my app, nothing happens, no error messages, no data, nothing. Leonardo's Rx and Tx LEDs blink only just for ONCE. Tx LED blinks continuously only when I open serial monitor, or TeraTerm, or any other software for reading the serial data.

When I use Arduino nano for example, Tx LED blinks all the time, does not matter if the serial monitor is open or not. Which means that the nano is transmitting data continuously ALWAYS.

Based on this info, question is next: does Leonardo need a different way of opening port and communicating?

To get an idea, my app is based on this video: https://www.youtube.com/watch?v=JsPpy6lVjIo

Sorry if some things are unclear, feel free to ask me any question about the project.

P.s I cannot use nano or uno for this project since they aren't capable of acting as HID devices. I have to use Leonardo or Pro micro, since my project is simply a 3 axis joystick.

here is my Arduino code

#include <HX711.h>
#include <Joystick.h>
#define calibrationfactor 28000 // Change this value to give accurate reading with your known mass.
const int LOADCELL1_DOUT_PIN = 2;
const int LOADCELL1_SCK_PIN = 3;

const int LOADCELL2_DOUT_PIN = 4;
const int LOADCELL2_SCK_PIN = 5;

const int LOADCELL3_DOUT_PIN = 6;
const int LOADCELL3_SCK_PIN = 7;
HX711 scale1;
HX711 scale2;
HX711 scale3;


  Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK,0,0,true,true,true,false,false,false,false,false,false,false,false);
 


int brake = 0;
int lastbrakeValue = 0;
int throtle = 0;
int lastthrotleValue = 0;
int clutch = 0;
int lastclutchValue = 0;

void setup() {
  // Ranges are 1023 by default
   Joystick.setBrakeRange(0, 1023);
   Joystick.setXAxisRange(0, 1023);
   Joystick.setYAxisRange(0, 1023);


 
  Joystick.begin();
  Serial.begin(9600);
 
  scale1.begin(LOADCELL1_DOUT_PIN, LOADCELL1_SCK_PIN);
  scale1.set_scale(calibrationfactor);
  scale1.tare();
  scale2.begin(LOADCELL2_DOUT_PIN, LOADCELL2_SCK_PIN);
  scale2.set_scale(calibrationfactor);
  scale2.tare();
  scale3.begin(LOADCELL3_DOUT_PIN, LOADCELL3_SCK_PIN);
  scale3.set_scale(calibrationfactor);
  scale3.tare();
}
void loop() {

//Displays reading in Serial Monitor

    
   brake = scale1.get_units(); 

   if(brake > 1023) brake = 1023;
   if (brake < 0 or brake < 1) {
    brake = 0;
  }
 
   if (lastbrakeValue != brake) {
     Joystick.setXAxis(brake);
     lastbrakeValue = brake;
  }
   
   
   
   clutch = scale2.get_units(); 
   if(clutch > 1023) clutch = 1023;
   if (clutch < 0 or clutch < 1) {
    clutch = 0;
  }
 
   if (lastclutchValue != clutch) {
     Joystick.setYAxis(clutch);
     lastclutchValue = clutch;
  }

   
   throtle = scale3.get_units(); 
   if(throtle > 1023) throtle = 1023;
   if (throtle < 0 or throtle < 1) {
    throtle = 0;
  }
 
   if (lastthrotleValue != throtle) {
     Joystick.setZAxis(throtle);
     lastthrotleValue = throtle;
  }
  
  Serial.print((String) throtle + "A" + brake + "B" + clutch + "C" + "\n");
  delay(20);
}

A leonardo has native USB for Serial Monitor
a hardware serial port is Serial1 pin 1 is Tx and pin 0 is Rx

the Arduino micro
Serial is USB Serial monitor
Serial1 is UART on pins 0 Rx and 1 Tx

simple test program for Leonardo (program should also work on Pro Mico)

// Arduino Leonardo Serial1 test

// Serial leonardo has native USB (uart0)
// Serial1 is hardware serial port pin 1 is Tx and pin 0 is Rx

// for loopback test connect pin 0 to pin 1

// for RS232 shield connect pin 1 to Tx and pin 0 to Rx
// for loopback test connect 9 pin D connector pins 2 and 3

unsigned long time;

void setup() {
  Serial.begin(115200);   // initialise serial monitor port
  Serial1.begin(115200);  // initialise Serial1
  Serial.write("Arduino Leonardo Serial1 test -  for loopback test connect pin 1 to pin 0\n");
}

void loop() {
  if (Serial1.available()) {      // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  }
  if (Serial.available()) {       // read from Serial outut to Serial1
    char inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

connect pin 1 to pin 0 to form a loopback test - text entered on keyboard is echoed back to display

Thanks for your reply, I have uploaded your code to my Leonardo, when I enter some text Rx LED blinks but no Tx, nothing is echoed back to serial monitor

Is there any way to force Leonardo to transmit serial data continuously at all times? Like any other arduino.

did you connect pin 0 to pin1? just tested on my Leonardo and loopback works OK

Serial to serial monitor
Serial1 on pin 1 Tx and pin 0 Rx to an external device

Oh I missed that, sorry. now it works. but how is it any good for the project?

the way that Serial behaves on reset is different on the UNO and Leonardo
have a read of https://docs.arduino.cc/retired/getting-started-guides/ArduinoLeonardoMicro/
what language is the GUI written in?

Edit: when you connect the Leonardo to your GUI try pressing the RESET button

The GUI is written in visual C#. Using visual studio Windows Forms Application. here is the video link of the tutorial I am using for the GUI: https://www.youtube.com/watch?v=JsPpy6lVjIo

The reset button does not help. I've read the article you've provided, now it is clear that the Leonardo board transmits information only after the serial port is opened (in contrast of uno and other boards).

The problem still remains. I am sure that my GUI opens ports correctly because it works fine with other arduinos.

how does your Leonardo program behave on reset?
e.g. does it assume the GUI is ready to accept information and it starts transmitting?

I didn't really understand the question. The program just restarts after resetting Arduino, you can see the code. Leonardo never starts transmitting by itself, it only transmits after I open port (start serial reader). If it were Uno, it would start transmitting right after resetting using this code.

the Leinardo takes a few seconds after loading the code or pressing the reset button to open the serial port, try

void setup() {
   Serial.begin(9600);
   // while the serial stream is not open, do nothing:
   while (!Serial) ;

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