1

So Im using this sensor: https://wiki.dfrobot.com/Serial_6_Axis_Accelerometer_SKU_SEN0386 with this library https://github.com/DFRobotdl/DFRobot_WT61PC

Im using 2 sensors but got poor frequency on Arduino Uno and decicded to switch to Mega since it supports multiple communications at once.

This code works perfectly fine on Uno, but doesnt on Mega. Of course I tried using this method: https://docs.arduino.cc/built-in-examples/communication/MultiSerialMega and I know I shouldnt be using Software Serial on Mega but this library has been written using Software Serial and I cant use a different method (or at least thats how I understand it).

Here is the reason.

SoftwareSerial mySerial(10, 11);
DFRobot_WT61PC sensor(&mySerial);

So I tried multiple variations of pins (ones supporting UART, pcint, TX/RX) https://docs.arduino.cc/learn/built-in-libraries/software-serial and nothing seems to we working. Also tried removing the SD card reader, tried the original example code. I also tried Hardware Serial as it should be but again, it doesnt support this sensor and ends up resulting in errors(or vice versa). Nothing.

Here is the working Uno code:

#include <DFRobot_WT61PC.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

#define FILE_BASE_NAME "Pomiar"
#define VCC2 7

const uint8_t CS_PIN = 4;

SoftwareSerial mySerial(2, 3);
SoftwareSerial mySerial2(5, 6);
DFRobot_WT61PC sensor(&mySerial);
DFRobot_WT61PC sensor2(&mySerial2);


File myFile;
File file;

const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.txt";

void setup()
{

  pinMode(VCC2,OUTPUT);
  digitalWrite(VCC2,HIGH);

  //Use Serial as debugging serial port 
  Serial.begin(9600);
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  while (SD.exists(fileName)) {
    if (fileName[BASE_NAME_SIZE + 1] != '9') {
      fileName[BASE_NAME_SIZE + 1]++;
    } else if (fileName[BASE_NAME_SIZE] != '9') {
      fileName[BASE_NAME_SIZE + 1] = '0';
      fileName[BASE_NAME_SIZE]++;
    } else {
      Serial.println(F("Can't create file name"));
      return;
    }
  }

  
  //Use software serial port mySerial as communication seiral port 
  mySerial.begin(9600);
  mySerial2.begin(9600);
  //Revise the data output frequncy of sensor FREQUENCY_0_1HZ for 0.1Hz, FREQUENCY_0_5HZ for 0.5Hz, FREQUENCY_1HZ for 1Hz, FREQUENCY_2HZ for 2Hz, 
  //                        FREQUENCY_5HZ for 5Hz, FREQUENCY_10HZ for 10Hz, FREQUENCY_20HZ for 20Hz, FREQUENCY_50HZ for 50Hz, 
  //                        FREQUENCY_100HZ for 100Hz, FREQUENCY_125HZ for 125Hz, FREQUENCY_200HZ for 200Hz.
  sensor.modifyFrequency(FREQUENCY_10HZ);
  sensor2.modifyFrequency(FREQUENCY_10HZ);
}


void loop()
{

  long timestamp = millis();

  float acc1;
  float acc2 ;
  float acc3 ;
  float gyro1 ;
  float gyro2 ;
  float gyro3;
  float angle1;
  float angle2;
  float angle3;

  float acc12 ;
  float acc22 ;
  float acc32 ;
  float gyro12;
  float gyro22 ;
  float gyro32 ;
  float angle12 ;
  float angle22;
  float angle32;
  
  mySerial.listen();
  if (sensor.available()) {
    
    acc1 = sensor.Acc.X;
    acc2 = sensor.Acc.Y;
    acc3 = sensor.Acc.Z;
    gyro1 = sensor.Gyro.X;
    gyro2 = sensor.Gyro.Y;
    gyro3 = sensor.Gyro.Z;
    angle1 = sensor.Angle.X;
    angle2 = sensor.Angle.Y;
    angle3 = sensor.Angle.Z;
    
  }

  mySerial2.listen();
  if (sensor2.available()) {
    
    acc12 = sensor2.Acc.X;
    acc22 = sensor2.Acc.Y;
    acc32 = sensor2.Acc.Z;
    gyro12 = sensor2.Gyro.X;
    gyro22 = sensor2.Gyro.Y;
    gyro32 = sensor2.Gyro.Z;
    angle12 = sensor2.Angle.X;
    angle22 = sensor2.Angle.Y;
    angle32 = sensor2.Angle.Z;

  }

  myFile = SD.open(fileName, FILE_WRITE);

  if (myFile) {
    myFile.print(timestamp);
    myFile.print("|");
    myFile.print(acc1);
    myFile.print("|");
    myFile.print(acc2);
    myFile.print("|");
    myFile.print(acc3);
    myFile.print("|");
    myFile.print(gyro1);
    myFile.print("|");
    myFile.print(gyro2);
    myFile.print("|");
    myFile.print(gyro3);
    myFile.print("|");
    myFile.print(angle1);
    myFile.print("|");
    myFile.print(angle2);
    myFile.print("|");
    myFile.print(angle3);
    myFile.println("");

    myFile.print(timestamp);
    myFile.print("|");
    myFile.print(acc12);
    myFile.print("|");
    myFile.print(acc22);
    myFile.print("|");
    myFile.print(acc32);
    myFile.print("|");
    myFile.print(gyro12);
    myFile.print("|");
    myFile.print(gyro22);
    myFile.print("|");
    myFile.print(gyro32);
    myFile.print("|");
    myFile.print(angle12);
    myFile.print("|");
    myFile.print(angle22);
    myFile.print("|");
    myFile.print(angle32);
    myFile.println("");
    
    myFile.close();
  } else {
    Serial.println("error opening test.txt");
  }
}

I also found this question: Arduino Mega 2560 SoftwareSerial Not Working. The guy has exact problem as I do but nooone found a solution to it. I also want to note that the manufacturer states that this library and sensor works fine on Mega. Really need your help.

cheers

@edit So I did some tests and the only code that actually shows something in monitor is:

#include <DFRobot_WT61PC.h>

DFRobot_WT61PC sensor(&Serial);

void setup()
{
  //Use Serial as debugging serial port 
  
  //Use software serial port mySerial as communication seiral port 
  Serial.begin(9600);
  sensor.modifyFrequency(FREQUENCY_10HZ);
}


void loop()
{
  if (sensor.available()) {
    Serial.print("Acc\t"); Serial.print(sensor.Acc.X); Serial.print("\t"); Serial.print(sensor.Acc.Y); Serial.print("\t"); Serial.println(sensor.Acc.Z); //acceleration information of X,Y,Z
    Serial.print("Gyro\t"); Serial.print(sensor.Gyro.X); Serial.print("\t"); Serial.print(sensor.Gyro.Y); Serial.print("\t"); Serial.println(sensor.Gyro.Z); //angular velocity information of X,Y,Z
    Serial.print("Angle\t"); Serial.print(sensor.Angle.X); Serial.print("\t"); Serial.print(sensor.Angle.Y); Serial.print("\t"); Serial.println(sensor.Angle.Z); //angle information of X, Y, Z 
    Serial.println(" ");
  }
}

monitor But as you see the output is corrupted.

14
  • When you can provide a serial instance to the library to use, then it is not written for SoftwareSerial only. Please show how you tried with HardwareSerial and the resulting errors
    – chrisl
    Jun 15, 2022 at 6:35
  • Quoted from SoftwareSerial Library, "Not all pins on the Mega and Mega 2560 boards support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69)", your code not using those pins.
    – hcheung
    Jun 15, 2022 at 6:42
  • @hcheung I tried them, the code I provided working code for uno so there is proof I didnt make a mistake anywhere
    – Aivion2000
    Jun 15, 2022 at 6:44
  • Also I took a look at the library you mentioned, there is no reason why HardwareSerial not work, if it is causing errors, it is something else, probably not instantiates the Serial1 or Serail2 correctly.
    – hcheung
    Jun 15, 2022 at 6:44
  • If you have problem in Mega, shows the code that doesn't work in Mega, not the code that work in Uno.
    – hcheung
    Jun 15, 2022 at 6:45

2 Answers 2

0

It's quick and dirty but you can make it Mega compatible by deleting the line #include <SoftwareSerial.h>, and replacing the lines

SoftwareSerial mySerial(2, 3);
SoftwareSerial mySerial2(5, 6);

with

#define mySerial Serial1
#define mySerial Serial2

replacing Serial1 and Serial2 with whichever of the Mega's serial ports you wish to use for mySerial and mySerial2, and connecting your DFRobot sensor signals to the correct pins for your chosen hardware serial ports.

1
  • Thx king it worked, also we connected tx to rx and rx to tx. We don't know why it's working, but it is.
    – Aivion2000
    Jun 20, 2022 at 20:34
0

There are three fairly major issues in your code:

  • The Mega does not support SoftwareSerial on pins 2, 3, 5, 6 because those pins do not support change interrupts. Thus it simply can't work on those pins. This itself is a show-stopper.

  • Your code to read data is flawed. You test if a single byte is available (sensor.available()) and then proceed to read 9 bytes. There is no guarantee that 8 of them are actually there.

  • I'm confused about what you are doing in your second block of code. You are telling the sensor library to get its data from Serial, which is where you type stuff. All that would do is echo what you type, bearing in mind my point directly above.

7
  • I used an example from this library, I changed nothing in the code, just the way I initialize the sensor. I don't know how to use the sensor.available differently.
    – Aivion2000
    Jun 16, 2022 at 8:22
  • + there are always 9 bytes comming out of the sensor, we tested it on Uno and it worked perfectly, why would the sensor work differently on Mega?
    – Aivion2000
    Jun 16, 2022 at 8:27
  • The bytes don't come out instantly. They take time. If you check for 1 byte then you can't guarantee that 9 bytes have arrived. Try changing to if (sensor.available() >= 9).
    – Nick Gammon
    Jun 16, 2022 at 9:27
  • why would the sensor work differently on Mega - because it doesn't support those pins. How many times do we need to make this point?
    – Nick Gammon
    Jun 16, 2022 at 9:28
  • You are ignoring the most likely explanation for your problem.
    – Nick Gammon
    Jun 16, 2022 at 9:30

Your Answer

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

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