Attaching interrupts to the MEGA2560


I am trying to use the interrupts with the MEGA2560 and I find the different pins are acting inconsistent with the levels I am reading. When I boot up I read the voltage on interrupts 2(pin 21) and 3 (pin 20) at 5 volts, but the interrupts 0 (pin 2) and 1 (pin 3) read a low level, 0 volts. I get the interrupts to work just fine with interrupts 2 and 3 but they do not work with interrupts 0 and 1. Does any one have any ideas what might cause this discrepancy between these interrupt inputs?


Does any one have any ideas what might cause this discrepancy between these interrupt inputs?

Just as a guess, I'd say it is your sketch. Of course, that's just a guess, not being able to see your sketch.

More to the point without knowing what you've connected to the various pins there's no way to judge whether those values are as intended or not. Circuit please....


Thanks for the reply. That’s the point. I disconnected them. There is nothing connected to the pins. I am measuring them with a DVM. I will attach the setup routine code but I don’t think the problem is there. I have two MEGA boards and they both do the same thing. As I said I can make it work with interrupt 2 but not interrupt 1. When initialized the the two pairs of interrupts read differently.

#include <SoftwareSerial.h>
#include "HWSerial.h"
#include "sms.h"
//#include "GSM.h"
//#include "SIM900.h"
#include <SD.h>
boolean gsmStarted=false;
const byte smsResetPin = 8;//SMS Reset pin
const byte smsPowerPin = 9;//SMS Power on/Off pin
const byte smsDTRPin = 42;//SMS DTR Pin
const byte smsStatusPin = 43;//SMS Status Pin
const byte phoneNumberLength = 20;
const byte messageLength = 161;
const int serialBaud = 57600;
char smsbuffer[messageLength];
char number[] = "BLANK NUMBER";
char  lastPhoneNumberToCall[phoneNumberLength] = "Last Phone number";
byte last_successful_msg = 0;
String inputString = "";         // a string to hold incoming data
const int sdChipSelect = 53;//Mega/MegaADK SD card select                  
volatile int ringFlag = LOW;
File dataFile;

void setup() {
//  pinMode(3,INPUT);
//  digitalWrite(3,HIGH);
  pinMode(smsPowerPin, OUTPUT);
  pinMode(smsDTRPin, OUTPUT);
  digitalWrite(smsDTRPin, HIGH);//Set DTR high
  pinMode(smsStatusPin, INPUT);
  digitalWrite(smsStatusPin, HIGH);
    if (digitalRead(smsStatusPin) == true){
      digitalWrite(smsPowerPin, LOW);//Initiate Power on
      digitalWrite(smsPowerPin, HIGH);//Return SMS power pin to high, done
  long timeOut = 100;
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baud rate.
  //For http uses is recommended to use 4800 or slower.
  if (gsm.begin(9600)){
  else Serial.println("\nstatus=IDLE");
// initialize SD card
   Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
//Initialize SD card  
  // see if the card is present and can be initialized:
  if (!SD.begin(sdChipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  Serial.println("card initialized.");
  // Open up the file we're going to log to!
  dataFile ="datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;

attachInterrupt(2, ringInterrupt, FALLING);//Interrupt 2, pin 21
void loop() {

Is there some information where I could find the initialization of these pins?


Try loading the blink example. Then measure the pin values. If they are correct, then one of the instances in your sketch is doing something with the interrupt pins.

All pins default to INPUT mode with internal pull-ups disabled - this means the value of digitalRead() and analogRead() will be undefined for any unconnected pins - they are "floating". Remember the input impedance of input pins is effectively infinite...

You either need to enable input pull ups (as you do for smsStatusPin - note there is now an INPUT_PULLUP mode for pinMode()), or connect something to the pin to define the voltage.

Connecting an interrupt to an unconnected pin sounds pointless to me - I presume you were intending to connect something but got confused by the floating pin behaviour not realizing its normal?