acquire data through the timerOne and MPU6050

greetings, I’m almost new in the forum first I apologize if the topic does not go according, I’m developing a project I’m using the MPU6050(only the accelerometer) and arduous MEGA2560

#include <TimerOne.h>
#include<Wire.h>

const int MPU_addr = 0x68; 
int16_t AcX, AcY, AcZ;
void setup() {
  Wire.begin();
  Wire.setClock(400000L);
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     
  Wire.endTransmission(true);
  Serial.begin(115200);

  Timer1.initialize(100000);
  Timer1.attachInterrupt(MPU);
}
void loop() {

}
void MPU()
{
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 6, true); 
  AcX = Wire.read() << 8 | Wire.read(); 
  AcY = Wire.read() << 8 | Wire.read(); 
  AcZ = Wire.read() << 8 | Wire.read(); 
  
  Serial.print(AcX);
  Serial.print(","); Serial.print(AcY);
  Serial.print(","); Serial.println(AcZ);
  }

I need to acquire from time to time the readings in this case angle and I have to use the TimerOne and the problem is that it does not print the data on the serial monitor

I appreciate your help

You can't use Serial.print etc. in the context of a timer callback routine.
In the ISR, collect, as global variables, the values you want, then print these in the loop().

You can't use Serial.print etc. in the context of a timer callback routine.

You can't make an I2C reading in the context of an interrupt service routine as well.

Both Serial printing and I2C rely on interrupts, and they are disabled within the ISR.

Why do you need to use a Timer1 interrupt?

thank you very much for responding i am new to arduino and the forum
Basically, as I understand it, I can't interrupt an I2c communication?

I need to use timers to collect samples according to a sampling time in this case 100 milliseconds and send them to the jupyter notebook.

thank you for your help

since it is a 100mS sampling frequency, you can do it something like this, in the loop(), without a hardware timer:

static uint32_t lastCollectionAtMs ;
if( millis() - lastCollectionAtMs > 100 ) {
   
  // collect and process samples here

  lastCollectionAtMs = millis() ; 
}

Thank you very much for the help and if now I understand that within ISR more if tegno that to use timer they have informed to me that making use of flags could be done but I do not find information on how to implement it

thank you for your help

andresz96:
Basically, as I understand it, I can’t interrupt an I2c communication?

There would rarely be any reason to interrupt an I2C communication. There are very few reasons to use an interrupt and most “newbies” do not understand what their actual purpose is and are prone to imagine they are for things they are not.

It is most improbable you will need to time something with more resolution than a millisecond, so you have no need to use a timer. You use millis().

And as always, once you understand these, you can move beyond the “XY Problem” and ask the proper questions as to how to do the thing you really wish to do. :sunglasses:

andresz96:
Thank you very much for the help and if now I understand that within ISR more if tegno that to use timer they have informed to me that making use of flags could be done but I do not find information on how to implement it

thank you for your help

You have said you have to use timer1 in your first post. If that is the case, you can set a flag in the ISR of timer1. You can test this flag in the loop() and then request data from the mpu6050 and reset the flag. There would, however, be better ways of achieving the same thing.
Further, it is possible to configure the Mpu6050 to generate an interrupt on some event, such as new data available to read. You could also handle this interrupt, set a flag and test it in the loop() and proceed as above.
It all depends on what task you have been given.