Problem with I2C bus (SDA &SCL) and interrupts Int2 or Int3 in MEGA2560.

Pins 20 and 21, which are input hardware interrupts to Int2 and Int3 pins, also are used for the I2C interface (SDA and SCL signals respectively).
When using the Wire library and have a ISR attached to interruptions 3 or 2 (Pin 20 or 21) a call to ISR will occur every time the ISR signals SDA and SCL signals are activated
This will cause a behavior not desired in software and hardware. Also, if the ISR routine consumes time raised the Arduino Mega 2560 may crash and will require a restart.
Is incompatible using the I2C bus and Int2 or Int3 interruptions at same time.
It took me several hours to figure out what was happening. I have not been able to find a warning about this incompatibility in the Arduino documentation about MEGA2560. In addition, it may affect arduido due. That's the reason for creating this topic.

That's just how it is. Many pins can be different things, but not at the same time.
To make a sketch crash by using interrupts in the wrong way is easy, calling the Serial library from inside an ISR is often enough.

When you need external interrupt, use other pins.
Or use the PCINT library, for the Pin Change Interrupt. They are for example available at pin A8 to A15.

Google for : arduino atmega2560 pin mapping
There is a picture with the Arduino pins and usage.

I'ts possible to use both at the same time, if the application allows for it.

I've used this configuration - I could disable the interrupt and start the I2C setup whenever I used the I2C, and re-attached the interrupts after completing the I2C function (attaching an interrupt also disables the I2C).

Also, make sure that your ISR is as short as possible, such as just setting a flag that gets checked elsewhere in your code. An ISR that only runs for a couple of microseconds is unlikely to crash.

SandraOos:
Also, make sure that your ISR is as short as possible, such as just setting a flag that gets checked elsewhere in your code. An ISR that only runs for a couple of microseconds is unlikely to crash.

I honestly believe that this is yet ANOTHER misleading "urban legend" unnecessarily propagated on this site without merit. Computing devices are not stand-alone , they interact and interrupts are part of the system to accomplish that. Repeating this advise in just not right, you may as well state "use blink without delay" instead.

Off my soapbox.
Cheers Vaclav

The device I've done is a Geiger counter that capture real-time information such as date, time, temperature, humidity, atmospheric pressure, and counts per minute in the geiger tube. I was using a real-time clock chip DS1302 based.
Everything worked fine until I connected at atmospheric pressure sensor that communicates with the I2C bus. At the time I replaced the DS1302 RTC with one based on D1307, known as Tiny RTC, to use the I2C bus to connect both sensors the RTC.

The interrupt routine was very simple

TubeImpulse void ()

{
_EventoEntrada ++;
}

With the variable defined as

Volatile.unsigned long _EventoEntrada;

The problem occurs when using the Wire library functions

Wire.begin (); // Start Wire.
dps.init (); // QFE (Field Elevation above ground level) is September to 0 meters.
// Same as init (MODE_STANDARD, 0, true);

Obviously, everything was resolved by attaching interruption to pin 19.

attachInterrupt (4, TubeImpulse, RISING); // Initializes interrupt 4 (pin 19) are released 20 and 21
// for I2C communicatios.

It is not my intention to PROPAGATE AN URBAN LEGEND. It took me two hours to discove the nature of the problem. I just wanted to share this experience with people who could also make the same mistake, because in the documentation MEGA2560 there are NOT cautioned that the SDA and SDL signals (on pins 20 and 21) are simultaneously those used for Int2 and Int3 interruptions.

I am unable to disable the interrupt, access the i2c bus, and then dwell interruption again, because I would lose track of events geiger tube. I regret that my intention was taken as SPREAD AN URBAN LEGEND. Just wanted to prevent someone from committing the same mistake I did.

Vaclav:
I honestly believe that this is yet ANOTHER misleading "urban legend" unnecessarily propagated on this site without merit. Computing devices are not stand-alone , they interact and interrupts are part of the system to accomplish that. Repeating this advise in just not right, you may as well state "use blink without delay" instead

It's advice that has a sound basis, particularly in the microcontroller world.

To say it is "just not right" or lacks merit exhibits a lack of understanding, honestly believed or otherwise.

Computing devices are not stand-alone , they interact and interrupts are part of the system to accomplish that.

This is what grown-ups call a "non sequitur". Even in real-world systems like Linux, interrupt handling is split into "top half" (the time-critical section) and "bottom half" (deferred processing)

I'm sorry I caused misunderstandings in this forum. I was working on the project shown in photo and made a mistake induced by incomplete documentation between interruptions and management of I2C bus. I wanted to prevent others from committing future the same error. Henceforth I will avoid sharing anything in this forum, since it caused problems.

alfelipe:
I'm sorry I caused misunderstandings in this forum.

You did not cause any misunderstandings. What @Vaclav posted is, quite simply, wrong.

I wanted to prevent others from committing future the same error.

Thank you.

Henceforth I will avoid sharing anything in this forum, since it caused problems.

You did not cause any problems. Please continue sharing.

alfelipe:
... Henceforth I will avoid sharing anything in this forum, since it caused problems.

Just ignore some comments. We appreciate your input. We are all Arduino-enthusiasts and moderators can take care of problems.

Is that a mini gel-lead battery in the photo ?

Yes. Is a lead-acid battery of 6 volts 2 Amperes hour. The project is a Geiger counter capable of use at least 4 types of Geiger-Muller tubes, with operates from 450 to 1500 plateau voltage. In front there is only a pontentiometer with switch. All is software-selected from touch screen. Took a week dedicating three hours every afternoon.

Very nice !
My Geiger board is somewhere in a box, doing nothing. Is has no display at all. I want to add 433MHz transmitter to it someday, so I can receive the data with my Mega board and upload the data to ThingSpeak.com.

The most difficult was to design the GUI to select the type of tube, with its characteristics defined in an array of struct. Then you need to choose between the "llogger mode" and "explorer mode".
Two of the four tubes (MCT17 and 6107) are sensitive to alpha radiation and needs greater anode voltage. What seemed harder, curiously, at the end was the easiest. The voltage from 450 to 1700 volts is controlled by software by varying the duty cycle of an integrated digital output with a PWM DC-DC converter. Then filtred with 4 x 10 nF / 2000 Volts capacitors. Frequency of DC step-up converter is greater 48 Khz, so is enought capacity. All features of the tubes are stored in a arrray structures, so once you choose the tube on the initial menu, the software provides proper tension and the conversion factor to calculate the dose in microsieverts, when applicable. In a week I will have fully completed. It's just a hobby.

typedef struct

{
String _TubeName;
int _Volts;
int _PWM;
float _Factor;
} record_type;

record_type _TubeDef[4];

void setup()
{

/* DefiniciĆ³n de los 4 tipos de tubo Geiger-Muller utilizados en el proyecto */

_TubeDef[0]._TubeName = "M4011 ";
_TubeDef[0]._Volts = 450;
_TubeDef[0]._PWM = 10;
_TubeDef[0]._Factor = 0.0081;

_TubeDef[1]._TubeName = "J308Br ";
_TubeDef[1]._Volts = 450;
_TubeDef[1]._PWM = 10;
_TubeDef[1]._Factor = 0.0086;

_TubeDef[2]._TubeName = "6107 ";
_TubeDef[2]._Volts = 750;
_TubeDef[2]._PWM = 21;
_TubeDef[2]._Factor = 0.0080;

_TubeDef[3]._TubeName = "MCT17 ";
_TubeDef[3]._Volts = 1300;
_TubeDef[3]._PWM = 121;
_TubeDef[3]._Factor = 0.0014;

...

The potentiometer has swich on/off from battery and control sound level of a piezo buzzer when arrives a particle. Power also is taken from USB port without switch on/off. Led red is Mega 2560 5 Volts DC, and geen flashes when sounds pulse. Really simple. But an unsolved problem is if you plug in BNC a wrong tube, for example, M4011 instead MCT17, overvoltage may destruct it inmediatly.

In "logger mode", the battery guarantees a range of 4 hours. Choose lead-acid by its ability to quickly load and "indelicate". A power connector 12 volts on the rear panel allows continued operation and charges the battery at the same time. On the left side panel and rear, is the RTC, Atmospheric pressure sensor (both with IIC bus) and humidity and temperature sensor, adds ambiental parameters to CMP count. The idea is to monitor the air alpha radiation to detect the presence of radon gas.