11/*
2+ *---------------------------------------------------------------------------------
3+ *
4+ * Copyright (c) 2025, SparkFun Electronics Inc.
5+ *
6+ * SPDX-License-Identifier: MIT
7+ *
8+ *---------------------------------------------------------------------------------
9+ */
210
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
612#include < Wire.h>
713
814SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor class
@@ -16,6 +22,8 @@ SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor cla
1622#define wirePort Wire
1723#endif
1824
25+ // ----------------------------------------------------------------------------------------
26+
1927void setup ()
2028{
2129 Serial.begin (115200 );
@@ -27,33 +35,48 @@ void setup()
2735 // Comment out this while loop, or it will prevent the remaining code from running.
2836
2937 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+
3144
3245 wirePort.begin ();
3346
3447 if (mySoilSensor.begin () == false )
3548 {
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+
3851 while (1 )
3952 ;
4053 }
4154 Serial.println (" Soil Moisture Sensor found!" );
4255
4356 mySoilSensor.LEDOff ();
57+
58+ Serial.println ();
59+ Serial.println (" Reading soil moisture sensor..." );
60+ Serial.println ();
4461}
4562
63+ // ----------------------------------------------------------------------------------------
64+
4665void loop ()
4766{
4867 // Wink the LED during the reading
4968 mySoilSensor.LEDOn ();
5069 // Let's get some readings
5170 uint16_t soilMoisture = mySoilSensor.readMoistureValue ();
71+
72+ // Output the value:
5273 Serial.print (" Soil Moisture: " );
5374 Serial.println (soilMoisture);
5475
76+ // small delay for the LED to be seen
5577 delay (100 );
5678 mySoilSensor.LEDOff ();
5779
80+ // delay our reading.
5881 delay (2000 );
5982}
0 commit comments