An Arduino library for interfacing with specialized soil sensors over serial communication. This library allows reading soil temperature, humidity, electrical conductivity, pH, nitrogen, phosphorus, and potassium levels from Halisense 7 in 1 Sensor.
- Read 7 soil parameters: temperature, humidity, electrical conductivity, pH, nitrogen, phosphorus, and potassium
- Support for both default and custom serial pins (especially useful for ESP32)
- Individual or batch reading of all parameters
- Comprehensive error handling
- Easy to use API
This library is designed to work with:
- Arduino boards with multiple hardware serial ports
- ESP32 boards with configurable serial pins
- Any board with a compatible soil sensor using the specified serial protocol
- Open the Arduino IDE
- Go to Sketch > Include Library > Manage Libraries...
- Search for "SoilSensor"
- Click "Install"
- Download the latest release from the GitHub repository
- Extract the ZIP file
- Move the extracted folder to your Arduino libraries directory
- Windows:
Documents\Arduino\libraries\ - macOS:
~/Documents/Arduino/libraries/ - Linux:
~/Arduino/libraries/
- Windows:
- Restart the Arduino IDE
#include <SoilSensor.h>
// Create a sensor instance using default pins
SoilSensor soilSensor(Serial2);
void setup() {
Serial.begin(115200);
// Initialize the sensor
if (soilSensor.begin(4800)) {
Serial.println("Sensor initialized successfully");
} else {
Serial.println("Failed to initialize sensor");
}
}
void loop() {
// Read all parameters
if (soilSensor.readAllVariables()) {
// Display the readings
Serial.print("Temperature: ");
Serial.print(soilSensor.getTemperature());
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(soilSensor.getHumidity());
Serial.println(" %");
// ... and so on for other parameters
}
delay(5000); // Wait 5 seconds before next reading
}#include <SoilSensor.h>
// Define custom pins
#define RX_PIN 16
#define TX_PIN 17
// Create a sensor instance with custom pins
SoilSensor soilSensor(Serial2, RX_PIN, TX_PIN);
void setup() {
Serial.begin(115200);
// Initialize the sensor
soilSensor.begin(9600);
}
// ... rest of the codeSoilSensor(HardwareSerial& serial): Constructor using default pinsSoilSensor(HardwareSerial& serial, int rxPin, int txPin): Constructor using custom pinsbool begin(uint32_t baudRate = 4800): Initialize the sensor with the specified baud rate
bool readAllVariables(): Read all soil parameters at oncebool readTemperature(): Read only temperaturebool readHumidity(): Read only humiditybool readEC(): Read only electrical conductivitybool readPH(): Read only pHbool readNitrogen(): Read only nitrogenbool readPhosphorus(): Read only phosphorusbool readPotassium(): Read only potassium
float getTemperature(): Get the last temperature reading (°C)float getHumidity(): Get the last humidity reading (%)float getEC(): Get the last electrical conductivity reading (µS/cm)float getPH(): Get the last pH readingfloat getNitrogen(): Get the last nitrogen reading (mg/kg)float getPhosphorus(): Get the last phosphorus reading (mg/kg)float getPotassium(): Get the last potassium reading (mg/kg)
The library comes with several examples demonstrating different features:
BasicReading: Simple example showing how to read all parametersCustomPins: Shows how to use custom RX and TX pins on ESP32IndividualReadings: Demonstrates reading parameters individuallyReadingsSerial: Reads parameters on demand by sending serial commands
SoilSensor/
├── examples/
│ ├── BasicReading/
│ │ └── BasicReading.ino
│ ├── CustomPins/
│ │ └── CustomPins.ino
│ ├── IndividualReadings/
│ │ └── IndividualReadings.ino
│ └── ReadingsSerial/
│ └── ReadingsSerial.ino
├── src/
│ ├── SoilSensor.cpp
│ └── SoilSensor.h
├── keywords.txt
├── library.properties
├── LICENSE
├── README.md
├── SoilSensor.cpp
└── SoilSensor.h
This library is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues or have questions, please open an issue.
