1
1
/*
2
+ *---------------------------------------------------------------------------------
3
+ *
4
+ * Copyright (c) 2025, SparkFun Electronics Inc.
5
+ *
6
+ * SPDX-License-Identifier: MIT
7
+ *
8
+ *---------------------------------------------------------------------------------
9
+ */
2
10
3
- */
4
-
5
- #include " SparkFun_Soil_Moisture_Sensor.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_Soil_Moisture
11
+ #include " SparkFun_Soil_Moisture_Sensor.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_Soil_Moisture_Sensor
6
12
#include < Wire.h>
7
13
8
14
SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor class
@@ -16,6 +22,8 @@ SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor cla
16
22
#define wirePort Wire
17
23
#endif
18
24
25
+ // ----------------------------------------------------------------------------------------
26
+
19
27
void setup ()
20
28
{
21
29
Serial.begin (115200 );
@@ -27,33 +35,48 @@ void setup()
27
35
// Comment out this while loop, or it will prevent the remaining code from running.
28
36
29
37
Serial.println ();
30
- Serial.println (" Soil Sensor Example 1 - Basic Readings" );
38
+ Serial.println (" ------------------------------------------------------------" );
39
+ Serial.println (" SparkFun Soil Sensor Example 2" );
40
+ Serial.println (" Basic Readings and On-Board LED Control" );
41
+ Serial.println (" ------------------------------------------------------------" );
42
+ Serial.println ();
43
+
31
44
32
45
wirePort.begin ();
33
46
34
47
if (mySoilSensor.begin () == false )
35
48
{
36
- Serial.println (" Soil Moisture Sensor not detected at default I2C address. Verify the sensor is connected "
37
- " correctly. Freezing... " );
49
+ Serial.println (" Soil Moisture Sensor not detected at default I2C address. Verify the sensor is connected. Stopping. " );
50
+
38
51
while (1 )
39
52
;
40
53
}
41
54
Serial.println (" Soil Moisture Sensor found!" );
42
55
43
56
mySoilSensor.LEDOff ();
57
+
58
+ Serial.println ();
59
+ Serial.println (" Reading soil moisture sensor..." );
60
+ Serial.println ();
44
61
}
45
62
63
+ // ----------------------------------------------------------------------------------------
64
+
46
65
void loop ()
47
66
{
48
67
// Wink the LED during the reading
49
68
mySoilSensor.LEDOn ();
50
69
// Let's get some readings
51
70
uint16_t soilMoisture = mySoilSensor.readMoistureValue ();
71
+
72
+ // Output the value:
52
73
Serial.print (" Soil Moisture: " );
53
74
Serial.println (soilMoisture);
54
75
76
+ // small delay for the LED to be seen
55
77
delay (100 );
56
78
mySoilSensor.LEDOff ();
57
79
80
+ // delay our reading.
58
81
delay (2000 );
59
82
}
0 commit comments