라즈베리 파이 관개
이 튜토리얼에서는 Raspberry Pi, 토양 수분 센서, 릴레이 및 펌프를 사용하여 자동 관개 시스템을 만드는 방법을 안내합니다. 구체적으로, 두 가지 응용 사례를 만드는 방법을 배웁니다:
- 응용 프로그램 1:
- 라즈베리 파이가 릴레이를 제어하여 토양 습기가 마를 때 펌프를 켜고 식물에 물을 줍니다.
- 라즈베리 파이가 릴레이를 제어하여 토양 습기가 젖었을 때 펌프를 끕니다.
- 응용 프로그램 2:
- 라즈베리 파이가 릴레이를 제어하여 토양 습기가 마르면 펌프를 5초 동안 켜고 나서 펌프를 끄고 토양 습기를 다시 확인합니다.
- 라즈베리 파이 - 토양 수분 센서 튜토리얼
- 라즈베리 파이 - 펌프 튜토리얼
- 라즈베리 파이는 토양 습도가 건조할 때 릴레이를 제어하여 펌프를 작동시켜 식물에 물을 줍니다.
- 라즈베리 파이는 토양 습도가 젖었을 때 릴레이를 제어하여 펌프를 끕니다.
- Raspberry Pi에 Raspbian 또는 기타 Raspberry Pi 호환 운영 체제가 설치되어 있는지 확인하세요.
- Raspberry Pi가 PC와 동일한 로컬 네트워크에 연결되어 있는지 확인하세요.
- 라이브러리를 설치해야 하는 경우 Raspberry Pi가 인터넷에 연결되어 있는지 확인하세요.
- Raspberry Pi를 처음 사용하는 경우, 라즈베리 파이 - 소프트웨어 설치을 참조하세요.
- Linux 및 macOS의 내장 SSH 클라이언트 또는 Windows의 PuTTY를 사용하여 SSH를 통해 PC를 Raspberry Pi에 연결하세요. SSH를 통해 PC를 Raspberry Pi에 연결하는 방법을 참조하세요.
- RPi.GPIO 라이브러리가 설치되어 있는지 확인하세요. 그렇지 않은 경우 다음 명령어를 사용하여 설치하세요:
- Adafruit_ADS1x15 라이브러리를 설치하려면 Raspberry Pi 터미널에서 다음 명령어를 실행하세요:
- 라즈베리 파이 - 토양 수분 센서의 지침에 따라 센서를 보정하여 젖은-마른 THRESHOLD 값을 얻습니다.
- Python 스크립트 파일 auto_irrigation_1.py을 생성하고 다음 코드를 추가합니다:
- 코드에서 보정된 THRESHOLD 값을 업데이트하세요.
- 파일을 저장하고 터미널에서 다음 명령을 실행하여 Python 스크립트를 실행하세요:
- 터미널에서 결과 확인.
- 라즈베리 파이는 토양이 5초 동안 건조할 때 펌프를 켜고, 그 후에 펌프를 끌 것입니다.
- 파이썬 스크립트 파일 auto_irrigation_2.py를 생성하고 다음 코드를 추가하세요:
- 코드에서 보정된 THRESHOLD 값을 업데이트하세요.
- 파일을 저장하고 터미널에서 다음 명령을 실행하여 Python 스크립트를 실행하세요:
- 터미널에서 결과 확인.
준비물
1 | × | 라즈베리 파이 5 | 아마존 | |
1 | × | 용량성 토양 수분 센서 | 쿠팡 | 아마존 | |
1 | × | ADS1115 ADC 모듈 | 아마존 | |
1 | × | 릴레이 | 아마존 | |
1 | × | 12V 펌프 | 쿠팡 | 아마존 | |
1 | × | Vinyl Tube | 아마존 | |
1 | × | 12V 전원 어댑터 | 아마존 | |
1 | × | DC 커넥터 전원 연결 잭 플러그 소켓 | 쿠팡 | 아마존 | |
1 | × | 점퍼케이블 | 쿠팡 | 아마존 | |
1 | × | (추천) 라즈베리 파이용 스크루 터미널 블록 쉴드 | 쿠팡 | 아마존 | |
1 | × | (추천) 라즈베리 파이 프로토타이핑 베이스 플레이트 & 브레드보드 키트 | 아마존 | |
1 | × | (추천) 라즈베리 파이용 HDMI 터치 스크린 모니터 | 아마존 |
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.
토양 습도 센서 및 펌프에 대하여
펌프 및 토양 수분 센서(핀아웃, 작동, 프로그래밍 등)에 익숙하지 않은 경우, 다음 튜토리얼이 도움이 될 수 있습니다:
선연결

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.
배선 구성을 간단하고 체계적으로 만들기 위해, Raspberry Pi용 스크루 터미널 블록 실드 사용을 권장합니다. 이 실드는 아래와 같이 더욱 안정적이고 관리하기 쉬운 연결을 제공합니다:

라즈베리 파이 코드 - 애플리케이션 1
관개 시스템을 다음과 같이 만들겠습니다:
자세한 사용 방법
sudo apt-get update
sudo apt-get install python3-rpi.gpio
sudo pip install Adafruit-ADS1x15
/*
* 이 라즈베리 파이 코드는 newbiely.kr 에서 개발되었습니다
* 이 라즈베리 파이 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다.
* 상세한 지침 및 연결도에 대해서는 다음을 방문하세요:
* https://newbiely.kr/tutorials/raspberry-pi/raspberry-pi-irrigation
*/
import time
import Adafruit_ADS1x15
import RPi.GPIO as GPIO
# Create an ADS1115 ADC object
adc = Adafruit_ADS1x15.ADS1115()
# Set the gain to ±4.096V (adjust if needed)
GAIN = 1
# Single threshold for wet/dry classification (adjust as needed)
THRESHOLD = 45000
# GPIO pin connected to the relay module to control the pump
RELAY_PIN = 23
# Setup GPIO mode and relay pin as output
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
# Initialize pump state to OFF
GPIO.output(RELAY_PIN, GPIO.LOW)
pump_state = False
# Main loop to read the analog value from the soil moisture sensor and control the pump
try:
while True:
# Read the raw analog value from channel A3
raw_value = adc.read_adc(3, gain=GAIN)
# Determine the wet-dry level based on the raw ADC value
if raw_value > THRESHOLD:
level = "DRY"
else:
level = "WET"
# Print the results
print("Raw Value: {} \t Wet-Dry Level: {}".format(raw_value, level))
# Control the pump based on the wet-dry level
if level == "DRY" and not pump_state:
GPIO.output(RELAY_PIN, GPIO.HIGH) # Turn on the pump
pump_state = True
print("Pump State: ON")
elif level == "WET" and pump_state:
GPIO.output(RELAY_PIN, GPIO.LOW) # Turn off the pump
pump_state = False
print("Pump State: OFF")
# Add a delay between readings (adjust as needed)
time.sleep(1)
except KeyboardInterrupt:
# Cleanup GPIO on exit
GPIO.cleanup()
print("\nExiting the program.")
python3 auto_irrigation_1.py
PuTTY - Raspberry Pi
Raw Value: 48520 Wet-Dry Level: DRY
Pump State: ON
Raw Value: 47235 Wet-Dry Level: DRY
Raw Value: 46032 Wet-Dry Level: DRY
Raw Value: 45047 Wet-Dry Level: DRY
Raw Value: 44047 Wet-Dry Level: WET
Pump State: OFF
Raw Value: 44047 Wet-Dry Level: WET
Raw Value: 44031 Wet-Dry Level: WET
Raw Value: 44023 Wet-Dry Level: WET
스크립트는 터미널에서 Ctrl + C를 누를 때까지 무한 루프로 계속 실행됩니다.
코드 설명
소스 코드의 주석에 포함된 줄별 설명을 확인하세요!
라즈베리 파이 코드 - 애플리케이션 2
우리는 관개 시스템을 다음과 같이 작동시킬 것입니다:
자세한 사용 방법
/*
* 이 라즈베리 파이 코드는 newbiely.kr 에서 개발되었습니다
* 이 라즈베리 파이 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다.
* 상세한 지침 및 연결도에 대해서는 다음을 방문하세요:
* https://newbiely.kr/tutorials/raspberry-pi/raspberry-pi-irrigation
*/
import time
import Adafruit_ADS1x15
import RPi.GPIO as GPIO
# Create an ADS1115 ADC object
adc = Adafruit_ADS1x15.ADS1115()
# Set the gain to ±4.096V (adjust if needed)
GAIN = 1
# Single threshold for wet/dry classification (adjust as needed)
THRESHOLD = 45000
# GPIO pin connected to the relay module to control the pump
RELAY_PIN = 12
# Function to determine the wet-dry level based on the soil moisture percentage
def wet_dry_level(soil_moisture):
if soil_moisture > THRESHOLD:
return "DRY"
else:
return "WET"
# Function to activate the pump for a specified duration
def activate_pump():
GPIO.output(RELAY_PIN, GPIO.HIGH) # Turn on the pump
print("Pump State: ON")
time.sleep(5) # Keep the pump on for 5 seconds
GPIO.output(RELAY_PIN, GPIO.LOW) # Turn off the pump
print("Pump State: OFF")
# Setup GPIO mode and relay pin as output
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
# Main loop to read the analog value from the soil moisture sensor and control the pump
try:
while True:
# Read the raw analog value from channel A3
raw_value = adc.read_adc(3, gain=GAIN)
# Determine the wet-dry level based on the raw ADC value
level = wet_dry_level(raw_value)
# Print the results
print("Raw Value: {} \t Wet-Dry Level: {}".format(raw_value, level))
# If soil is wet, activate the pump
if level == "DRY":
activate_pump()
# Add a delay between readings (adjust as needed)
time.sleep(1)
except KeyboardInterrupt:
# Cleanup GPIO on exit
GPIO.cleanup()
print("\nExiting the program.")
python3 auto_irrigation_2.py
PuTTY - Raspberry Pi
Raw Value: 48520 Wet-Dry Level: DRY
Pump State: ON
Pump State: OFF
Raw Value: 42400 Wet-Dry Level: WET
Raw Value: 42600 Wet-Dry Level: WET
Raw Value: 42800 Wet-Dry Level: WET
동영상
비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.