SDA interrupt on Arduino Nano

Hi,
i need to generate an interrupt when the serial buffer has new data.
I'm working on my old project (pobably someone remember me), a variometer with arduino Nano.
For my project i'm using a GY-BMP280 sensor and an Arduino nano (the library is the "SparkFunBME280.h").
i set the sensor so has to have a reading every 63ms. I would like an interrupt every new data (SDA pin) and measure the elapsed time between two interrupts.

Someone can help me??

Why not use Serial.available() to determine that new serial data has been received ?

Matteo1991:
Hi,
i need to generate an interrupt when the serial buffer has new data.
I'm working on my old project (pobably someone remember me), a variometer with arduino Nano.
For my project i'm using a GY-BMP280 sensor and an Arduino nano (the library is the "SparkFunBME280.h").
i set the sensor so has to have a reading every 63ms. I would like an interrupt every new data (SDA pin) and measure the elapsed time between two interrupts.

Someone can help me??

I'm confused. You talk about serial and the serial buffer and then you talk about SDA which is an I2C pin and isn't involved with serial but with I2C. What have you actually got there? You know all of that stuff is already interrupt driven don't you?

UKHeliBob:
Why not use Serial.available() to determine that new serial data has been received ?

Because i need of interrupt every new data! i don't wont to control if the buffer is empty or not!

Delta_G:
I'm confused. You talk about serial and the serial buffer and then you talk about SDA which is an I2C pin and isn't involved with serial but with I2C. What have you actually got there? You know all of that stuff is already interrupt driven don't you?

I use the "wire" library. The some tutorial speaks about "i2c" but they use "wire"... I 'm confused, aslo me....

I use the "wire" library

And which interface does the Wire library control?

I'm bored with the hand-waving now.

Post code. Post specific problems.

Matteo1991:
Because i need of interrupt every new data! i don't wont to control if the buffer is empty or not!

That's just the point. You already have an interrupt running for serial data. And available doesn't control the buffer, it tells you whether or not there is anything in the buffer. That's your flag that the interrupt fired.

Post a link to the exact BME280 module you are using.

No "data ready" interrupt is available from the BME280 sensor itself.

The Arduino has to query the device and receive the data over the selected bus, either I2C or SPI. You could use a timer or millis() to trigger the queries.

so I use if(Serial.available())?

Is the sensor attached to the Serial interface?
If so, yes.

Otherwise, see my earlier comment about hand-waving.

The sensor is this below:

My program is this:

#include <Wire.h>
#include "SparkFunBME280.h"
#include <LiquidCrystal.h>
#include <TimerOne.h>

BME280 Sensor; //Uses I2C address 0x76 (jumper closed)

#define n_samples 20 //20 samples for second

float pressure[n_samples];

void setup() {
//=========interrupts configuration ==========
//Timer1.initialize(50000); //50ms
//Timer1.attachInterrupt(isr_timer);
//========================================
//=========wire configuration ==========
Wire.begin();
Serial.begin(9600);
//========================================

//=========sensor configuration ==========
Sensor.begin();
Sensor.settings.commInterface = I2C_MODE;
Sensor.setI2CAddress(0x76); //if it doesn't work, try 0x77
    //runMode can be:
    //  0, Sleep mode
    //  1 or 2, Forced mode
    //  3, Normal mode
Sensor.settings.runMode = 3;
    //tStandby can be (time between two measure)
    //  0, 0.5ms
    //  1, 62.5ms
    //  2, 125ms
    //  3, 250ms
    //  4, 500ms
    //  5, 1000ms
    //  6, 10ms
    //  7, 20ms
Sensor.settings.tStandby = 5;
    //filter can be off or number of FIR coefficients to use:
    //  0, filter off
    //  1, coefficients = 2
    //  2, coefficients = 4
    //  3, coefficients = 8
    //  4, coefficients = 16
Sensor.settings.filter = 0;
    //pressOverSample can be: (number of bits per sample)
    //  0, skipped
    //  1 through 5, oversampling *1, *2, *4, *8, *16 respectively
Sensor.settings.pressOverSample = 1;
//========================================

//===========Check sensor=================
if(Sensor.beginI2C() == false){
Serial.println("Error: no sensor! Restart the sistem.");
while(1);//never ending cycle
}
else{
Serial.println("===The sensor is working!===");  
}
//========================================
delay(2000);
}

void loop() {
if(Serial.available()){
pressure[0] = Sensor.readFloatPressure()/100;//the pressure is reads in KPa
//=================================================================
Serial.println("========================================");  
Serial.println("Pressure:");  
Serial.print(pressure[0]); 
Serial.println("hPa"); 
Serial.println("========================================");  
//==================================================================
}

}

but “if(Serial.available()){” doesn’t work… Arduino doesn’t sent data…

i need to measure the exatly time between measures.

some idea?

Matteo1991:
i need to measure the exatly time between measures.

some idea?

You should go through the BMP280 datasheet. You will see that the BMP280 is only ever a slave device whether you are using I2C or SPI so that you have to ask it for the pressure and temperature. The BMP280 has no pin indicating when a measurement is available.

The closest I can see to you achieving "exatly time between measures" is for the Arduino to take a reading at known points in time. The known points could be achieved using an external clock generating interrupts, an internal timer/counter or by using millis() . I would expect that all these will give near "exatly time between measures" but since you don't say how accurate the time between measures needs to be I can't be certain.

stowite:
You will see that the BMP280 is only ever a slave device whether you are using I2C or SPI so that you have to ask it for the pressure and temperature. The BMP280 has no pin indicating when a measurement is available.

This is the main point. I don’t know “when” the meauser has been performed…
I need no more than 20 or 30 samples for second. Probably, if i set my sensor in 0.5ms “between measures” and i read every interupt (isr routine) of 0.5 second, the error will be very low…
do you agree??

I don’t understend how to measure the reading time…
I’m trying this code:

t1 = millis();
pressure[0] = Sensor.readFloatPressure()/100;//the pressure is reads in KPa
t2 = millis();
t = t2-t1;

And “t” is ever 1.00ms… the frequency in my project is 20/30Hz, more or less…

I think that the total error is accetable.

Some one can help me in estimating error?

p.s. Sensor.readFloatPressure()/100; doesn’t work in the ISR… :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

I don't follow your logic. You say you need "no more than 20 or 30 samples for second" so would 1 sample per second be acceptable. That would meet your stated requirement.

I would be interested to knowing under what conditions your pressure and temperature are changing fast enough that 20 or more samples a second are needed. I have worked with systems that deal with a human in freefall and this results in terminal velocity speed of around 180 ft per second. In 1/20 th of a second it will fall by the equivalent of about 9 ft. I can only imagine a need for 20 samples per second for some form of explosive decompression or the vertical dive of a rocket.

stowite:
I don't follow your logic. You say you need "no more than 20 or 30 samples for second" so would 1 sample per second be acceptable. That would meet your stated requirement.

This is a variometer.