Getting a magnetometer reading via serial as fast as possible

Hi all!

This is my first arduino project, so I am still learning everything.
My project requires me to read values from a HMC588L (from adafruit) as fast as possible in a computer.
So, I am using an arduino to receive an I2C signal from the magnetometer and send the values through serial to the computer.
I am currently using a slightly modified version of the code adafruit provided. As far as I understand, the magnetometer can produce more than 100 measurements per second. I can only read about 10-12.
I think that the major choking point is the serial communication. I've been trying to figure out the most efficient way to transfer the data, but everybody is saying different things on forums.
Here is my code right now. I am reading a digital pin, because that is how my synchronization happens with a labview code.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//timing variables
long interval = 50;           // interval at which to send the serial signal
long previousMillis = 0;        // will store last time LED was updated

//trigger variables
const int  triggerPin = 2;
int triggerState = 0;         // current state of the trigger
int lastTriggerState = 0;     // previous state of the trigger

/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

void displaySensorDetails(void)
{
  sensor_t sensor;
  mag.getSensor(&sensor);

  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  Serial.println("------------------------------------");

}

void setup(void) 
{
  // initialize the button pin as a input:
  pinMode(triggerPin, INPUT);
  
  //initialize Serial
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
  /* Display some basic information on this sensor */
  displaySensorDetails();
}

void loop(void) 
{
 unsigned long currentMillis = millis();
 
 
 
  // read the pushbutton input pin:
  triggerState = digitalRead(triggerPin);
  
    // compare the triggerState to its previous state
  if (triggerState != lastTriggerState) {
    // if the state has changed, send the signal
    if (triggerState == HIGH) {
      Serial.print("triggered  ");
       Serial.print("t: "); Serial.print(currentMillis); Serial.println("  ");
    }
  }
   lastTriggerState = triggerState;
  
  
  
  
  // check to see if it's time to send the signal; that is, if the 
  // difference between the current time and last time you sent the signal 
  // is bigger than the interval.
  if(currentMillis - previousMillis > interval) {
    // save the last time you sent the signal 
    previousMillis = currentMillis;   

     Serial.print("t: "); Serial.print(currentMillis); Serial.print("  ");
     
     /* Get a new sensor event */ 
     sensors_event_t event; 
     mag.getEvent(&event);

     /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
     Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
     Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
     Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");

  }

}

I am hoping you guys can help me optimize this code. Thanks!

char Trigmes[]| | = "tr";

What's that?

Being able to read a sensor many,many times a second is quite a different issue to printing the values read at a very slow rate on a serial line.

Edit: cross posting is really frowned-upon here, so to save your embarrassment, I've deleted the duplicate.

What's that?

Sorry, I was messing around with concatenating strings and forgot to clean it. I deleted it from the original post.

Edit: cross posting is really frowned-upon here, so to save your embarrassment, I've deleted the duplicate.

Sorry about that.. I thought different people read and post on different sections. Lesson learned :slight_smile:

Being able to read a sensor many,many times a second is quite a different issue to printing the values read at a very slow rate on a serial line.

So i guess I need to optimize my serial communication. I thought that starting multiple serial communications takes way longer than sending one long string. Is that correct? If so, then I need to somehow concatenate all the different stings into one.
What is an efficient way of doing that?

Well, at 9600, you're limited to just 960 characters per second, so bump that to 115200 first, and see how it goes.

Well, at 9600, you're limited to just 960 characters per second, so bump that to 115200 first, and see how it goes.

well.. I feel pretty stupid, but right after I posted, I tried that and it did wonders..

Was my theory about multiple connections correct though?

Was my theory about multiple connections correct though?

For a 16MHz processor?
No, not really.

allright!
thank you! :slight_smile: