Interrupt Pins on ProMini

Dear All,

I'm now developing an IoT based on Arduino ProMini, Sorry I haven't found a specific category on ProMini, but I believe it is very similar with Arduino Nano.

The IoT is using two Hall-Effect Type Liquid Flow Sensors (AICHI OF05ZAT) and LoRa (RFM95W). It seems it's working for the flow sensors when I use hardware interrupt pins (pin 2 and pin 3). I haven't check the LoRa function, but I read in many references the LoRa need also to be connected to hardware interrupt pin (pin 2 or pin 3).

The alternatives:

  • LoRa uses hardware interrupt pin, e.g pin 2, while Flow Sensors use other pins configure as interrupt pins
  • Flow Sensors use hardware interrupt pins, while LoRa uses other pin as interrupt pin

Please suggest the best approach on this matter :pray:

Thank you

Mula W. Wangsaputra

Dear All,

Here is the sketch :point_down:

/* Emmision Monitoring IoT
 *  Mula W Wangsaputra
 *  YB1BUL
 *  10 Jun 2022
 */

#include <SPI.h>
#include <LoRa.h>

#define EngStsPin A1
#define ExtPwrPin A2
#define BatLevPin A3
#define FeedFFSPin 2
#define RetnFFSPin 3
#define ConnCheck 4
#define FuelTypePin0 8
#define FuelTypePin1 9

byte FeedSensInt=0;             //0=digital pin 2
byte RetnSensInt=1;             //1=digital pin 3

const long frequency=915E6;     //LoRa Frequency
const int csPin=10;             //LoRa radio chip select
const int resetPin=5;           //LoRa radio reset
const int irqPin=6;             //change for your board; must be a hardware interrupt pin

float fpc=0.46;                 //Feed Line Pulse Contstant in mL/p (mili-liter per pulse)
float rpc=0.46;                 //Return Line Pulse Contstant in mL/p (mili-liter per pulse)
                                //examples: 30 pulses/sec=1800 pulses/min=828 mL/min=0.828 L/m  


//Engine Status variables
float engsts,min_engsts,d_engsts;      
float v_min_engsts=7.0;                 //min_engsts=(R4)*V_real_engsts/(R3+R4), min_real_engsts=11V, min_engsts=2.62
float test_engsts,v_test_engsts=12.0;
float R3=15000;
float R4=4700;
//External Power level variables
float extpwr,d_extpwr;                  //extpwr=(R5+R6)*A2/R6, A2max=4.9V, extpwr_max=8.9V
float nom_extpwr=8.6;
float test_extpwr=8.7;
float R5=8200;
float R6=10000;
//Battery Level variables
float bl,d_bl,test_bl=8.4;               //bl=(R7+R8)*A3/R8, A2max=4.9V, bl_max=8.9V
float R7=8200;
float R8=10000;
//Fuel Type, Fuel Burnt and Run Time variables
int fueltype;
bool ft0,ft1;
//
int feedpulsecount,retnpulsecount;
float flowrate,qtyml,f_burntml,f_burnt;
float run_time,run_time_min;
int samper=5;
unsigned long oldTime;
//LoRa Package
String s_fuel_type,s_engsts,s_ext_pwr_sts,s_ext_pwr,s_bl,s_f_burnt,s_run_time,s_del_pack;
int txtime=0,txint=4;

void setup() 
{
  Serial.begin(9600);
  while (!Serial);

  pinMode(EngStsPin,INPUT);
  pinMode(ExtPwrPin,INPUT);
  pinMode(BatLevPin,INPUT);
  pinMode(FeedFFSPin, INPUT);
//  digitalWrite(FeedFFSPin, HIGH);
  pinMode(RetnFFSPin, INPUT);
//  digitalWrite(RetnFFSPin, HIGH);
  pinMode(ConnCheck,INPUT_PULLUP);
  pinMode(FuelTypePin0,INPUT_PULLUP);
  pinMode(FuelTypePin1,INPUT_PULLUP);
  
  feedpulsecount=0;
  retnpulsecount=0;
  flowrate      =0.0;
  qtyml         =0.0;
  f_burntml     =0.0;
  f_burnt       =0.0;
  run_time      =0.0;
  oldTime       =0;

  d_engsts=(5.0/1024)*(R3+R4)/R4;
  min_engsts=v_min_engsts*R4/(R3+R4);
  test_engsts=v_test_engsts*R4/(R3+R4);
  d_extpwr=(5.0/1024)*(R5+R6)/R6;
  d_bl=(5.0/1024)*(R7+R8)/R8;

  //___________________________Fuel Type__________________________________________________ 
   ft0=digitalRead(FuelTypePin0);
   ft1=digitalRead(FuelTypePin1);
   if(ft0==false && ft1==false)
   {
     fueltype=0;
     s_fuel_type=" FBD"; //Fuel: Bio Diesel
   }
   else
   {
     if(ft0==true && ft1==false)
     {
       fueltype=1;
       s_fuel_type=" FRD"; //Fuel: Regular Diesel
     }
     else
     { 
       if(ft0==false && ft1==true)
       {
         fueltype=2;
         s_fuel_type=" FPL"; //Fuel: Pertalite
       }
       else
       {
         if(ft0==true && ft1==true)
         {
           fueltype=3;
           s_fuel_type=" FPM"; //Fuel: Pertamax
         }
        }
       }
     }
//________________________________________________________________________________________
  
  attachInterrupt(FeedSensInt, FeedPulseCounter, FALLING);
  attachInterrupt(RetnSensInt, RetnPulseCounter, FALLING);

  LoRa.setPins(csPin, resetPin, irqPin);

  if (!LoRa.begin(frequency)) 
  {
  Serial.println("LoRa init failed. Check your connections.");
  while (true);
  }

  Serial.println("LoRa init succeeded.");
  Serial.println();
  Serial.println("LoRa Simple Node");
  Serial.println("Only receive messages from gateways");
  Serial.println("Tx: invertIQ disable");
  Serial.println("Rx: invertIQ enable");
  Serial.println();

  LoRa.onReceive(onReceive);
  LoRa.onTxDone(onTxDone);
  LoRa_rxMode();
}  


void loop() 
{ 
//__________________________________Engine Status Check________________________________
  engsts=analogRead(EngStsPin)*d_engsts; 
//  engsts=test_engsts;
  if(engsts>min_engsts)
   {
     s_engsts=" ER"; //Engine Running
   }
   else
   {
     s_engsts=" EI"; //Engine Idle
   }
//______________________________________________________________________________________

                    
  while(engsts>min_engsts) 
  {
    
// _______________External Power Check____________________    
    extpwr=analogRead(ExtPwrPin)*d_extpwr;
//    extpwr=test_extpwr;
    s_ext_pwr=String(extpwr,1);
    if(extpwr>nom_extpwr)
    {
      s_ext_pwr_sts=" EPG"; //External Power: Good
    }
    else
    {
      s_ext_pwr_sts=" EPL"; //External Power: Low
    }
//__________________________________________________________    

  
//________________Battery Level Check_____________________  
    bl=analogRead(BatLevPin)*d_bl;
//    bl=test_bl;
    s_bl=" BL"+String(bl,1); //Baterry Level
//________________________________________________________    


  if((millis()-oldTime)>(1000*samper))            // Only process counters once samper x second
   { 
    detachInterrupt(FeedSensInt);
    detachInterrupt(RetnSensInt);
    flowrate=((1000.0*samper)/(millis()-oldTime))*(feedpulsecount*fpc-retnpulsecount*rpc); //mili-Liter per second 
    oldTime=millis();    
    qtyml=flowrate*samper;                         //mili-Liter for (samper) seconds  
    f_burntml+=qtyml;
    f_burnt=f_burntml/1000;                        //Liters
    s_f_burnt="  FB"+String(f_burnt,2)+"L";
    run_time=run_time+samper;                      //seconds
    run_time_min=run_time/60;                      //minutes 
    s_run_time="  RT"+String(run_time_min,1)+"m";
    s_del_pack=s_fuel_type+s_engsts+s_ext_pwr_sts+s_ext_pwr+s_bl+s_f_burnt+s_run_time;    
    feedpulsecount=0;
    retnpulsecount=0;  
    attachInterrupt(FeedSensInt,FeedPulseCounter,FALLING);
    attachInterrupt(RetnSensInt,RetnPulseCounter,FALLING); 
    txtime=txtime+1;
   }

//___________________Send Monitoring Data Package via LoRa__________________
   if(txtime==txint)
    {
     LoRa_sendMessage(s_del_pack);                  //send a message 
//     delay(5000); 
     Serial.println(s_del_pack);         
     txtime=0;
    } 
//____________________________________________________________________________    

   engsts=0;
  }
 }


//_____________________Interupt for Sensor Pulse Counter________________________________
void FeedPulseCounter()
{
  feedpulsecount++;
}

void RetnPulseCounter()
{
  retnpulsecount++;
}


//__________________________________Interupt for LoRa___________________________
void LoRa_rxMode()
{
  LoRa.enableInvertIQ();                // active invert I and Q signals
  LoRa.receive();                       // set receive mode
}


void LoRa_txMode()
{
  LoRa.idle();                          // set standby mode
  LoRa.disableInvertIQ();               // normal mode
}

void LoRa_sendMessage(String message) 
{
  LoRa_txMode();                        // set tx mode
  LoRa.beginPacket();                   // start packet
  LoRa.print(message);                  // add payload
  LoRa.endPacket(true);                 // finish packet and send it
}

void onReceive(int packetSize) 
{
  String message = "";

  while (LoRa.available()) 
  {
    message += (char)LoRa.read();
  }

  Serial.print("Node Receive: ");
  Serial.println(message);
}

void onTxDone() 
{
  Serial.println("TxDone");
  LoRa_rxMode();
}

boolean runEvery(unsigned long interval)
{
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    return true;
  }
  return false;
}

You can use PCINT (Pin Change Interrupt) with such sensors.
Search the forum for sample code.

Dear DrDiettrich,

Thank you, so for the sensors I better use the PCINT? LoRa will use hardware interface pin 2 or 3.
PCINT can only detect "CHANGE" not "RISING", "FALLING" and "CHANGE" like the hardware interrupt. Maybe I should try what is the difference, maybe it counts the pulse twice per pulse.

Thanks

Mula

As I recall that LoRa library does require that the DIO0 pin is one capable of a hardware interrupt, so normally pin 2 or 3 on an ATmega328P.

If thats a problem there is a LoRa library that can have DIO0 on any of the ATmega pins that are free, or indeed can be used without DIO0 connectred at all.

Dear srnet,

Thank you for the Information. Yes I see some LoRa connection between DIO0 and pin 2 or 3, I also see some LoRa which they don't connect DIOx to Arduino. Actually what is the purpose of DIOx? Are they related to control the Node/IoT from the Gateway?

Thanks

Mula

If you want the detail on all the DIOx pins can be used for, read the SX127X datasheet.

DIO0 is the normal one used by the LoRa device to flag TXdone and RXdone.

Whats 'the Gateway' ?

Dear srnet,

OK thanks, maybe at least connect DIO0 to pin 2 or 3.

My plan is to gather fuel flow from Hall-Effect sensors and transmitted it via LoRa then the "Gateway" is also LoRa and ESP32-Wifi in receive mode, further the WiFi will be connected to internet, so the information from the remote sensors can be monitored on PC, smartphone, etc :grinning:

Thanks

Mula