Hello Everyone,
I am needing some assistance with a project that I am working on for school. For one aspect of it I am trying to make an automated process for recording a DC motor's speed. There are quite a few projects that I have found online similar to this and they all seem to work for the individuals well. However, I seem to be getting some pretty weird recordings. When data is being displayed on the 16x2 LCD it has a very large fluctuation. For instance, with 3.3v supplied to the small dc motor (with attached 1 blade prop) it will produce values close to 4000. But, it will soon begin to show data ranging in 4000 +- 500 RPM. Can anyone help me by giving some recommendations / guidance for possible features to add to my circuit or code that will alleviate this issue that I am encountering? I am not sure if these errors are being produced due to noise in the system or if I am missing something that should be implemented. Below, I have attached an image of my current circuit as well as the code in my Arduino Uno R3. Also, it is important to note the the IR sensor in use is a " IR Infrared Obstacle Avoidance Sensor Module 3-Wire Reflective Photoelectric Sensor Module".
Thanks
p.s. I am very new to this if that wasn't obvious already.
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7); //defining lcd pins
const int dataIN = 2; //IR sensor INPUT
unsigned long prevmillis; // To store time
unsigned long duration; // To store time difference
unsigned long lcdrefresh; // To store time for lcd to refresh
int rpm; // RPM value
boolean currentstate; // Current state of IR input scan
boolean prevstate; // State of IR sensor in previous scan
void setup()
{
pinMode(dataIN,INPUT);
lcd.begin(16,2);
prevmillis = 0;
prevstate = LOW;
}
void loop()
{
// RPM Measurement
currentstate = digitalRead(dataIN); // Read IR sensor state
if( prevstate != currentstate) // If there is change in input
{
if( currentstate == HIGH ) // If input only changes from LOW to HIGH
{
duration = ( micros() - prevmillis ); // Time difference between revolution in microsecond
rpm = (60000000/duration); // rpm = (1/ time millis)10001000*60;
prevmillis = micros(); // store time for next revolution calculation
}
}
prevstate = currentstate; // store this scan (prev scan) data for next scan
// LCD Display
if( ( millis()-lcdrefresh ) >= 3000 )
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Speed of Motor");
lcd.setCursor(0,1);
lcd.print("RPM = ");
lcd.print(rpm);
lcdrefresh = millis();
}
}
