Re: [응용하기] 수분+워터펌프_릴레이 사용X
Makerist
센서기초
1
1679
2024.03.17 04:19
// 토양 수분 센서 및 워터 펌프 핀 설정 const int soilSensorPin = A0; // 토양 수분 센서를 A0 핀에 연결 const int waterPumpPin = 2; // 워터 펌프 모터를 2번 핀에 연결 // 토양 수분 센서의 임계값 설정 const int threshold = 500; // 수분이 부족한 경우의 임계값 설정 (임의로 설정) void setup() { // 워터 펌프 핀을 출력으로 설정 pinMode(waterPumpPin, OUTPUT); // 초기 상태에서 워터 펌프 비활성화 digitalWrite(waterPumpPin, LOW); } void loop() { // 토양 수분 센서 값 읽기 int soilMoisture = analogRead(soilSensorPin); // 토양 수분이 부족한 경우 워터 펌프 작동 if (soilMoisture < threshold) { Serial.println("수분 부족! 워터 펌프 작동"); digitalWrite(waterPumpPin, HIGH); // 워터 펌프 모터 활성화 } else { Serial.println("수분 충분. 워터 펌프 작동 중지"); digitalWrite(waterPumpPin, LOW); // 워터 펌프 모터 비활성화 } // 잠시 대기 delay(1000); }