'YF-S201'에 해당되는 글 1건
- 2017.06.28 Hardware | YF-S201 water flow sensor 가지고 놀기 4
1. 이런 센서도 있네?
향후 집도 짓고 배관 시설도 공사도 해야 해서 "Water Flow Sensor" 를 익힐 필요가 있습니다.
...라는건 뻥이구요 (집 짓는게 장래 희망은 사실), Arduino 로 할 수 있는 센서는 어떤게 있을까 찾아 봤습니다.
"Water Flow Sensor" 라는게 있네요.
쓰임새는 액체가 흐르는 파이프에 설치하여 흐르는 양을 검출할 수 있는 센서 입니다.
요런거죠.
2. 구입
역시 Aliexpress 에서 구입 했습니다.
없는게 없죠?
3천원 미만으로 무료 배송이면 괜찮츄?
3. 도착 및 분해
도착 기념 샷 입니다.
구성은 단순하네요.
뒷면을 보니 흐르는 방향이 표시되어 있습니다.
내부가 궁금해 졌습니다.
나사 4개가 너무 눈에 띄게 만들어 졌네요. 분리해 봅니다.
물이 들어갈까봐 고무 패킹에 잘 둘러쌓여 있습니다.
호스 부분은 프로펠러가 달려 있어서 회전하게 되어 있습니다.
회전축에 자석같은게 붙어 있어서, 이 부분이 상판 회로의 센서와 hall effect 를 검출하는 것 같습니다.
상판 회로는 잘 분리되어 있습니다.
4. Layout
Pin 연결은 다음과 같습니다.
YF-S201 | Arduino Nano ------------------------- Red | 5V Black | GND Yellow | D2 -------------------------
Hall effect 를 이용한 센서라고 하는데, 전자기를 이용한 방식인 듯 합니다.
그래서 그런지 digital 단자와 연결됩니다.
Hall effect 는 다음 Wikipedia 를 참고해 보세요.
- https://en.wikipedia.org/wiki/Hall_effect
5. Source
Code 는 다음과 같습니다.
출처는 아래 link 입니다.
- https://diyhacking.com/arduino-flow-rate-sensor/
Code 소스는 아래 link 입니다.
- http://diyhacking.com/projects/FlowMeterDIY.ino
/*
Liquid flow rate sensor -DIYhacking.com Arvind Sanjeev
Measure the liquid/water flow rate using this code.
Connect Vcc and Gnd of sensor to arduino, and the
signal line to arduino digital pin 2.
*/
byte statusLed = 13;
byte sensorInterrupt = 0; // 0 = digital pin 2
byte sensorPin = 2;
// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
void setup()
{
// Initialize a serial connection for reporting values to the host
Serial.begin(38400);
// Set up the status LED line as an output
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH); // We have an active-low LED attached
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
// The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
// Configured to trigger on a FALLING state change (transition from HIGH
// state to LOW state)
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
/**
* Main program loop
*/
void loop()
{
if((millis() - oldTime) > 1000) // Only process counters once per second
{
// Disable the interrupt while calculating flow rate and sending the value to
// the host
detachInterrupt(sensorInterrupt);
// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
// Note the time this processing pass was executed. Note that because we've
// disabled interrupts the millis() function won't actually be incrementing right
// at this point, but it will still return the value it was set to just before
// interrupts went away.
oldTime = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
unsigned int frac;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print("."); // Print the decimal point
// Determine the fractional part. The 10 multiplier gives us 1 decimal place.
frac = (flowRate - int(flowRate)) * 10;
Serial.print(frac, DEC) ; // Print the fractional part of the variable
Serial.print("L/min");
// Print the number of litres flowed in this second
Serial.print(" Current Liquid Flowing: "); // Output separator
Serial.print(flowMilliLitres);
Serial.print("mL/Sec");
// Print the cumulative total of litres flowed since starting
Serial.print(" Output Liquid Quantity: "); // Output separator
Serial.print(totalMilliLitres);
Serial.println("mL");
// Reset the pulse counter so we can start incrementing again
pulseCount = 0;
// Enable the interrupt again now that we've finished sending output
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
}
/*
Insterrupt Service Routine
*/
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}
6. 측정
당장 물 호스를 연결할 수가 없어, 입으로 바람을 불어 봅니다.
잘 되네요 !!!
Serial Monitor 로 결과를 보면 아래와 같습니다.
신기하게 잘 잡힙니다.
Flow rate / 흐르는 양 / 총량을 계산해 줍니다.
원작자가 소스코드를 잘 짜신것 같습니다.
FIN
이제 집만 지으면 될 것 같습니다.
'Hardware' 카테고리의 다른 글
| Hardware | 마이크로소프트 무선 마우스 4000 분해해보기 (0) | 2017.07.04 |
|---|---|
| Hardware | 금속탐지기를 조립해 보자 (0) | 2017.07.03 |
| Hardware | Arduino 로 Photoresister 가지고 놀기 - 1 (0) | 2017.06.25 |
| Hardware | 인비오 IPC-7080 청소기 (0) | 2017.06.23 |
| Hardware | MeeGoPad 히트싱크 업그레이드 하기 - 2 (0) | 2017.06.20 |


