|
| 1 | +/* |
| 2 | + *--------------------------------------------------------------------------------- |
| 3 | + * |
| 4 | + * Copyright (c) 2025, SparkFun Electronics Inc. |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: MIT |
| 7 | + * |
| 8 | + * This example demonstrates how to change the I2C address of the Soil Sensor. |
| 9 | + * |
| 10 | + * Supported Sensor: |
| 11 | + * Qwiic Soil Moisture Sensor https://www.sparkfun.com/sparkfun-qwiic-soil-moisture-sensor.html |
| 12 | + * |
| 13 | + * Hardware Connections: |
| 14 | + * - Connect the Qwiic Soil Moisture Sensor to the Qwiic connector of your development board (SparkFun Thing Plus, |
| 15 | + *ReadBoard ...etc.) If you don't have a development board with a Qwiic connector, you can purchase one at sparkfun.com |
| 16 | + * - Connect the development board to your computer using a USB cable. |
| 17 | + * - Open the Serial Monitor at a baud rate of 115200 to see the sketch output. |
| 18 | + * |
| 19 | + *--------------------------------------------------------------------------------- |
| 20 | + */ |
| 21 | + |
| 22 | +#include "SparkFun_Soil_Moisture_Sensor.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_Soil_Moisture_Sensor |
| 23 | +#include <Wire.h> |
| 24 | + |
| 25 | +SparkFunSoilMoistureSensorI2C mySoilSensor; // Create an instance of the sensor class |
| 26 | + |
| 27 | +// Define our alternate I2C address - the default address + 1 |
| 28 | +#define ALTERNATE_I2C_ADDRESS SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS + 1 |
| 29 | + |
| 30 | +// Some Dev boards have their QWIIC connector on Wire or Wire1 |
| 31 | +// This #ifdef will help this sketch work across more products |
| 32 | + |
| 33 | +#ifdef ARDUINO_SPARKFUN_THINGPLUS_RP2040 |
| 34 | +#define wirePort Wire1 |
| 35 | +#else |
| 36 | +#define wirePort Wire |
| 37 | +#endif |
| 38 | + |
| 39 | +//---------------------------------------------------------------------------------------- |
| 40 | +void setup() |
| 41 | +{ |
| 42 | + Serial.begin(115200); |
| 43 | + |
| 44 | + while (!Serial) |
| 45 | + delay(10); // Wait for Serial to become available. |
| 46 | + |
| 47 | + // NOTE: |
| 48 | + // Changing I2C addresses and reconnecting can be a bit fiddily. If the following fails, try resetting the board |
| 49 | + |
| 50 | + Serial.println(); |
| 51 | + Serial.println("------------------------------------------------------------"); |
| 52 | + Serial.println("SparkFun Soil Sensor Example 4"); |
| 53 | + Serial.println("Change the I2C Address Example"); |
| 54 | + Serial.println("------------------------------------------------------------"); |
| 55 | + Serial.println(); |
| 56 | + |
| 57 | + wirePort.begin(); |
| 58 | + |
| 59 | + Serial.println("Checking for the sensor at the default I2C address..."); |
| 60 | + |
| 61 | + if (mySoilSensor.begin() == false) |
| 62 | + { |
| 63 | + // Did the sensor get left at the alt address? Try the alt address |
| 64 | + SparkFunSoilMoistureSensorI2C tmpSensor; |
| 65 | + if (tmpSensor.begin(ALTERNATE_I2C_ADDRESS) == true) |
| 66 | + { |
| 67 | + Serial.println("Sensor found at the alternate address. Changing the address back to the default."); |
| 68 | + tmpSensor.setI2CAddress(SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS); |
| 69 | + delay(3000); |
| 70 | + if (mySoilSensor.begin() == false) |
| 71 | + { |
| 72 | + Serial.println( |
| 73 | + "Error: Soil Moisture Sensor not detected at default I2C address. Verify the sensor is connected. Stopping."); |
| 74 | + while (1) |
| 75 | + ; |
| 76 | + } |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + Serial.println( |
| 81 | + "Soil Moisture Sensor not detected at default or alternative I2C address. Verify the sensor is connected. Stopping."); |
| 82 | + while (1) |
| 83 | + ; |
| 84 | + } |
| 85 | + } |
| 86 | + Serial.println("Soil Moisture Sensor found!"); |
| 87 | + Serial.println(); |
| 88 | + |
| 89 | + // write out the current value |
| 90 | + Serial.print("Sensor Address: 0x"); |
| 91 | + Serial.print(mySoilSensor.address(), HEX); |
| 92 | + |
| 93 | + // Output the value: |
| 94 | + Serial.print(", Soil Moisture: "); |
| 95 | + Serial.print(mySoilSensor.readMoistureValue()); |
| 96 | + Serial.print(" (sensor value), "); |
| 97 | + |
| 98 | + // Now the percent moisture |
| 99 | + Serial.print(mySoilSensor.readMoisturePercentage()); |
| 100 | + Serial.println("% wet"); |
| 101 | + |
| 102 | + Serial.println(); |
| 103 | + |
| 104 | + Serial.println("Changing the I2C address of the sensor to: 0x" + String(ALTERNATE_I2C_ADDRESS, HEX)); |
| 105 | + |
| 106 | + // Change the I2C address |
| 107 | + if (mySoilSensor.setI2CAddress(ALTERNATE_I2C_ADDRESS) != kSTkErrOk) |
| 108 | + { |
| 109 | + Serial.println("ERROR: Unable to change the I2C address. Stopping."); |
| 110 | + while (1) |
| 111 | + ; |
| 112 | + } |
| 113 | + |
| 114 | + // let the changes take place on the sensor board. |
| 115 | + delay(3000); |
| 116 | + Serial.println(); |
| 117 | + |
| 118 | + // try the sensor at the new address |
| 119 | + SparkFunSoilMoistureSensorI2C mySoilSensorNewAddress; // Create an instance of the sensor class at new address |
| 120 | + |
| 121 | + if (mySoilSensorNewAddress.begin(ALTERNATE_I2C_ADDRESS) == false) |
| 122 | + { |
| 123 | + Serial.println( |
| 124 | + "Error: Soil Moisture Sensor not detected at new I2C address. Stopping."); |
| 125 | + while (1) |
| 126 | + ; |
| 127 | + } |
| 128 | + Serial.println("Soil Moisture Sensor found at the new address!"); |
| 129 | + Serial.println();; |
| 130 | + |
| 131 | + // write out the current value |
| 132 | + Serial.print("Sensor Address: 0x"); |
| 133 | + Serial.print(mySoilSensorNewAddress.address(), HEX); |
| 134 | + |
| 135 | + // Output the value: |
| 136 | + Serial.print(", Soil Moisture: "); |
| 137 | + Serial.print(mySoilSensorNewAddress.readMoistureValue()); |
| 138 | + Serial.print(" (sensor value), "); |
| 139 | + |
| 140 | + // Now the percent moisture |
| 141 | + Serial.print(mySoilSensorNewAddress.readMoisturePercentage()); |
| 142 | + Serial.println("% wet"); |
| 143 | + |
| 144 | + |
| 145 | + Serial.println("Changing the I2C address of the sensor to: 0x" + String(SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS, HEX)); |
| 146 | + |
| 147 | + // Change the I2C address |
| 148 | + if (mySoilSensorNewAddress.setI2CAddress(SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS) != kSTkErrOk) |
| 149 | + { |
| 150 | + Serial.println("ERROR: Unable to change the I2C address. Stopping."); |
| 151 | + while (1) |
| 152 | + ; |
| 153 | + } |
| 154 | + |
| 155 | + // let the changes take place on the sensor board. |
| 156 | + delay(3000); |
| 157 | + |
| 158 | + Serial.println(); |
| 159 | + Serial.println("Address changing complete. Entering loop..."); |
| 160 | + Serial.println(); |
| 161 | + |
| 162 | +} |
| 163 | + |
| 164 | +//---------------------------------------------------------------------------------------- |
| 165 | +void loop() |
| 166 | +{ |
| 167 | + // write out the current value |
| 168 | + Serial.print("Sensor Address: 0x"); |
| 169 | + Serial.print(mySoilSensor.address(), HEX); |
| 170 | + Serial.print(", Soil Moisture: "); |
| 171 | + Serial.print(mySoilSensor.readMoistureValue()); |
| 172 | + Serial.print(" (sensor value), "); |
| 173 | + |
| 174 | + // Now the percent moisture |
| 175 | + Serial.print(mySoilSensor.readMoisturePercentage()); |
| 176 | + Serial.println("% wet"); |
| 177 | + |
| 178 | + // loop delay |
| 179 | + delay(5000); |
| 180 | +} |
0 commit comments