IR sensor (for RPM) and HX711 (Load cell for Weight)

Hello there. I am building a dynamometer. I am using IR sensor for measuring rpm and HX711 load cell to measure the weight. Indivisually both of them are working great, but when i combine the code the IR sensor starts malfunctioning. It is not giving accurate rpm values. I have seen people posting similar problem in the forum before but i couldnt get proper solution. So if you have any way to fix the code, do help me.

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Arduino.h>
#include "HX711.h"

LiquidCrystal_I2C lcd(0x27,20,4);  //defining lcd pins

const int dataIN = 2; //IR sensor INPUT

unsigned long prevmillis = 0; // To store time
unsigned long duration; // To store time difference
unsigned long lcdrefresh; // To store time for lcd to refresh
unsigned long 

int rpm; // RPM value

bool currentstate ; // Current state of IR input scan
bool prevstate = LOW; // State of IR sensor in previous scan

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 8;
const int LOADCELL_SCK_PIN = 9;
HX711 scale;

void setup()
{
  Serial.begin(57600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(-143.15);            // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();

  pinMode(dataIN,INPUT);
  
  lcd.init();
  lcd.backlight();     
 
}

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)*1000*1000*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 ) >= 100 )
    {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Speed of Motor");
      lcd.setCursor(0,1);
      lcd.print("RPM = ");
      lcd.print(rpm);  
      Serial.print("Rpm: ");
      Serial.println(rpm);       
      lcdrefresh = millis();   
    }

  lcd.setCursor(1,2);
  lcd.print("Weight: ");
  Serial.print("Weight");
  Serial.println(scale.get_units());
  lcd.setCursor(8,3);
  lcd.print(scale.get_units(),1);

}

Did you ever get this figured out?

Yes i figured it out.
The IR sensor and HX711 wont work together due to different processing time. So i Used two arduino boards to run the programs seperately and then broght the outputs together by I2C connection of the boards.

@purnasish
Glad you came up with a solution. I had used the PULSEIN command with an IR sensor trying to collect RPM data. That command will detect the state change of the sensor and then track the time until the next state change. But I had problems also, although I think they might have been related to stray light or an inconsistent aperture. I don't really know. In the end, I switched to a hall effect sensor, also using the PULSEIN command. The Hall and PULSEIN seem to work pretty consistently together.
Lloyd-ss

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