Can't read serial data

Hi there.

Arduino Starter here.
I’m trying to connect a JY-901 gyro+acc+mag sensor (MPU6050 + bluetooth) to a Pro Micro (5V). I’m not interested in using the bluetooth I just want to read serial data from it into serial monitor.

Setup:
Windows drivers SparkFun Pro Micro
Sensor Arduino
GND GND
VCC VCC
TXD RX1

Serial.available() returns 0;

Here is the sketch that comes with the sensor and I modified it to work with Pro Micro:

#include <Wire.h>
#include <JY901.h>
/*
Test on Uno R3.
JY901   UnoR3
TX <---> 0(Rx)
*/

int bytesRead = 0;
  
void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  bytesRead = Serial.available();
  if(bytesRead > 0)
  {
    JY901.CopeSerialData(Serial.read()); //Call JY901 data cope function
  }
  
   Serial.println(bytesRead);
  
  //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() // not executed for Leo/Micro
{
  while (Serial.available()) 
  {
    JY901.CopeSerialData(Serial.read()); //Call JY901 data cope function
  }
}

Why are you trying to read from Serial?

Mark

Thanks for replying.
I probably did it the wrong way, didn’t I? I’m a total beginner - I know I could mess it all up due to causes you wouldn’t believe :slight_smile:
I’m not sure I understand your question. Is there a better way?
I know there are 2 ways to read data from the sensor: serial and I2C. I have both sketches but I went with serial cause it seemed the simpler way.
And I’m not confident I can use SDA and SCL pins. Judging from the sensor I’d say they didn’t consider connecting to those pins directly. Please take a look at the photo:

For what it’s worth, on the other side there’s the bluetooth.

On a ProMicro the pins 0 and 1 are Serial1 and Serial is what communicates with the PC over the USB connection.

...R

I modified the sketch according to use Serial1 but it still doesn’t work. I made a progress tho: I could get something out of that sensor with Serial1.read(), which printed as garbage. I kind of expected this, as data has to be “decoded” by JY901.CopeSerialData() but this function doesn’t seem to output anything at all.
If anybody wants to take a look:

#include <Wire.h>
#include <JY901.h>
/*
Test on Uno R3.
JY901   UnoR3
TX <---> 0(Rx)
*/

int bytesRead1 = 0, bytesRead = 0;
char data[64] = {'\0'};

void setup() 
{
  Serial1.begin(9600);
  Serial.begin(9600);
}

void loop() 
{
  bytesRead1 = Serial1.available();
  bytesRead = Serial.available();
  
  if(bytesRead1 > 0)
  {
    Serial.println(bytesRead1);

    for(byte i = 0; i < 64; i++)
    {
      data[i] = Serial1.read();
      Serial.print(data[i]);
    }
   
    JY901.CopeSerialData(data); //Call JY901 data cope function
  } 

  
  //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(1000);
}

/*
  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() // not executed for Leo/Micro
{
  while (Serial.available()) 
  {
    JY901.CopeSerialData(Serial.read()); //Call JY901 data cope function
  }
}

Here’s the said function in JY901.cpp:

void CJY901 ::CopeSerialData(unsigned char ucData)
{
 static unsigned char ucRxBuffer[250];
 static unsigned char ucRxCnt = 0; 
 
 ucRxBuffer[ucRxCnt++]=ucData;
 if (ucRxBuffer[0]!=0x55) 
 {
 ucRxCnt=0;
 return;
 }
 if (ucRxCnt<11) {return;}
 else
 {
 switch(ucRxBuffer[1])
 {
 case 0x50: memcpy(&stcTime,&ucRxBuffer[2],8);break;
 case 0x51: memcpy(&stcAcc,&ucRxBuffer[2],8);break;
 case 0x52: memcpy(&stcGyro,&ucRxBuffer[2],8);break;
 case 0x53: memcpy(&stcAngle,&ucRxBuffer[2],8);break;
 case 0x54: memcpy(&stcMag,&ucRxBuffer[2],8);break;
 case 0x55: memcpy(&stcDStatus,&ucRxBuffer[2],8);break;
 case 0x56: memcpy(&stcPress,&ucRxBuffer[2],8);break;
 case 0x57: memcpy(&stcLonLat,&ucRxBuffer[2],8);break;
 case 0x58: memcpy(&stcGPSV,&ucRxBuffer[2],8);break;
 }
 ucRxCnt=0;
 }
}

Thank you guys for help.

PS:
Does anything have to do with using a Pro Micro instead of Uno?..

    Serial.println(bytesRead1);

    for(byte i = 0; i < 64; i++)
    {
      data[i] = Serial1.read();
      Serial.print(data[i]);
    }

If there is one byte available to read, read all 64 of them. That hasn’t worked for anyone else, and won’t work for you.

What, EXACTLY, is connected to the Micro, and where?

Thank you for willing to help.

I have this IMU JY901 that looks like this: |500x387 based on MPU6050 with 3 axes gyroscope, 3 axes accelerometer, 3 axes magnetometer with integrated Kalman filter. It also has a bluetooth glued to one side: |500x331 On the other side it has the MPU6050 glued, as shown in the picture a few posts above.

Connections: JY901 Pro Micro GND GND VCC VCC TXD RX1

So as it looks from the sketches I use Serial1 for module-ProMicro and Serial to display to monitor.

Can I provide more info?