Any ways to communicate rx of Leornardo to serial monitor?

Hi, I have recently bought a JY-901 (A accelerometer made based on MPU6050).

I'm using their sample code. There's a problem that by using that, although I can communicate the JY-901 with my UNO board, I can't with my LEONARDO board.

I understood that the leornardo board is having serial.print for USB; so I changed all serial to serial1. I'm not sure should I use serial1/serial in the serial.begin tho, so I just leave both there.

After i did so, all the datas disappear from the serial monitor. Are there anyways I could use the "rx" to communicate with the LEORNARDO board like what they did in the UNO board?

Here's my code:

#include <Wire.h>
#include <JY901.h>

void setup() 
{
  Serial1.begin(115200);
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop() 
{
  //print received data. Data was received in serialEvent;
       
  Serial1.print("Acc:");Serial1.print((float)JY901.stcAcc.a[0]/32768*16);Serial1.print(" ");Serial1.print((float)JY901.stcAcc.a[1]/32768*16);Serial1.print(" ");Serial1.println((float)JY901.stcAcc.a[2]/32768*16);
  
  Serial1.println("");
  delay(500);
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() 
{ 
  while (Serial1.available()) 
  {
    JY901.CopeSerialData(Serial1.read()); //Call JY901 data cope function
  }
}

Thanks so much for helping! I have also attached the JY-901 library but not sure if it's useful.

JY901.cpp (3.21 KB)

JY901.h (3.41 KB)

Are there anyways I could use the "rx" to communicate with the LEORNARDO board like what they did in the UNO board?

Print everything to both interfaces Serial and Serial1.

On a Leonardo messages to Serial go through the regular USB connection and messages to Serial1 go through Pins 0 and 1.

If you want to send messages through pins 0 and 1 to the PC use a USB-TTL cable connected to Rx Tx and GND.

...R

Thank for all your answering!

In a nutshell, Adruino Leornado's rx only talks to its tx but can't sent messages back to the computer thru its built-in USB by software coding?

Also I'm thinking is it possible to use digital pin to communicate with the accelerometer? It's possible for buttons but I have utterly no idea to do for an accelerometer.

Thanks again,
Cher

You should use Serial on the Leonardo to communicate with the PC Serial Monitor.

You would use Serial1 if you had some other serial device, such as a GPS, connected to pins 0 and 1. Do you have anything connected to 0 and 1 on the Leonardo?

Thanks for including the library. I had a look at it and it seems very simple. It is communicating with the JY-901 via I2C (also called Wire in the Arduino world.) That's on pins 9 and 10 of the module. While the module does have serial pins (3 and 4) they aren't used by the library and should not be connected to the Leonardo.

The I2C pins on the Leonardo are labelled SCL and SDA. These are paralled with pins 2 and 3 on the Leonardo. This is UNLIKE most other Arduinos, which parallel A4 and A5 with SCL and SDA.

Thanks for the insights.

I have attached the model of the accelerometer. They only got RXD & TXD ports and I have connected the TXD from the accelerometer to the RXD(0) of the Leonardo board.

It's pretty difficult for me to find solution online cos seems a lot of accelerometer(MPU6050-wise) have a SCL & SDA ports but mine don't. So what I could do is connect the TXD to somewhere. That's why I wonder is there anyway out that I could connect the TXD to another digital pin and get the readings to the serial monitor by software coding.

If not...probably i should buy one accelerometer with wire connections.

OK, that's not the JY-901 that my Google found. That's why we usually insist on you providing the links to the products you have.

Then that library is not going to work. Did you attach the correct library?

IF your module really is Serial only and it's attached to pins 0 and 1 on the Leonardo then you have the perfect setup. You can talk to the module on Serial1 and to the PC on Serial.

Maybe seeing the working UNO code may help determine what is really going on with this module you have.

Sorry I should have done that. The Library actually worked in UNO, it just doesn't work in LEONARDO sadly. Here's the code that has been worked in UNO which is pretty the same with the one I posted.

I didn't attached anything to 1(TX1) of LEONARDO board since I didn't too in UNO board. Should I do so? There's a RXD on the accelerator but I don't see how attaching it with the board's TX will affect the serial monitor? I see there's a comment above telling me to attach the 1(TX1) from LEONARDO to computer's USB? I don't hope to do that since it will need 2 USB attaching to the same PC.

Again, thanks so much for helping!

#include <Wire.h>
#include <JY901.h>
/*
Test on Uno R3.
JY901   UnoR3
TX <---> 0(Rx)
*/
void setup() 
{
  Serial.begin(115200);
}

void loop() 
{
  //print received data. Data was received in serialEvent;
  Serial.print("Time:20");Serial.print(JY901.stcTime.ucYear);Serial.print("-");Serial.print(JY901.stcTime.ucMonth);Serial.print("-");Serial.print(JY901.stcTime.ucDay);
  Serial.print(" ");Serial.print(JY901.stcTime.ucHour);Serial.print(":");Serial.print(JY901.stcTime.ucMinute);Serial.print(":");Serial.println((float)JY901.stcTime.ucSecond+(float)JY901.stcTime.usMiliSecond/1000);
               
  Serial.print("Acc:");Serial.print((float)JY901.stcAcc.a[0]/32768*16);Serial.print(" ");Serial.print((float)JY901.stcAcc.a[1]/32768*16);Serial.print(" ");Serial.println((float)JY901.stcAcc.a[2]/32768*16);
  
  Serial.print("Gyro:");Serial.print((float)JY901.stcGyro.w[0]/32768*2000);Serial.print(" ");Serial.print((float)JY901.stcGyro.w[1]/32768*2000);Serial.print(" ");Serial.println((float)JY901.stcGyro.w[2]/32768*2000);
  
  Serial.print("Angle:");Serial.print((float)JY901.stcAngle.Angle[0]/32768*180);Serial.print(" ");Serial.print((float)JY901.stcAngle.Angle[1]/32768*180);Serial.print(" ");Serial.println((float)JY901.stcAngle.Angle[2]/32768*180);
  
  Serial.print("Mag:");Serial.print(JY901.stcMag.h[0]);Serial.print(" ");Serial.print(JY901.stcMag.h[1]);Serial.print(" ");Serial.println(JY901.stcMag.h[2]);
  
  Serial.print("Pressure:");Serial.print(JY901.stcPress.lPressure);Serial.print(" ");Serial.println((float)JY901.stcPress.lAltitude/100);
  
  Serial.print("DStatus:");Serial.print(JY901.stcDStatus.sDStatus[0]);Serial.print(" ");Serial.print(JY901.stcDStatus.sDStatus[1]);Serial.print(" ");Serial.print(JY901.stcDStatus.sDStatus[2]);Serial.print(" ");Serial.println(JY901.stcDStatus.sDStatus[3]);
  
  Serial.print("Longitude:");Serial.print(JY901.stcLonLat.lLon/10000000);Serial.print("Deg");Serial.print((double)(JY901.stcLonLat.lLon % 10000000)/1e5);Serial.print("m Lattitude:");
  Serial.print(JY901.stcLonLat.lLat/10000000);Serial.print("Deg");Serial.print((double)(JY901.stcLonLat.lLat % 10000000)/1e5);Serial.println("m");
  
  Serial.print("GPSHeight:");Serial.print((float)JY901.stcGPSV.sGPSHeight/10);Serial.print("m GPSYaw:");Serial.print((float)JY901.stcGPSV.sGPSYaw/10);Serial.print("Deg GPSV:");Serial.print((float)JY901.stcGPSV.lGPSVelocity/1000);Serial.println("km/h");
  
  Serial.println("");
  delay(500);
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() 
{
  while (Serial.available()) 
  {
    JY901.CopeSerialData(Serial.read()); //Call JY901 data cope function
  }
}

Cher

z98720922:
In a nutshell, Adruino Leornado's rx only talks to its tx but can't sent messages back to the computer thru its built-in USB by software coding?

You are correct saying that it cannot send data through the built-in USB connector, but what would be the point of the Rx only talking to its Tx? That would be the same as talking to yourself :slight_smile:

The purpose of Rx1 and Tx1 and Serial1 on a Leonardo (or on a Mega) is to allow you to communicate with another serial device that uses TTL signal levels - such as another Arduino.

...R

What you want to see in serial monitor should go through Serial, not Serial1.

So everything in loop() should use Serial.

z98720922:
Here's the code that has been worked in UNO which is pretty the same with the one I posted.

That code is not using the serial pins on your module unless you are using a different library to the one posted at the beginning.

That code will work on the Leo with no changes.

Which UNO pins are connected to the module?