Home BARON.BE

MySonarJC Library

(Open source avaible on https://github.com/ponslet/MySonarJC )

Multiple Ultrasonic Sensors with Configurable Parameters



Features

Compatible Sensors

Installation

1. Download the MySonarJC library
2. Include it in your Arduino project using:
#include "MySonarJC.h"

API Reference

Class Initialization

MySonarJC myObjSonar;

Methods

  • begin()
    void begin(int numberOfSensors);

    Initializes the sonar system with the specified number of sensors.

  • parameters()
    void parameters(int index, int rxPin, int txPin, 
        int minDistance = 5, int maxDistance = 300, 
        int maxRetry = 2, int fineTuningPulse = 200, 
        bool debug = false);

    Configures individual sensor parameters.

  • distance()
    int distance(int index);

    Reads the distance from a specified sensor. Returns distance in centimeters.

Example Usage

#include "MySonarJC.h"

// Pin definitions
#define TRIG_PIN1 5
#define ECHO_PIN1 35
#define TRIG_PIN2 4
#define ECHO_PIN2 26

MySonarJC sonar;

void setup() {
    Serial.begin(115200);
    // Initialize for 2 sensors
    sonar.begin(2);
    
    // Configure sensors
    sonar.parameters(0, TRIG_PIN1, ECHO_PIN1, 23, 300); // JSN-SR04T
    sonar.parameters(1, TRIG_PIN2, ECHO_PIN2); // HC-SR04
}

void loop() {
    int distance1 = sonar.distance(0);
    int distance2 = sonar.distance(1);
    Serial.println("Distance 1: " + String(distance1) + " cm");
    Serial.println("Distance 2: " + String(distance2) + " cm");
    delay(50);
}

Fine-Tuning Guidelines

1. Pulse Timing Adjustment

  • Start with default value (200)
  • Decrease if sensor responds quickly
  • Increase if readings are unstable
  • Valid range: 50-300

2. Retry Settings

  • Default: 2 retries
  • Increase for noisy environments
  • Decrease for faster readings

3. Distance Range

  • JSN-SR04T: 23-300cm
  • HC-SR04: 5-300cm
  • RCWL-1670: 5-300cm

Debug Mode

sonar.parameters(0, TRIG_PIN, ECHO_PIN, 5, 300, 2, 200, true);

Enable debug mode to monitor echo duration values in Serial monitor for optimization.

Performance Considerations

Limitations

Contributing