Do we need to change anything in the code to replace ir sensor with hall sensor

Good morning,
Im new in arduino and I am making a bike speedometer. I saw a video to get some help making it but he used IR sensor. IR sensors can be disturbed at sunlight so i decided to use hall sensor so it is more accurate. So, do I have to change anything in the code mentioned below?


#include <LiquidCrystal.h>
#include <TimerOne.h>

const int rs = 8, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int IRSensorPin = 2; 
const int ledPin = 13;

int inputState;
int lastInputState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 5;

long time;
long endTime;
long startTime;
int RPM = 0;
double trip = 0;
double kkbanspd = 0.00223;
float lnTime = 0;
 int spff = (RPM * kkbanspd) * 60;
void setup(void) {
  pinMode(IRSensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("Hive");
  lcd.setCursor(0, 1);
  lcd.print("Industries");
  delay(2000);
  endTime = 0;
  Timer1.initialize(1000000);  // Set the timer to 60 rpm, 1,000,000 microseconds (1 second)
  Timer1.attachInterrupt(timerIsr);  // Attach the service routine here
  int spff = (RPM * kkbanspd) * 60;

}

// ---------------------------------------------------------------
void loop(void) {
  time = millis();
  int currentSwitchState = digitalRead(IRSensorPin);

  if (currentSwitchState != lastInputState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (currentSwitchState != inputState) {
      inputState = currentSwitchState;
      if (inputState == LOW) {
        digitalWrite(ledPin, LOW);
        calculateRPM(); // Real RPM from sensor
      }
      else {
        digitalWrite(ledPin, HIGH);
      }
    }
  }
  lastInputState = currentSwitchState;
}

// ---------------------------------------------------------------
void calculateRPM() {
  startTime = lastDebounceTime;
  lnTime = startTime - endTime;
  RPM = 60000 / (startTime - endTime);
  endTime = startTime;
  trip++;
}

void timerIsr()
{
  // Print RPM every second
  // RPM based on timer
  Serial.println("---------------");
  time = millis() / 1000;
  Serial.print(time);
  Serial.print(" RPM: ");
  Serial.println(RPM);
  Serial.print(spff);

  

  

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Spd:");
  lcd.setCursor(5, 0);
  lcd.print((RPM * kkbanspd) * 60);
  lcd.setCursor(12, 0);
  lcd.print("Km/h");
  Serial.print((RPM * kkbanspd) * 60);
  lcd.setCursor(0, 1);
  lcd.print("Trp:");
  lcd.setCursor(5, 1);
  lcd.print(trip * kkbanspd);
  lcd.setCursor(12, 1);
  lcd.print("Km");
  delay(500);
  RPM = 0;
}

Thanks in advance :slight_smile:

in your code the IRSensorPin was just set as an INPUT. Do you know if there was an external pull-up or pull-down resistor?

does your hall sensor need one?

Just select a video/code already using a Hall sensor.

https://www.google.com/search?q=Arduino+speedometer+hall-effect

No, in the circuit there is no resister excluding for the backlight of the screen. So can i directly connect hall sensor without editing any code?

Thankyou

Probably. The beauty of Arduino is it's pretty easy to find out by testing.

Actually the thing is i don't have the hall sensor right now so that is why am i asking. :slight_smile:

Hi,
Have you ordered it ?
If so can you post link to data/specs please?

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

I have not ordered yet but which one should i order. https://amzn.eu/d/3yrWTmI this type or https://amzn.eu/d/hlrHZ2j this type

Thanks

The ky-035 is probably easier to use: it's the same a3144 sensor as the other link but mounted on the small board with pins for ease of implementation. Looks like it has an led on there too so you can see what the sensor says without even reading the pin.

If you are going to mount components on your own boards then the loose one would probably be better.

But the one on the board is almost 10x more expensive as I'm sure you noticed: 280/1 vs 155/5 (=31/1).

I'd suggest you get these

and something like this, which you can cut and use to make mounting easier.

https://www.amazon.in/s?k=veroboard&i=industrial&crid=2MX25GGPI1&sprefix=veroboard%2Cindustrial%2C72&ref=nb_sb_noss_2

Thankyou guys for your suggestions :slight_smile:

so let's come back to the real question. Do i really need to change the code after installing hall sensor?
thanks

you might have to change LOW to HIGH depending on you physical setup

with your IR setup, may be light was going through all the time (HIGH) and only a small obstacle was on the way when the rotation was complete (LOW)

when the magnet will be in front of the sensor and that will trigger a HIGH, rest of the time it will be LOW

➜ basically the code should work, give or take minor adaptation on the detection signal

Won't really know till it has been tried.

Thanks. Ill try once i get the sensor.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.