0
votes

I'm currently trying to set up an Arduino Uno and a Hall Effect Sensor (A3144) to measure the RPM of a motor. I have tested the sensor, and it reads "0" when sensing the magnet, and "1" when the magnet is removed. I have also written code that I thought would work, but when I test my program, nothing appears on the Serial Monitor. If anyone has any ideas of how I can change my coding to get it to work, I would greatly appreciate some advice!

Here is my code:

int sensorPin = 2;            //Hall Effect Sensor at Digital Pin 2
float hall_thresh = 100.0;    //Set number of hall trips for RPM reading

void setup() {
  Serial.begin (115200);      // setup serial - diagnostics - port 
  pinMode (sensorPin, INPUT); // setup pins
}

void loop() {
  // preallocate values for tachometer
  float hall_count = 1.0;
  float start = micros();
  bool on_state = false;

  // counting the number of times the hall sensor is tripped
  while (true) {
    if (digitalRead(sensorPin) == 0) {
      if (on_state == false) {
        on_state = true;
        hall_count += 1.0;
      }
    } else {
      on_state = false;
    }
    if (hall_count >= hall_thresh) {
      break;
    }
  }
  // print information about Time and RPM
  float end_time = micros();
  float time_passed = ((end_time-start)/1000000.0);
  Serial.print("Time Passed: ");
  Serial.print(time_passed);
  Serial.println("s");
  float rpm_val = (hall_count / time_passed) * 60.0;
  Serial.print(rpm_val);
  Serial.println(" RPM");
  delay(1);                   // set delay in between reads for stability
}
2
Format your code properly. And why would you use floats for counters? Terrible idea.TomServo
If it never prints anything, it is probably because it is stuck in your while loop.ocrdu
Maybe add a Serial.print() call inside the while loop to see what it's doing?Pranav Hosangadi
Hi, I have direct experience with engine RPM sensors and using the Arduino as a DIY ECU. Please let me know what engine you are trying to read from and what the approximate MAX rpm would be. I have posted an answer that should get you going.edgar_wideman

2 Answers

0
votes

Here is a recreation of your code, do not hate me if it doesn't work first try, I just confirmed that it compiles and no debugging done. I have a very similar setup that is working as an engine control unit (ECU) operation on a Teensy 4.0, so obtaining RPM using a similar method is proven to work.

int sensorPin = 3; // interupt pin on UNO, adjust according to board.
unsigned long value = 0;
int hall_thresh = 100;    //Set number of hall trips for RPM reading

unsigned long startTime = 0;
void setup() {
  Serial.begin(115200);
  pinMode(sensorPin, INPUT);

  attachInterrupt(digitalPinToInterrupt(sensorPin), addValue, RISING);
}
void loop() {
  if (value >= hall_thresh) { // Math copied from StackOverflow question. 
    unsigned long endTime = micros();
    unsigned long time_passed = ((endTime - startTime) / 1000000.0);
    Serial.print("Time Passed: ");
    Serial.print(time_passed);
    Serial.println("s");
    float rpm_val = (value / time_passed) * 60.0;
    Serial.print(rpm_val);
    Serial.println(" RPM");
    value = 0;
    startTime = micros();
  }
}
void addValue() {
  value++;
}

Please post your problems if you have any and I will be happy to help with the debugging.

0
votes

i had the same problem but i used stm32f103c8t6 (but that is not important).

Problem is in "hall_thresh" variable. Since you start counting from number 1 (float hall_count = 1.0;) the while loop is waiting 99 times (99 times of putting the magnet near the sensor and away) then it will compare it to "hall_thresh".

If hall_count equals hall_thresh then the while loop will breaks and send time and RPM to serial monitor.

So try to change the hall_thresh variable to 10 or 5 and try it with magnet in your hand.

Next time when you use motor you will change the hall_thresh variable to higher number because motor is a lot faster.

Hope it helps :)