This tutorial is the third one in the series corresponding to the Sunbrella project for the Sensing the City course taught by Allison Lassiter for students in the University of Pennsylvania’s Department of City and Regional Planning.
This tutorial will focus on how to create a rain (water) trigger override for a motor using an Adafruit Analog Water Sensor. The mechanism aims to override the default setting of the servo motor (as coded in Using photoresistors and a servo motor to make a single-axis solar tracker by Delfina Vildosola) such that it adjusts the roof to a predefined position for optimum rain protection. This tutorial is also in series with the tutorial by Laura Frances: Using a solar cell and rechargeable battery to power a servo motor.
Parts List
Included in kit:
Servo Motor
Breadboard
Jumper wires
9V Battery
Sensor Specifications
- A water sensor that is compatible with Arduino UNO board.
- Adafruit Analog Water Sensor is a simple analog water sensor that can be used to detect the presence or absence of water. It works by measuring the resistance between two exposed electrodes on the sensor, which changes when water is present. It can be connected directly to an analog input on the Arduino board and costs around $1.95.
- Description from the website: The sensor has a very lightly biased transistor that is connected to two long sets of interlocking traces. When a little bit of water/condensation lands on the PCB and touches two of the traces, the transistor will turn on, setting the output pin high. The output voltage does vary a bit with how much water is detected, but honestly, we recommend just using it as an “on or off” type sensor. (source: https://www.adafruit.com/product/4965)
Circuit Diagram
Here’s the circuit diagram for the rain trigger override circuit:
In this circuit, the motor is connected to the motor driver, which is controlled by the Arduino. The Adafruit Analog Water Sensor is connected to the Arduino analog input pin A0.
Connections: connect the minus (-) pin to the ground, connect the plus (+) pin to 5V DC, and then the output signal pin (S) goes to your breadboard. For servo motor connections and roof connections, refer to Using photoresistors and a servo motor to make a single-axis solar tracker by Delfina Vildosola.
Step 1: Connect the circuit
Connect the circuit according to the circuit diagram above. Make sure all connections are secure and correctly placed.
Step 2: Upload the code
Upload the code to the Arduino using the Arduino IDE. Make sure the correct board and serial port are selected in the IDE.
Step 3: Test the circuit
Power on the circuit and test it by pouring some water on the Adafruit Analog Water Sensor. The motor should move in one direction when the water level is high, and move in the opposite direction when the water level is low.
Step 4: Customize the code
You can customize the code by changing the water level threshold that triggers the motor movement. We set a threshold at 500, but you can adjust this to suit your needs.
Code
Here’s the code for the rain trigger override circuit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
// Define the pin numbers for the water sensor, motor, and photosensor int waterSensorPin = A0; //analog pin int motorPin = 3; int photoSensorPin = A1; //analog pin // Define the threshold values for triggering the motor action and adjusting the motor position int rainThreshold = 500; //change the threshold value int sunThreshold = 700; //change the threshold value void setup() { // Initialize the digital pins for the motor pinMode(motorPin, OUTPUT); // Set up the serial communication for debugging Serial.begin(9600); } void loop() { // Read the value from the water sensor and photosensor int waterSensorValue = analogRead(waterSensorPin); int photoSensorValue = analogRead(photoSensorPin); // Print the sensor values to the serial monitor for debugging Serial.print("Water Sensor Value: "); Serial.print(waterSensorValue); Serial.print(" | "); Serial.print("Photo Sensor Value: "); Serial.println(photoSensorValue); // Check if it's raining based on the water sensor value if (waterSensorValue < rainThreshold) { // Trigger the motor action by setting the motor pin to high digitalWrite(motorPin, HIGH); } // Check if it's not raining and the sun intensity is high based on the photosensor value else if (waterSensorValue >= rainThreshold && photoSensorValue > sunThreshold) { // Adjust the roof position to create shade by setting the motor pin to high digitalWrite(motorPin, HIGH); } // Stop the motor action by setting the motor pin to low else { digitalWrite(motorPin, LOW); } // Wait for a short delay before repeating the loop delay(1000); //depends on you, you can change it to 5 mins } |
In this code, the Adafruit Analog Water Sensor is read using the analogRead() function. If the water level is high (i.e., rain is detected), the motor is moved in one direction using the digitalWrite() function. If the water level is low (i.e., no rain is detected), the motor is moved in the opposite direction to assume the default position.