2017년 8월 8일 화요일

유량센서 사용하기 - 인터럽트 이용

유량센서 작동방식이 홀센서가 내부에 있어서 1회전에 할때마다 트리거가 발생하는 원리이므로

트리거 신호를 인터럽트에 연결시켜서 회전수를 카운트 해서 유량을 계산하는 코드입니다.

신호선에는 10K 저항을 연결해서 풀업저항으로 만들어 줍니다. 

1회전시 유량은 유량센서 용량에 따라 다르므로 스펙에 맞게 공식을 수정해야 할 것 같습니다.

제가 구한 유량센서는 The frequency calculation = constant flow rate (L/min) 7.5 * unit time (in seconds)

분당 7.5 리터니깐 초당 계산하니깐 60을 더 곱해줍니다.

인터럽트를 사용하니 SoftSerial은 사용할 수 없을것 같네요. 같이 인터럽트를 사용하니 WiFi을 연결하려면

하드웨어 시리얼을  이용해야 할 듯 합니다.

flow meter arduino

아래 사이트를 코드를 참고하였습니다.


// Reading liquid flow rate using an Arduino 
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn@thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com 

volatile int NbTopsFan;            //measuring the rising edges of the signal
int Calc; 
int hallsensor = 2;                //The pin location of the sensor

void rpm ()                        //This is the function that the interupt calls
{
  NbTopsFan++;                     //This function measures the rising and falling edge of signal
}                                  //The setup() method runs once, when the sketch starts

void setup() 

{
 pinMode(hallsensor, INPUT);       //initializes digital pin 2 as an input
 Serial.begin(9600);               //This is the setup function where the serial port is initialised
 attachInterrupt(0, rpm, RISING);  //and the interrupt is attached
}
                                   // the loop() method runs over and over again
                                   // as long as the Arduino has power
void loop () 
{
 NbTopsFan = 0;                    //Set NbTops to 0 ready for calculations
 sei();                            //Enables interrupts
 delay (1000);                     //Wait 1 second
 cli();                            //Disable interrupts
 Calc = (NbTopsFan * 60 / 7.5);    //(Pulse frequency x 60) / 7.5Q = flow rate in L/hour
 Serial.print (Calc, DEC);         //Prints the number calculated above
 Serial.print (" L/hour\r\n");     //Prints "L/hour" and returns a new line
}

댓글 없음:

댓글 쓰기