hall effect tachometer problem

Hello.
I have been working on an automatic magnetic coil pickup winding machine the last while. I've got an AC sewing machine motor with speed being controlled by a 5v servo whose shaft is mechanically coupled to a potentiometer on a variac. I've a hall effect sensor stationed on the side of the machine and a magnet on the fly wheel for counting rpm's.

I am using code from here Arduino Playground - ReadingRPM
but when I run it, the serial monitor spits out about 10 numbers then stops. Can anyone point me in the right direction as to why this is happening? I'm using a Mega board with a hall effect sensor plugged in at pin 18 (interrupt 5).

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

volatile int rpmcount;
 unsigned int rpm;

 unsigned long timeold;
//++++++++++++++++++++++++
int sensorValue = 0; 
const int analoginPin = A8;  // Analog input pin that the main speed control potentiometer is attached to
int relay = 19;         //relay on digital pin 19


 void setup()
 {
     digitalWrite(relay, HIGH);
    //RELAY
  pinMode(relay, OUTPUT);

  // Main Speed Pot
  pinMode(analoginPin, INPUT);
  //STEPPER

  myservo.attach(10);  // attaches the servo on pin 10 to the servo object 
   Serial.begin(9600);
   attachInterrupt(5, rpm_fun, RISING);

   rpmcount = 0;
   rpm = 0;
   timeold = 0;
 }

 void loop()
 {
    
rpmCal();
  
   sensorValue = analogRead(analoginPin);            // reads the value of the potentiometer (value between 0 and 1023) 
  sensorValue = map(sensorValue, 1023, 0, 20, 200);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(sensorValue);  
 }

void rpmCal(){
    if (rpmcount >= 20) { 
     //Update RPM every 20 counts, increase this for better RPM resolution,
     //decrease for faster update
     rpm = 30*1000/(millis() - timeold)*rpmcount;
     timeold = millis();
     rpmcount = 0;
     Serial.println(rpm);

   } 
}
 void rpm_fun()
 {
   rpmcount++;// this code will be executed every time the interrupt 5 (pin18) gets low.
  
 }

I just tried the same code with only the arduino on (the sewing machine motor has an independent switch). It works fine when I move the wheel by hand, as you would expect it to, but when I turn on the mains the serial monitor stops.

Could this be an interference issue?

Just as a matter of interest, and probably not germane to your problem, I read on the forum that it's better to use a falling edge with the interrupt pin pulled high. I made these changes to that code recently, when I measured the speed of a cpu fan using its third wire.

   pinMode(2, INPUT_PULLUP);
   //attachInterrupt(0, rpm_fun, RISING);
   attachInterrupt(0, rpm_fun, FALLING);

by the way I'm using a A3144 sensor with pullup resistor. Do I need a filtering capacitor in there somewhere?

@ Jimbo I tried your code, which does work when mains power is off, but not with it on.

ok so I've taken out the 10k pullup resistor and am now using the internal pullup as Jimbo used in the above code.

Still not doing anything once I turn on the mains power though. Being able to calculate the RPM is crucial to this project as I need to be able to determine the speed of another motor and count how many winds have gone onto the pickup with it. Any help or suggestions welcome.

Are interrupts prone to odd behaviour from EMI?

Are interrupts prone to odd behaviour from EMI?

No, but Arduinos are. And external wiring connected to the Arduino acts like an antenna, picking up all kinds of shit.

Well as I said, I didn't think what I said was necessarily germane. But that said, with a pullup- either internal or external- shouldn't one check for the FALLING edge?

EDIT- As suggested here by retrolefty

@PaulS, ok...if I shielded the Mega inside a metal box might that mitigate EMI a bit?

might that mitigate EMI a bit?

"A bit" and "enough" might not be the same thing.

I've already enclosed the arduino and everything inside an enclosure. Having to rehouse the arduino in a box is going to seriously mess up my design so I'm looking for any sort of alternatives or other causes before I have to unplug everything, extend cables etc.....

I was originally going to use an IR LED transmitter and receiver pair as a tachometer but came across the much simpler hall effect sensor option. I've got practically everything else in this build working and in place.

There's a switch mode power supply providing 12v to a servo and 5v via a 7805 regulator. Tomorrow I'm going to test the hall effect sensor with the PSU taken out of the circuit to see if it's the source; the mains motor is powered via a relay, which was powered off for part of my unsuccessful test making me think perhaps it is not the cause.

As with every single step of this process, huge learning coupled with huge pain in the ass and treading water....

Hello all,

I have also a problem with my hall sensor interrupt.
I am working on a project to monitor/automate my boat.

For that, I want to measure the diesel RPM. I use a hall sensor (TLE4905), which is unipolar.
The problem is that my interrupt isn't working properly on my DUE. I created an falling interrupt which increases a counter.

For testing purposes, I only want to see the number of counts, so when I move my magnet along the sensor (input goes from high to low) , I expect the counter increments by 1, am I correct?
That's not the case, it increments with a random value.
What is wrong, or should I use a latching hall sensor (us1881) and 2 magnets to operate properly?

The code:

volatile int rpmcount = 0;

void setup() {

pinMode(0, INPUT);
digitalWrite(0, HIGH); //use internal pull-up
attachInterrupt(0, rpm, FALLING);
Serial.begin(9600);
}

void loop() {

Serial.println(rpmcount);

}

void rpm() {

rpmcount++;
}

The reason often cited for putting the { on the line with the statement is that it requires fewer lines of code. That makes no sense when you then follow the statement with the curly brace with a blank line.

There
is
no
good
reason
to
put
all
your
statements
in
column
one.

pinMode(0, INPUT);
digitalWrite(0, HIGH); //use internal pull-up

On the serial port pin? Why?

attachInterrupt(0, rpm, FALLING);

Which physical pin are you connecting the sensor to?

Hello Paul, thanx for the quick reply and the coding tip.

I have an arduino due, I thought that every pin could be used for an interrupt.
The sensor is connected to pin 0 (yes, that is RX0).

The first argument to attachInterrupt() is the interrupt number. That is NOT the same as the pin number. You need to look up, for your Arduino, which pin to use for interrupt 0. Usually, that is pin 2, not pin 0.

Thanx, but i looked at the info from attachInterrupt and it says:
The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all available pins. You can directly specify the pin number in attachInterrupt().

So what would go wrong?

So what would go wrong?

Then, the question becomes Do you want to use pin 0 for Serial or for the interrupt? Both is not the right answer.

I want to use pin 0 for the interrupt.

The hall sensor has an hysteresis. So I don't know why the interrupt isn't incrementing correctly.
A reed switch would bounce, so that's the reason to choose a hall sensor.

I want to use pin 0 for the interrupt.

Then, you won't be able to use it for Serial. So, remove your call to Serial.begin() and all your Serial.print() statements.

The hall sensor has an hysteresis. So I don't know why the interrupt isn't incrementing correctly.
A reed switch would bounce, so that's the reason to choose a hall sensor.

That is certainly not true of all hall sensors. Many are quite noisy, and bounce just like switches, especially when the transition is very slow. If you have an oscilloscope, take a look at the sensor output. I'll bet you see lots of spikes during a slow transition. I have, and gave up on using hall sensors in one project!

jremington:
That is certainly not true of all hall sensors. Many are quite noisy, and bounce just like switches, especially when the transition is very slow. If you have an oscilloscope, take a look at the sensor output. I'll bet you see lots of spikes during a slow transition. I have, and gave up on using hall sensors in one project!

That's new for me! I don't have a scope so could not check it.
Are there good hall sensors? like the US1881 latching with smitt trigger?