Interrupt detection with 9 V power

Hi:

I have written code which detects pulses from a hall effect sensor. I am using interrupt 0 to detect these pulses.
When I power the arduino UNO with a 9 volt power supply (transformer) , the interrupt reads a high number (around 142) regardless of the frequency it is receiving. When the arduino is powered with th usb, it reads the correct value.
I have verified the signal accuracy with an oscilloscope.
Does anyone have any idea why and if I can correct this?

Mike

Could you say more about "the interrupt reads a high number (around 142)". What does that number represent and how do you read it?

Could you say more about "the interrupt reads a high number (around 142)". What does that number represent and how do you read it?

Hi johnwasser:
Thanks for the reply.
The interrupts increment a value in the in the sketch and I output that to an LCD and (to debug) to serial.
I should note that , to test the sketch (in the absence of a flowmeter) the pulse is generated by another arduino mega which generates a 5 volt pulse of 10ms duration (adjustable) and varying (adjustable) frequency.
This works as long as both arduinos are powered by usb.
I have attached the sketch.
I just noticed that when the UNO is powered with the 9 volt supply and there is no connection to digital pin 2 (interrupt), the interrupt count is 61. This makes me suspect interference from 60 Hz line voltage.
Could the higher number be some harmonic between the two power supplies causing interference?
If so, is there a simple way to eliminate this interference?
Is this a common problem with arduinos?

PS: the sketch logs well depth flow and time to serial (for a data acquisition program) and an SD card.

/*
  AnalogReadSerial
 Reads an analog input on pin 0, prints the result to the serial monitor and an LCD. 
 The circuit:
 Pulse output from flowmeter to digital pin 3 (interrupt).
 0-20 mAmp reading from well transducer converted to 1-5 Volts with 250 ohm resistor to analog pin 0.
 * LCD RS pin to digital pin 3
 * LCD Enable pin to digital pin 4
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 6
 * LCD D6 pin to digital pin 7
 * LCD D7 pin to digital pin 8
 * LCD R/W pin to ground
 * 10K potentiometer:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 */

#include <LiquidCrystal.h>                    // include the library code:
#include <SD.h>                               // include SD library 
#include "RTClib.h"
#include <Wire.h>

RTC_DS1307 RTC;

File myFile;                                 //?
LiquidCrystal lcd(3, 4, 5, 6, 7, 8);         // initialize the library with the numbers of the interface pins
unsigned long time;
unsigned long timeTwo;
unsigned long timeSerial;
unsigned long timeTwoSerial;

float LogInterval = 60000.0;                // Time in milliseconds that depth is printed to serial.
int ppg = 0;                                // pulses per gallon output by the flowmeter

float f = 0.0;                              // flow
float timer = 0.0;                          // timer for data
float sensorValue = 0;
float mA = 0;
float mAAverage = 0;                        //mAAverage will be used to average fluctuating sensorValue
volatile int pulseCount;
int hallsensor = 2;                        // The pin location of the flow sensor.

void setup() {  
  Serial.begin(9600);
  attachInterrupt(0, pulseCounter, RISING);         // Interrupt on rising edge of signal.
  pinMode(hallsensor, INPUT);              // initializes digital pin 2 as an input.

  Serial.print("Initializing SD card..."); 
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Initializing SD");
  lcd.setCursor(0,1);
  lcd.print("card...");
  delay(2000);
  lcd.clear();

  /* Note that even if it's not used as the CS pin, the hardware SS pin 
   (10 on most Arduino boards, 53 on the Mega) must be left as an output 
   or the SD library functions will not work. 
   so Pin 10 is set to output for Duemilanove*/

  pinMode(10, OUTPUT);  

  if (!SD.begin(10)) {
    Serial.println("Initialization failed!");
    lcd.clear();  
    lcd.setCursor(0,0);
    lcd.print("Initialization");
    lcd.setCursor(0,1);
    lcd.print("failed.");
    delay(3000);
    lcd.clear();
    return;
  }
  Serial.println("Initialization done.");
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Initialization");
  lcd.setCursor(0,1);
  lcd.print("done.");
  delay(3000);
  lcd.clear();
  Wire.begin();
  RTC.begin();                                    // open? clock
  myFile = SD.open("depth.txt", FILE_WRITE);      // open the file. note that only one file can be open at a time  

  DateTime now = RTC.now();  

  myFile.print(now.year(), DEC);                  // Print year/month/day to depth.txt
  myFile.print('/');
  myFile.print(now.month(), DEC);
  myFile.print('/');
  myFile.println(now.day(), DEC);
  myFile.close();                                 // close file
  delay(1000);                                    // delay to allow analogIn to stabilize 
  sensorValue = analogRead(A0);
  mA = sensorValue/51.2;
  mAAverage = mA; 
}

void loop() {

  /* When millis is >= 1000 read sensorValue , convert to mA and average with last mA reading.
   * This keeps a running average for 60 one per second readings of mA.(noise reduction)
   */

  sei();                                          // enable interrupt 
  time = millis() - timeTwo;                      // reset time to 0
  timeSerial = millis() - timeTwoSerial;          // reset timeSerial to 0

  if (time >= 1000)
  {
    cli();                                        // disable interrupt
    //Serial.println(time);//debugline
    sensorValue = analogRead(A0);
    mA = sensorValue/51.2;                        // Convert sensorvalue 0-1024 to 0-20 mA
    mAAverage = (mAAverage + mA)/2;               // Average sensorValue readings every loop
    f = (pulseCount * 60.0) / ppg;
    //Serial.println(f);//debugline

    lcd.begin(16, 2);                             // set up the LCD's number of columns and rows
    lcd.print(mA,3);                              // Print average mA value to the LCD.    
    lcd.print(" mA");
    lcd.setCursor(0,1);
    lcd.print(pulseCount);
    lcd.print(" pulses/second");    

    pulseCount = 0; 
    timeTwo = millis();    
  }

  /**************************************************************
   * Every LogInterval print the average of the last 60 readings *
   * to serial and reset mAAverage to last mA reading            *
   ***************************************************************/
  if (timeSerial >= LogInterval)
  { 
    timer = millis() / LogInterval;
    Serial.print("DATA");
    Serial.print(",");
    DateTime now = RTC.now();
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print( "," );
    Serial.print(timer);
    Serial.print(",");
    Serial.print(mAAverage,3);
    Serial.print(",");
    Serial.println(f);
    mAAverage = mA;

    /***************************************************************** 
     * open file called depth.txt and write time, mAAverge, flow (f). *
     * note that only one file can be open at a time, so you have to  *
     * close this one before opening another.                         *
     ******************************************************************/
    myFile = SD.open("depth.txt", FILE_WRITE);

    if (myFile) {                                // if the file opened okay, write to it
      DateTime now = RTC.now();

      
      myFile.print(now.hour(), DEC);
      myFile.print(':');
      myFile.print(now.minute(), DEC);
      myFile.print(':');
      myFile.print(now.second(), DEC);
      myFile.print( "," );
      myFile.print(timer);
      myFile.print( "," );
      myFile.print(mAAverage,3);                
      myFile.print(",");
      myFile.println(f);
      myFile.close();
    } 
    else {                                        // if the file didn't open, print an error:      
      Serial.println("error opening depth.txt");
    }
    timeTwoSerial = millis();
  }
}
/********************************************************
 * Function called by interrupt 0. increment pulseCount *
 ********************************************************/
void pulseCounter ()
{  
  pulseCount++;
}

Did you remember to connect a ground wire between the 'sensor' and your Arduino? If they are both powered off the same USB host they will have a common ground but if one is on battery power, you need the ground connection.

Hi johnwasser:

That works! I had a ground connected , but through what I though was a 10 k pulldown resistor.
with the ground connected directly it works well.
Your help is appreciated.

Thanks,
Mike

Glad I was able to help. :slight_smile: