5.2 Threshold

In many projects, you will encounter such a need. “When xxx reaches a certain level, then…”

For example, in a smart home, when the light intensity is lower than 50Lux, turn on the light; Another example is in the computer motherboard, if the operating temperature of the CPU is higher than 65 degrees Celsius, turn on the fan, and so on.

In these requirements, the keyword “threshold” is reflected.

We can adjust the value of the threshold to make the circuit operate more in line with individual needs. For example, if I like a brighter living environment, I can increase the threshold of the automatic lights of the smart home to 80Lux. Another example is that the ventilation environment of my studio is not very good, and the heat dissipation demand is higher, then the threshold value of automatic fan opening can be adjusted to 50 degrees Celsius.

Here we use soil moisture sensor and 2 LEDs to make a pot monitor. If the soil is too dry, the red LED will light up; if the soil is moist enough, the green LED will light up. You need to manually adjust the thresholds for determining the dryness and wetness of the soil.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

3 in 1 Starter Kit

380+

3 in 1 Starter Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

SunFounder R3 Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

LED

BUY

Soil Moisture Module

BUY

Schematic

../_images/circuit_8.2_threshold.png

Wiring

../_images/threshold_bb.png

Code

Note

  • Open the 5.2.threshold.ino file under the path of 3in1-kit\basic_project\5.2.threshold.

  • Or copy this code into Arduino IDE.

After the code is uploaded successfully, if your threshold is set correctly, you will see the red LED light up when the soil is dry to remind you that you need to water; after watering, the green LED will light up.

How it works

...

void loop() {
    int sensorValue = analogRead(soilMoisture);
    Serial.println(sensorValue);
    if (sensorValue > threshold) {
        digitalWrite(redPin, HIGH); // Turn the red LED
        digitalWrite(greenPin, LOW); // green
    } else {
        digitalWrite(greenPin, HIGH); // Turn on the green LED
        digitalWrite(redPin, LOW); // red
    }
}
...

First set a threshold value and then read the value of the soil moisture module, its value decreases as the moisture level increases. If the value currently read is greater than the set threshold, then let the red LED light up, otherwise it will turn on the green LED.

This threshold value needs to be adjusted according to the actual situation, you can upload the code first, then open the serial monitor to check the value, record the value in both wet and dry conditions, and then choose a middle value as the threshold value.