오렌지 보드를 사용하여 토양 수분센서로 수분량 측정하기

 
[컴퓨터월드] 아두이노는 이탈리어로 ‘친한 친구’라는 뜻을 가진 대표적인 오픈소스 하드웨어다. 딱딱하고 접근하기 힘들었던 임베디드 분야를 누구나 쉽게 접근할 수 있도록 만든 미니 기판이라 할 수 있다. 이번 강좌에 사용하는 아두이노 오렌지보드는 한국형 아두이노라 할 수 있다.

아두이노의 보급은 오픈소스 하드웨어의 확산을 불러일으켰고 메이커 문화의 확산에도 큰 기여를 했다. 최근에는 인텔, 마이크로소프트 등 대형 기업들도 이런 오픈소스 하드웨어 시장에 뛰어들기 시작했다. 그 만큼 오픈소스 하드웨어 시장의 잠재력이 커졌다는 얘기이다.


토양 수분센서란 무엇인가?

토양 수분센서는 화분이나 땅의 흙에 직접 꽂아서 토양의 습도가 어느 정도인지 판단할 때 쓰는 센서이다. 정확하게는 토양 내 수분함량에 따른 저항의 변화를 측정하는 센서이다. 토양 내 수분과 토양을 구성하는 입자의 크기 및 다양성에 영향을 받으며, 토양 내 수분함량이 많으면 저항값이 작아지고, 수분함량이 적으면 저항값이 커진다. 그러나 토양 내 수분함량이 매우 많을 때는 전기저항이 둔감하여 오차가 크다는 단점을 가지고 있기도 하다.

 

 

토양 수분센서 사용방법

토양 수분센서는 토양에 직접 꽂는 센서 단자와 센서 보드, 그리고 케이블로 구성돼 있다. 센서 단자의 2개 핀과 센서 보드의 2개 핀을 서로 연결한다. (+/-는 구분 안해도 된다.) 그 다음 케이블을 센서 보드의 DO 핀을 제외한 3개의 핀 (ACC, GND, AO)을 연결한다.

DO핀은 디지털 출력으로 0과 1만을 출력하며, AO핀은 아날로그 출력으로 토양 내 수분의 양을 보다 상세하게 출력한다. 또한, 센서보드의 파란색 가변 저항을 돌려서 기준값을 정해놓고 사용할 수 있다. 이 글의 예제에서는 AO핀을 사용하여 아날로그 값을 확인한다. 측정된 값은 측정한 위치의 수분량을 나타내는 것이 아니라, 수분량에 따른 저항의 변화를 0~1023의 ADC 범위내에서 환산된 결과이다.

 

 


필요한 부품 목록
오렌지보드로 토양 수분센서를 제어해보기 위한 준비물은 아래와 같다.

 

 

하드웨어 연결하기

1. 오렌지보드의 5V핀을 브레드보드의 +버스에 연결한다.
2. 오렌지보드의 GND핀을 브레드보드의 -버스에 연결한다.
3. 피에조 부저를 그림과 같이 +단자가 위로 향하게 하여 세로로 꽂는다.
4. 피에조 부저의 +단자를 오렌지보드의 A2번 핀에 연결한다.
5. 피에조 부저의 -단자를 -버스에 연결한다.
6. 토양 수분센서의 VCC단자를 +버스에 연결한다.
7. 토양 수분센서의 AO단자를 오렌지보드의 A0번 핀에 연결한다.
8. 토양 수분센서의 GND단자를 -버스에 연결한다.

 


전자 회로도

 
 

 

소스코드

/*
제목 : 토양 수분센서로 수분량 측정하기
내용 : 토양 수분센서로 토양에 있는 수분량을 측정하고, 일정 수분량 이하면 소리를 내도록 만들어 봅니다.
*/
// 토양 수분센서를 A0번 핀으로 설정합니다.
int soil = A0;
// 피에조 부저를 A2번 핀으로 설정합니다.
int piezo = A2;
// 음계 표준 주파수(4옥타브) : 도, 레, 미, 파, 솔, 라, 시, 도
// 음계표를 참고하여 원하는 음계로 설정해도 됩니다.
int tones[] = {261, 294, 330, 349, 392, 440, 494, 523};
// 실행시 가장 먼저 호출되는 함수이며, 최초 1회만 실행됩니다.
// 변수를 선언하거나 초기화를 위한 코드를 포함합니다.
void setup() {
// 토양 수분센서의 동작 상태를 확인하기 위하여 시리얼 통신을 설정합니다. (전송속도 9600bps)
// 메뉴 Tool -> Serial Monitor 클릭
Serial.begin(9600);
// 피에조 부저가 연결된 핀을 OUTPUT으로 설정합니다.
pinMode(piezo, OUTPUT);
}

// setup() 함수가 호출된 이후, loop() 함수가 호출되며,
// 블록 안의 코드를 무한히 반복 실행됩니다.
void loop() {
// 토양 수분센서로부터 측정된 값을 읽습니다.
// 측정된 값은 실제 수분량을 나타내는 것이 아니라, 0~1023 범위로 환산된 저항값을 의미합니다.
int value = analogRead(soil);
// 토양 수분센서로부터 측정된 값를 시리얼 모니터에 출력합니다.
Serial.print("read sensor value : ");
Serial.println(value);
// 토양 수분센서로부터 측정된 값 (0~1024)을, 음계 배열 변수 값 (0~7) 범위로 변환합니다.
int ton = map(value, 0, 1023, 0, 7);
// 변환된 음계의 주파수를 시리얼 모니터에 출력합니다.
Serial.print("output ton ");
Serial.println(tones[ton]);
Serial.print("Hz");
// 피에조 부저가 연결된 핀으로 지정된 주파수를 가지는 square-wave(구형파 또는 사각파)플 생성하도록 설정합니다.
// 도가 261Hz의 주파수를 가진다면, 1초에 261번의 square-wave을 발생신킨다는 의미입니다.
// 디지털은 0 과 1 (HIGH와 LOW)로 표현이되므로, 1초 동안에 0과 1의 변화를 261번 주는 것과 같습니다.
tone(piezo, tones[ton]);
// 1초 동안 대기합니다.
delay(1000);
}


위 코드는 피에조 부저와 토양 수분센서 사용에 관한 소스가 같이 들어있다. 토양 수분센서의 관한 사용은 값을 사용하기 위한 변환식이 없고 단순하게 출력되는 아날로그 값을 읽어 ADC값으로 0부터 1024까지 값을 읽어오니 코드를 읽는데 큰 어려움이 없다.

토양 수분센서와 피에조 부저 코드상에서 모두 아날로그 값 기반으로 작동시키기 때문에 작동 핀을 아날로그 핀으로 선언한다.

그리고 피에조 부저로 도레미파솔라시도 음계를 나타내기 위해 주파수를 배열로 작성하는데

int tones[] = {261, 294, 330, 349, 392, 440, 494, 523};


아래 표를 참고하여 주파수를 작성한다. 코드는 4옥타브를 기준으로 작성했다. 만약 4옥타브로 작성했는데 소리가 구분이 잘 안 될 경우에는 옥타브를 올리면 소리가 좋아진다.

 

토양 수분센서는 analogRead()를 통해 값을 읽어오고 그 값을 map함수를 통해 0부터 7사이의 값으로 변환한다. 코드 주석에도 써있지만 analogRead()를 통해 읽어온 값은 실제 수분량이 아닌 환산된 저항값임을 주의한다.

// 토양 수분센서로부터 측정된 값을 읽습니다.
// 측정된 값은 실제 수분량을 나타내는 것이 아니라, 0~1023 범위로 환산된 저항값을 의미합니다.
int value = analogRead(soil);
// 토양 수분센서로부터 측정된 값를 시리얼 모니터에 출력합니다.
Serial.print("read sensor value : ");
Serial.println(value);
// 토양 수분센서로부터 측정된 값 (0~1024)을, 음계 배열 변수 값 (0~7) 범위로 변환합니다.
int ton = map(value, 0, 1023, 0, 7);

0부터 7사이의 값으로 변환시키면 이제 그 값에 따라 피에조 부저에서는 도레미파솔라시도 소리가 나게 된다.
만약 3의 값이 출력됐다면 파 소리가 나고, 6의 값이 출력됐다면 시 소리가 나게 된다.

tone(piezo, tones[ton]);


마치며
토양 수분센서는 다른 센서와 달리 사용 용도가 명확하기 때문에 실생활에서 사용할 수 있는 프로젝트를 제작하기에 가장 쉬운 센서이다. 인터넷에서 검색하면 토양 수분센서와 다양한 센서를 연결하여 각종 화분 알리미 프로젝트가 검색되는 것을 볼 수 있다. 특히 블루투스와 연결한다면 스마트폰을 사용하여 나만의 화분 알리미 시스템을 개발해 볼 수도 있다. 정확한 수분량을 측정하지 못한다는 것은 단점이지만 싼가격과 사용하기 쉽다는 장점이 있기 때문에 프로젝트 제작에 응용해 본다면 좋은 작품을 만들 수 있을 것이다.

 

 

 코코아팹은 한국 메이커 문화를 만들어가는 온라인 메이커포털이다. 오픈소스 하드웨어 오렌지보드의 생산과 활용법 공개 등 많은 활동을 진행하고 있다.


 


[Learning] Easy-to-follow Orange Board Tutorial (9)
How to Measure Water Contents Using Orange Board With a Soil Moisture Sensor

Arduino means a close friend in Italian and is a typical open-source hardware. It is a mini board made for anyone to easily access an Embedded field, which has been hard and difficult to approach. The Arduino Orange Board to be used in this tutorial is a Korean version of Arduino boards.

The distribution of Arduino boards triggered the spread of Open Source Hardware (OSHW) and significantly contributed to the spread of Maker Culture as well. Recently, conglomerates such as Intel, and Microsoft, started entering this open-source hardware market. This reflects that the potential of open-source hardware market has grown likewise.

What Is A Soil Moisture Sensor?

A soil moisture sensor is a sensor used to determine how much moisture is contained in soil by inserting it directly into a flowerpot or into soil. To be precise, it is a sensor to measure changes in a resistor in accordance with the water content in soil. It is affected by the moisture in soil as well as the size and variety of particles that compose soil. The more the water contents is in soil, the less a resistor value goes, and the less the water contents is, the larger the resistor value goes. However, it exhibits a limitation that it is prone to errors due to the insensitivity of an electric resistance occurred if there is a large amount of water contained in soil.

 

 How to Use a Soil Moisture Sensor

A soil moisture sensor consists of sensor probes that directly plug into soil, a sensor board, and a cable. Connect 2 pins of sensor probes to 2 pins of the sensor board (You do not need to differentiate + and -). Next, connect a cable to 3 pins (ACC, GND, and AO) except DO pin on the sensor board.

DO pin is a digital output having 0 and 1 only, and AO pin is an analogue output transmitting the water contents in soil with specific values in detail. Additionally, you may set a standard value to use by turning a variable resistance in blue color on the sensor board.

The example of this article illustrates the step of checking an analogue value using AO pin. The measured value does not indicate the water content measured at its location, but shows the result of converting the change of resistance in accordance with the water content within the range of ADC from 0 to 1023.

 

List of Items Required
To control the soil moisture sensor using Orange Board, the following items are required for preparation

 


How to Connect to the Hardware
1. Connect 5V pin of Orange Board to + bus of Breadboard
2. Connect GND pin of Orange Board to - bus of Breadboard
3. Insert Piezo Buzzer vertically having its + connector face upward as shown in the figure.
4. Connect + connector of Piezo Buzzer to A2 pin of Orange Board
5. Connect - connector of Piezo Buzzer to - bus.
6. Connect VCC connector of Soil Moisture Sensor to + bus.
7. Connect AO connector of Soil Moisture Sensor to A0 pin of Orange Board
8. Connect GND connector of Soil Moisture Sensor to - bus.

 


Circuit Diagram

 
 

Source Codes

/*
Title: How to measure the water contents using a soil moisture sensor
Details: After measuring the water contents in soil using a soil moisture sensor, have it make a sound if the measurement goes below a certain level of water content.
*/
// Set the soil water sensor to A0 pin.
int soil = A0;
// Set piezo buzzer to A2 pin.
int piezo = A2;
// Scale standard frequencies (4 Octaves): Do, Re, Mi, Fa, So, La, Ti, Do
// You can set a scale you want referring to a scale table.
int tones[] = {261, 294, 330, 349, 392, 440, 494, 523};
// When running, this is the function to be called first, and executed only once in its first run.
// It includes codes to declare variables or reset.
void setup() {
// It sets a serial communication to check the operation status of the soil moisture sensor. (Transfer rate 9600bps)
// Menu Tool -> Click a serial monitor
Serial.begin(9600);
// It sets the pin connected to piezo buzzer to be OUTPUT.
pinMode(piezo, OUTPUT);
}

// After setup() function gets called, loop() function gets called,
// It repeats codes in the block indefinitely.
void loop() {
// It reads a value measured from the soil moisture sensor.
// The measured value does not show the actual water content, but indicates a converted resistance value within the range from 0 to 1023.
int value = analogRead(soil);
// It outputs measured values from the soil moisture sensor to a serial monitor.
Serial.print("read sensor value : ");
Serial.println(value);
// It converts measured values (0–1024) from the soil moisture sensor to the range of a scale array of variables (0–7).
int ton = map(value, 0, 1023, 0, 7);
// It outputs converted frequencies of scale to the serial monitor.
Serial.print("output ton ");
Serial.println(tones[ton]);
Serial.print("Hz");
// It sets to create a square-wave that has a designated frequency to the pin connected to piezo buzzer.
// If Do has its frequency as 261Hz, it means that a square-wave 261 times for 1 second has been created.
// Because digital values are expressed in 0s and 1s (HIGH and LOW), it is equal to making changes of 0 and 1 for 261 times during 1 second.
tone(piezo, tones[ton]);
// It waits for 1 second.
delay(1000);
}



The above code contains source codes with regard to using both a piezo buzzer and a soil moisture sensor. For the code to use a soil moisture sensor, it is not difficult to read the code as there is no conversion formula to use values and as it fetches a value from 0 to 1024 out of ADC values after reading simple analogue output values.

Working pins are declared to be analogue pins since all values in the soil moisture sensor code as well as the piezo buzzer code work on the analogue value basis.
 

int tones[] = {261, 294, 330, 349, 392, 440, 494, 523};

And refer to the table below to make frequencies as the frequencies need to be in an array to express the scale of Do Re Mi Fa So La Ti Do using the piezo buzzer. The code is written based upon 4 octaves. If a sound cannot be clearly distinguished after writing a code based upon 4 octaves, you just need to elevate such octaves.

 

The soil moisture sensor reads values through analogRead() and converts them to values between 0 and 7 through map function. As written in the comment section of the code, please be noted that the values read from analogRead() are not the actual water contents, but converted resistance values.

// The measured values from the soil moisture sensor is read.
// The measured value does not show the actual water content, but indicates a converted resistance value within the range from 0 to 1023.
int value = analogRead(soil);
// The measured values from the soil moisture sensor is output to a serial monitor.
Serial.print("read sensor value : "); Serial.println(value);
// The measured values (0–1024) from the soil moisture sensor is converted to the range of a scale array of variables (0–7).
int ton = map(value, 0, 1023, 0, 7);

If converted to a value between 0 and 7, now you can hear the sound of Do Re Mi Fa So La Ti Do from the piezo buzzer, matching that value. If a value of 3 has been output, you can hear 'Fa', and if a value of 6 has been output, then you can hear 'Ti'.
 

tone(piezo, tones[ton]);

 

Closing Remarks
As the soil moisture sensor has a clear and definite purpose of use unlike other sensors, you may use it in your real life and also make applications most conveniently.

If you search on the internet, you may see a variety of projects related to flowerpot notice projects that is connected to soil moisture sensors and other diverse sensors. Especially, if connected to Bluetooth, you may develop your own flowerpot notification system using a smartphone. Although it is a disadvantage that precise water contents cannot be measured, you may still create a good piece of work if you apply it to your project due to the advantages of its low cost and convenience of using.

 

저작권자 © 아이티데일리 무단전재 및 재배포 금지