SparkFun Soil Moisture Sensor  v1.0.1-2-g96f40cb
Library for the SparkFun Soil Moisture Sensor
Loading...
Searching...
No Matches
SparkFunSoilMoistureSensor Class Reference

#include <SparkFun_Soil_Moisture_Sensor.h>

Inheritance diagram for SparkFunSoilMoistureSensor:

Public Member Functions

bool begin (const uint8_t address=SF_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS, TwoWire &wirePort=Wire)
 Begins the Device. More...
 
bool isConnected ()
 Checks if the device is connected and responding. More...
 
sfTkError_t begin (sfTkIBus *theBus=nullptr)
 Initializes communication with the soil moisture sensor. More...
 
sfTkError_t LEDOff (void)
 Turns off the on-board LED. More...
 
sfTkError_t LEDOn (void)
 Turns on the on-board LED. More...
 
uint16_t readMoistureValue (void)
 Reads the moisture value from the sensor. More...
 
float readMoistureRatio (void)
 Reads the sensor value and returns the moisture ratio from the sensor. More...
 
float readMoisturePercentage (void)
 Reads the sensor value and returns the moisture percentage from the sensor. More...
 
sfTkError_t setI2CAddress (uint8_t newAddress)
 Changes the I2C address of the sensor. More...
 
uint8_t address (void)
 Returns the current address of the sensor. More...
 

Protected Attributes

sfTkIBus * _theBus
 The toolkit bus the sensor is connected to. More...
 

Member Function Documentation

◆ address()

uint8_t sfDevSoilMoisture::address ( void  )
inherited

Returns the current address of the sensor.

This function returns the current I2C address of the sensor if using I2C, or the Chip Select (CS) pin if using SPI.

Returns
The current I2C address of the sensor, or the CS pin for SPI

◆ begin() [1/2]

bool SparkFunSoilMoistureSensor::begin ( const uint8_t  address = SF_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS,
TwoWire &  wirePort = Wire 
)
inline

Begins the Device.

Initializes the soil moisture sensor for I2C communication using the specified I2C address and wire port.

Parameters
addressI2C device address to use for the sensor (default is SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS)
wirePortWire port to use for I2C communication (default is Wire)
Returns
true if successful, false otherwise

◆ begin() [2/2]

sfTkError_t sfDevSoilMoisture::begin ( sfTkIBus *  theBus = nullptr)
inherited

Initializes communication with the soil moisture sensor.

This function must be called before any other methods. It establishes communication with the sensor using the provided I2C bus interface.

Parameters
theBusPointer to an I2C toolkit object. If nullptr, uses previously set bus
Returns
ksfTkErrOk if initialization succeeds
ksfTkErrBusNotInit if no valid bus interface is provided

◆ isConnected()

bool SparkFunSoilMoistureSensor::isConnected ( )
inline

Checks if the device is connected and responding.

Tests communication with the sensor by attempting to ping it over I2C. A successful ping indicates the sensor is properly connected and responding.

Returns
true if the sensor responds to communication attempts, false otherwise

◆ LEDOff()

sfTkError_t sfDevSoilMoisture::LEDOff ( void  )
inherited

Turns off the on-board LED.

Disables the sensor's built-in LED

Returns
ksfTkErrOk if LED was successfully turned off
ksfTkErrBusNotInit if no bus interface is configured
ksfTkErrFail if sensor doesn't respond or a communication error occurs
See also
LEDOn()

◆ LEDOn()

sfTkError_t sfDevSoilMoisture::LEDOn ( void  )
inherited

Turns on the on-board LED.

Enables the sensor's built-in LED by sending the appropriate command

Returns
ksfTkErrOk if LED was successfully turned on
ksfTkErrBusNotInit if no bus interface is configured
ksfTkErrFail if sensor doesn't respond or a communication error occurs
See also
LEDOff()

◆ readMoisturePercentage()

float sfDevSoilMoisture::readMoisturePercentage ( void  )
inherited

Reads the sensor value and returns the moisture percentage from the sensor.

This function reads the raw moisture value from the sensor and converts it to a percentage between 0 and 100%.

Returns
The moisture percentage, where 0% indicates a dry sensor and 100% indicates a wet sensor

◆ readMoistureRatio()

float sfDevSoilMoisture::readMoistureRatio ( void  )
inherited

Reads the sensor value and returns the moisture ratio from the sensor.

This function reads the raw moisture value from the sensor and converts it to a ratio between 0 and 1.0.

Returns
The moisture ratio, where 0 indicates a dry sensor and 1.0 indicates a wet sensor
Note
On error, this method returns 0

◆ readMoistureValue()

uint16_t sfDevSoilMoisture::readMoistureValue ( void  )
inherited

Reads the moisture value from the sensor.

This function reads the raw moisture value from the sensor, which is a resistance reading between 0 and 1023.

Returns
The moisture value - a resistance reading between 0 and 1023
Note
A value of 0 indicates a wet sensor, while 1023 indicates a dry sensor
On error, this method returns 0

◆ setI2CAddress()

sfTkError_t sfDevSoilMoisture::setI2CAddress ( uint8_t  newAddress)
inherited

Changes the I2C address of the sensor.

This function sets a new I2C address for the sensor. The new address will be used for all future I2C communication with the sensor. This value is persistent

Parameters
newAddressThe new I2C address to assign to the sensor
Returns
ksfTkErrOk if successful, otherwise an error code
Note
If communicating via I2C, the provided address is used for future I2C communication

Member Data Documentation

◆ _theBus

sfTkIBus* sfDevSoilMoisture::_theBus
protectedinherited

The toolkit bus the sensor is connected to.

This member variable holds a pointer to the bus interface (I2C or SPI) used for communication with the soil moisture sensor.


The documentation for this class was generated from the following file: