This is a simple demonstration of the SDI-12 library for Arduino.
It uses the blocking SDI12Recorder API to discover and identify all sensors active on any pin on your board.
Each sensor should have a unique address already - if not, multiple sensors may respond simultaneously to the same request and the output will not be readable by the Arduino.
To address a sensor, please see Example B: b_address_change.ino
PlatformIO Configuration
1; PlatformIO Project Configuration File
4description = SDI-12 Library Example C: Searching all Addresses for Sensors
5src_dir = .piolibdeps/Arduino-SDI-12_ID1486/examples/c_check_all_addresses
The Complete Example
2 * @example{lineno} c_check_all_addresses.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Kevin M.Smith <SDI12@ethosengineering.org>
8 * @brief Example C: Check all Addresses for Active Sensors and Print Status
10 * This is a simple demonstration of the SDI-12 library for Arduino.
12 * It searches the configured pin and address ranges and prints identification
13 * information for each active sensor.
14 * THIS CAN BE *REALLY* SLOW TO RUN!!!
16 * Each sensor should have a unique address already - if not, multiple sensors may
17 * respond simultaneously to the same request and the output will not be readable
20 * To address a sensor, please see Example B: b_address_change.ino
23#include <SDI12Config.h>
24#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR)) && !defined(SDI12_INTERNAL_PCINT)
25#include <EnableInterrupt.h>
28#include <SDI12Recorder.h>
31#define SDI12_DATA_PIN 7
33#ifndef SDI12_POWER_PIN
34#define SDI12_POWER_PIN 22
36#ifndef SDI12_TERMINAL_BAUD
37#define SDI12_TERMINAL_BAUD 115200
39uint32_t serialBaud = SDI12_TERMINAL_BAUD; /*!< The baud rate for the serial port */
40int8_t powerPin = SDI12_POWER_PIN; /*!< The sensor power pin (or -1) */
41int8_t firstPin = 3; /*! change to lowest pin number to search on your board */
42int8_t lastPin = 7; /*! change to highest pin number to search on your board */
43int8_t firstAddress = 0; /* The first address in the address space to check (0='0') */
44int8_t lastAddress = 61; /* The last address in the address space to check (61='z') */
47 * @brief gets identification information from a sensor, and prints it to the serial
50 * @param recorder the SDI-12 recorder instance
51 * @param address a character between '0'-'9', 'a'-'z', or 'A'-'Z'.
53void printInfo(SDI12Recorder& recorder, char address) {
54 SDI12Recorder::Identification sensorInfo;
55 if (recorder.identify(address, sensorInfo)) {
56 recorder.printSensorInfo(sensorInfo, Serial);
58 recorder.printRecorderResult(Serial);
63void scanAddressSpace(SDI12Recorder& recorder) {
64 // Quickly scan the address space
65 for (int8_t i = firstAddress; i <= lastAddress; i++) {
66 char addr = SDI12Address::fromIndex(i);
67 Serial.print(F("i="));
69 Serial.print(F(" addr="));
71 if (recorder.requestAcknowledgement(addr)) { printInfo(recorder, addr); }
76 Serial.begin(serialBaud);
77 while (!Serial && millis() < 10000L);
78 char tbuf[3] = {'\0'};
81 F("//\n// Start Search for SDI-12 Devices \n// -----------------------"));
85 Serial.print(F("Powering up sensors with pin "));
86 Serial.print(String(powerPin));
87 Serial.println(F(", wait 10s..."));
88 pinMode(powerPin, OUTPUT);
89 digitalWrite(powerPin, HIGH);
92 Serial.println(F("Wait 5s..."));
96 for (int8_t pin = firstPin; pin <= lastPin; pin++) {
97 if (pin != powerPin) {
98 Serial.print(F("Opening SDI-12 bus on pin "));
100 Serial.print(F(" ("));
101 Serial.print(itoa(pin, tbuf, 10));
103 Serial.println(F("..."));
105 SDI12Recorder mySDI12Recorder(mySDI12);
107 delay(500); // allow things to settle
109 scanAddressSpace(mySDI12Recorder);
115 F("\n//\n// End Search for SDI-12 Devices \n// ---------------------"));
118 digitalWrite(powerPin, LOW);