Been working on a data logging application lately with the intentions of recording my biking sessions and displaying them on a mapping API. The general idea is to use the Hall sensor common in many RPM applications to record revolutions to the EEPROM onboard the Arduino.
I’m having a few issues getting the readings I need, namely the revolutions are not counting with any consistency. I think the issue is with the way Arduino loops through the script. Below is the first iteration of the code I’ve written.
NOTE: Script updated August 31, with Blalor’s suggestions. The RPM reading now works with a high rate of speed.
int pinHall = 2; //Hall Sensor int pinLed = 13; //Indicator LED int time = 10000; //EEPROM Write Interval volatile int rpm = 0; //RPM Counter void setup() { pinMode(pinHall, INPUT); pinMode(pinLed, OUTPUT); Serial.begin(9600); } void loop() { //Write to EEPROM at specified interval if ((millis() % time) == 0) writeEEPROM(); //If Hall Sensor triggered then init the test attachInterrupt(1, incrementRPM, FALLING); } void incrementRPM() { rpm++; digitalWrite(pinLed, HIGH); Serial.print("RPM: "); Serial.println(rpm); } void writeEEPROM() { noInterrupts(); Serial.print("EEPROM: "); Serial.println(rpm); rpm = 0; interrupts(); }
It appears to me like the checkReading() method isn’t firing with any consistency and therefore I’m not incrementing rpm properly. The writeEEPROM() method is firing on target, however I worry that the writing process may be delaying the rpm count.
Below is a Fritzing illustration of the circuit I’ve wired up:
Suggestions? I’m open to any hints someone might have on how to structure this a little differently.
2 Comments
I think I would try an interrupt-driven approach, personally. Seems to me that if the RPM is sufficiently high, you’ll miss the high state.
Thanks Blalor, that pointed me in the right direction.
Replaced: if (digitalRead(pinHall) == HIGH) checkReading();
With: attachInterrupt(1, incrementRPM, FALLING);
And “int rpm” with “volatile int rpm”
Seems to be working perfectly now.
One Trackback/Pingback
[…] in the right position you wouldn’t pick up the digital I/O change. The solution, offered by Blalor, was to use the Arduino Interrupt commands. The Interrupt fires whenever a digital I/O signal is […]
Post a Comment