NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » reed switch/interrupts

August 06, 2010
by esoderberg
esoderberg's Avatar

I'm trying to use a reed switch and some mags to give a tach/speed reading.
I'm using a 328P chip with the input from the reed switch to PB0. The plan was to have a counter count up each rev of my wheel, and then just divide by the elapsed time to get RPM. This seemed straight forward but my counter seems to be incrementing by 7 each time the circuit is closed and PB0 goes high, and by one when it drops low again. Circuit closing is verified by LED. Any suggestions as to why ttach1 doesn't count up by ones using code below would be appreciated.

// This is what gets executed on interrupt for measuring frequency of reed switch input, set to trip with change to PB0

ISR(PCINT0_vect){

//Trigger on passing of mag by reed switch.  ie one rotation of wheel

                if((PINB & (1<<PB0))){

                ttach1++;}// if change in PB0 plus high ---  ie switch closed then increment tach

}

void init_tach(){

  // make PB0 input pin

  DDRB &= ~(1<<PB0);
  PORTB &=~(1<<PB0);

  //Enable Pin change interrupt
  PCICR |= (1<<PCIE0); // this enables interrupts on pins PCINT 7-0

 //set the mask on pin change interrupt 0 so that only PCINT0 (PB0) triggers interrupt

  PCMSK0 |=(1<<PCINT0);

 }

Looping in Main:

//loop through main 20 times then take tach count/total time through 20 loops(dt*ttach2) with dt unit time per loop

ttach2++;

if (ttach2>19) {v = ttach1*1.57/(dt*ttach2);// v = circumference * number of rev/ unit time

ttach2=0;
ttach1=0;//reset counter to zero
}
August 07, 2010
by Rick_S
Rick_S's Avatar

You may be seeing an effect called switch bounce. What happens is in the fraction of a second before the switch changes state from fully open to fully closed then back to open again, there is a 'gray' are where it briefly bounces between open & closed. This gives a device, like a microcontroller that senses a switch quickly, multiple triggers where you would expect one.

Adding debounce to your program could help. A better alternative would be to change the input device from a switch to a hall effect sensor or other non-mechacnial device.

I don't know what RPM the Wheel you are speaking of is expected to spin at but a switch will limit your ability as the mechanical action will eat up a bit of the time and if the magnetic field passes your reed too quickly it may not actuate at all. The solid state devices whether magnetic or light based have less issues like that.

Hope I gave you some food for thought,

Rick

August 07, 2010
by esoderberg
esoderberg's Avatar

Rick,

I think your supposition was correct. I put a filter on the counter to discount below a certain threshold and I now see the counter climb by ones for each mag pass. However, I'm going to also take your suggestion to use a hall switch because I don't think the reed switch will support a high enough frequency. Thanks for the feedback.

Eric

August 07, 2010
by mongo
mongo's Avatar

Something that you might want to keep in mind...

Most Hall Effect sensors only sense one pole of a magnet so make sure you have the right end facing it as it passes.

Post a Reply

Please log in to post a reply.

Did you know that a thermometer can be made "faster" by using a bit of math? Learn more...