[solved] Hall in D2 + Timer0 sketch to count frequency = bizarre result

Adding 1o kohm

Object: measure frequency
Sensor: hall attached to pin D2
Arduino UNO
Sketch (below)
// Frequency timer; Reply 1
// Author: Nick Gammon
// Date: 10th February 2012
// Input: Pin D2

Data and questions
1)
With hall in D2:
Example of one display:
“ Took: 319905 counts. Frequency: 50.01 Hz. “
Hz moves every 500ms between 50 +/- 2Hz approx.

Q).- Is 50 +/- 1 Hz a coherent result?

2)
Without any sensor in D2: readings are the same as in case 1).
I think with no signal in D2 attachInterrupt(0, isr, RISING) doesn’t fire so reading should have stopped.

Q).- What is wrong?

Thank you in advance


Sketch
volatile boolean first;
volatile boolean triggered;
volatile unsigned long overflowCount;
volatile unsigned long startTime;
volatile unsigned long finishTime;

// here on rising edge
void isr ()
{
unsigned int counter = TCNT1; // quickly save it

// wait until we noticed last one
if (triggered)
return;

if (first)
{
startTime = (overflowCount << 16) + counter;
first = false;
return;
}

finishTime = (overflowCount << 16) + counter;
triggered = true;
detachInterrupt(0);
} // end of isr

// timer overflows (every 65536 counts)
ISR (TIMER1_OVF_vect)
{
overflowCount++;
} // end of TIMER1_OVF_vect

void prepareForInterrupts ()
{
// get ready for next time
EIFR = bit (INTF0); // clear flag for interrupt 0
first = true;
triggered = false; // re-arm for next time
attachInterrupt(0, isr, RISING);
} // end of prepareForInterrupts

void setup ()
{
Serial.begin(115200);
Serial.println(“Frequency Counter”);

// reset Timer 1
TCCR1A = 0;
TCCR1B = 0;
// Timer 1 - interrupt on overflow
TIMSK1 = bit (TOIE1); // enable Timer1 Interrupt
// zero it
TCNT1 = 0;
overflowCount = 0;
// start Timer 1
TCCR1B = bit (CS20); // no prescaling

// set up for interrupts
prepareForInterrupts ();

} // end of setup

void loop ()
{

if (!triggered)
return;

unsigned long elapsedTime = finishTime - startTime;
float freq = F_CPU / float (elapsedTime); // each tick is 62.5 nS at 16 MHz

Serial.print ("Took: “);
Serial.print (elapsedTime);
Serial.print (” counts. ");

Serial.print ("Frequency: “);
Serial.print (freq);
Serial.println (” Hz. ");

// so we can read it
delay (500);

prepareForInterrupts ();
} // end of loop

Hi, can you post a copy of your circuit please including how you are powering the project, CAD or picture of hand drawn circuit in jpg, png or pdf.
It looks as though there is no connection to the input of the arduino at all, 50Hz is radiation from the mains power, I assume that your mains power is 50Hz.

Check the connections to the Hall Effect device and what is its spec number?

Hope to help.

Tom...... :slight_smile:

TomGeorge:
Hi, can you post a copy of your circuit please including how you are powering the project, CAD or picture of hand drawn circuit in jpg, png or pdf.
It looks as though there is no connection to the input of the arduino at all, 50Hz is radiation from the mains power, I assume that your mains power is 50Hz.

Check the connections to the Hall Effect device and what is its spec number?

Hope to help.

Tom… :slight_smile:

Tom: you hope.

  • Circuit: attached
  • Hall: Honneywell 2SS52M Series
  • Connection checked: UNO see ‘surrounding-noise’ main power freq, and the wire become the antenna !!:
    1).- if I disconnect the wire directly form D2 reading stops.
    2).- if I disconnect hall but maintaining wire from D2 to breadboard UNO still reads 50 hz (+/-).
    3).- if I touch wire or hall with fingers, reading change from 50 to 33000 - 70000 Hz.
    In all cases with no magnet moving.

I will try using shielded wire in the hall active leg, but hall itself remains there.

Any recommendation?

Thank you

TomGeorge:
Hi, can you post a copy of your circuit please including how you are powering the project, CAD or picture of hand drawn circuit in jpg, png or pdf.
It looks as though there is no connection to the input of the arduino at all, 50Hz is radiation from the mains power, I assume that your mains power is 50Hz.

Check the connections to the Hall Effect device and what is its spec number?

Hope to help.

Tom...... :slight_smile:

Sorry, I tried to say you help

Hi, the spec sheet in the sample circuits show a resistor from ic output to +supply, I can’t find any reference but it looks as though the output is open collector.
Try putting a 10K resistor between 5V and the output of the IC.
Shielding in the case of long lengths of output would be a good idea.

Tom… :slight_smile:

TomGeorge:
Hi, the spec sheet in the sample circuits show a resistor from ic output to +supply, I can't find any reference but it looks as though the output is open collector.
Try putting a 10K resistor between 5V and the output of the IC.
Shielding in the case of long lengths of output would be a good idea.

Tom... :slight_smile:

Tom.

You are right. It is a hall output´ sinking type, so it needs pullup.
Spec. sheet doesnt show any figure.
So I'll try with 10k as you recommend me.

Tomorrow (expect) I'll inform.

It is very confortable to work with people helping as you do.

Nice week

TomGeorge:
Hi, the spec sheet in the sample circuits show a resistor from ic output to +supply, I can't find any reference but it looks as though the output is open collector.
Try putting a 10K resistor between 5V and the output of the IC.
Shielding in the case of long lengths of output would be a good idea.

Tom... :slight_smile:

Yes, it is.

Door open. Now I can going on with my experiments.

Thank you very much.

And Honor to Nick Gammon.

Hi good to hear that things are working.

Glad to have helped.

Tom........ :slight_smile: