Fastest, Simplest, and Easiest Hall Effect Tachometer

This is one of my first Arduino projects, so please let me know if there is something that I did poorly, or that can be improved. I got the base code from this project I found, and adapted it for use with a simple hall effect tachometer. Fast Reading Tachometer!!! YAY! This is designed to work with any rotating object that can have a magnet attached to it. I am using it on a lawnmower engine, which already has a magnet inside the flywheel that works perfectly. It appears that this code can be used in conjunction with other statements inside the loop, since adding a delay doesn't appear to have a negative effect on the project. Here is the code, it's pretty straightforward, just connect the lcd display however you like it, I attached it to different pins than typical. This was done on a Mega 2560 board made by Elegoo. The program calculates rpm based on the amount of time between detecting a magnet, which seems to be much much faster and more accurate than the common method of counting pulses and dividing by a fixed amount of time. To my knowledge, there is no other hall effect tach program for Arduino that uses the same method, so I decided to share this, hoping to help others who are looking for a responsive, accurate tachometer using a magnetic sensor and an Arduino board. Thank you for reading, and I look forward to hearing your feedback. If there is something that can be done better another way, please don't hold back. I would like to learn how to code better.
Edit: Hall effect sensor I used was purchased on amazon, and has the numbers 44e and 402 on it.
Amazon describes it as 3144E A3144.

I will post a circuit diagram later.

#include <LiquidCrystal.h> 
int Contrast=100;
LiquidCrystal lcd(40,41,42,43,44,45,46);  

int Pin = 2; // Hall sensor at pin 2

unsigned long rpm = 0;
unsigned long duration;

void setup() {
  pinMode(Pin, INPUT); //Sets sensor as input
  analogWrite(6,Contrast);
  lcd.begin(16, 2);
}
void loop()
{
  duration = pulseIn(Pin, FALLING, 500000); //times the amount of microseconds the motor is not timing IR, Times out after 100000 uS. Raise the timeout for slower RPM readings. .5 second
  rpm = 60000.0/duration*1000; //See above
 
  lcd.clear();  
  lcd.print("Engine RPM ");
  lcd.print(rpm);
}

Hi, @timjones
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Thanks for posting your project.
You need to put your code in code tags as shown in the above link.
A circuit diagram would be the best, rather than describing your circuit with words.
Also a link to data/specs of your HallEffect device.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thanks, I fixed the code problem, and attached the hall effect sensor info. I will draw up a circuit diagram later, and update as I continue to test and improve it.