Difficulty setting up hall effect sensor

Hello everyone,

I am working off a simple tutorial from DIY Hacking (https://diyhacking.com/arduino-hall-effect-sensor-tutorial/) and I have had some trouble getting the whole loop to work. For reference I am not wiring or Arduino genius so bear with me if I am missing something obvious.

Here is my code:
/*
Arduino Hall Effect Sensor Project
by Arvind Sanjeev
Please check out http://diyhacking.com for the tutorial of this project.
DIY Hacking
*/
volatile byte half_revolutions;
unsigned int rpm;
unsigned long timeold;
void setup()
{
** Serial.begin(115200);**
** attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)**
** half_revolutions = 0;**
** rpm = 0;**
** timeold = 0;**
}
void loop()//Measure RPM
{
** if (half_revolutions >= 20) {**
rpm = 30*1000/(millis() - timeold)half_revolutions;
** timeold = millis();
*
** half_revolutions = 0;**
** //Serial.println(rpm,DEC);**
** }**
}
void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
{
** half_revolutions++;**
** Serial.println(“detect”);**
}

That is the exact code from the site so there shouldn’t be any problems with that.

The attached photo is my setup. It is wired the same way as in the tutorial but when I place a strong magnet nearby the sensor there is no “detect” string in the serial monitor on the 115200 baud and I am not sure why. Any thoughts?

Just a sugggestion but using code tags would be better for sketches as sometimes the forum munges code without them ( </> )

Also the little chain link icon allows you to post real links.

Thats a verrry old tutorial btw

This one has some issues but it is those issues that may help you a little if you read the comments.

As for your attached pic I dont see it ?

The code compiles fine so it is either the incorret type of hall effect or a wiring issue from what you have given us so far.

I'm not sure if this could be the problem, but did you set the baud rate in the Monitor window to the same 115200 baud (bottom right corner)?

Karma MSX I should have included that off the bat.

Hi,

On the SAMD21 based arduino board, the first parameter to attachInterrupt() is the pin number, not the interrupt number.

Replace:

  attachInterrupt(0, magnet_detect, RISING); //this is for pin 0

With the following :

  attachInterrupt(2, magnet_detect, RISING); //this is for pin 2

For maximum compatibility between boards, use the recommended syntax :

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

More info here : attachInterrupt() - Arduino Reference