AJ-SR04M Waterproof ultrasonic sensor testing TrigEco Mode-1 and UART Communication mode-4 code









1. TrigEco mode-1 code-

>>TrigEco mode-1 Minimal code-

#define ECHOPIN 5
#define TRIGPIN 18

void setup() {
  Serial.begin(115200);
  pinMode(ECHOPIN, INPUT);     // Fixed: Standard input without pull-up
  pinMode(TRIGPIN, OUTPUT);
}

void loop() {
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);       // Fixed: Standard 10µs trigger pulse
  digitalWrite(TRIGPIN, LOW);
 
  // 30,000 µs (30ms) timeout allows measurement up to ~5 meters
  long duration = pulseIn(ECHOPIN, HIGH, 50000);
  int distance = duration / 58;

  Serial.print(distance);
  Serial.println(" cm");
  delay(50);
}





>>TrigEco mode-1 Full code with Display-

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED Display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Sensor Pins
#define ECHOPIN 5
#define TRIGPIN 18

void setup() {
  Serial.begin(115200);
  pinMode(ECHOPIN, INPUT);    
  pinMode(TRIGPIN, OUTPUT);

  // Initialize the OLED display (I2C address 0x3C)
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while(1);
  }

  // Initial display splash screen
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(10, 20);
  display.println("Fiox Water Sensor");
  display.setCursor(20, 40);
  display.println("Initializing...");
  display.display();
  delay(1500);
}

void loop() {
  // Trigger the ultrasonic pulse
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);      
  digitalWrite(TRIGPIN, LOW);
 
  // Read echo pulse (100ms timeout for extended range testing)
  long duration = pulseIn(ECHOPIN, HIGH, 50000);
  int distance = duration / 58;

  // Print to Serial Monitor
  Serial.print(distance);
  Serial.println(" cm");

  // Update OLED Display
  display.clearDisplay();
 
  // Header text
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("AJ-SR04M Distance");
 
  // Draw a dividing line
  display.drawFastHLine(0, 12, 128, SSD1306_WHITE);

  // Big Distance Text
  display.setTextSize(2);
  display.setCursor(0, 25);
  display.print("Dist:");
  display.setCursor(60, 25);
  display.print(distance);
 
  display.setTextSize(1);
  display.setCursor(105, 30);
  display.print("cm");

  // Push updates to screen
  display.display();
 
  delay(100);
}
















2. UART Communication Mode-4 code-

>connect 47k resistance at R19
>upload code
>power on off, reset may not work

>>UART Communication Mode-4 Minima code-

#include <SoftwareSerial.h>

#define RX_PIN 16
#define TX_PIN 17

SoftwareSerial mySerial(RX_PIN, TX_PIN);

byte packet[4];
int packetIndex = 0;
unsigned long lastByteTime = 0;
unsigned long lastTriggerTime = 0;
const unsigned long triggerInterval = 100;

void setup() {
  Serial.begin(115200);
  mySerial.begin(9600);
  Serial.println("AJ-SR04M Serial Only Initialized");
}

void loop() {
  if (millis() - lastTriggerTime >= triggerInterval) {
    lastTriggerTime = millis();
    mySerial.write(0x01);
  }

  while (mySerial.available() > 0) {
    byte incomingByte = mySerial.read();

    if (millis() - lastByteTime > 100) {
      packetIndex = 0;
    }
    lastByteTime = millis();

    if (packetIndex == 0) {
      if (incomingByte == 0xFF) {
        packet[0] = incomingByte;
        packetIndex = 1;
      }
    } else {
      packet[packetIndex] = incomingByte;
      packetIndex++;

      if (packetIndex == 4) {
        byte d1 = packet[1];
        byte d2 = packet[2];
        byte checksum = packet[3];
        byte calculatedChecksum = d1 + d2;

        if (checksum == calculatedChecksum || checksum == (calculatedChecksum - 1)) {
          int distance_mm = (d1 << 8) + d2;
          int distance_cm = distance_mm / 10;

          Serial.print("Distance: ");
          Serial.print(distance_cm);
          Serial.println(" cm");
        }
        packetIndex = 0;
      }
    }
  }
}

















>>UART Communication Mode-4 full code with display-

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED Display Settings (0.96 inch, 128x64 resolution)
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
#define SCREEN_ADDRESS 0x3C // Common I2C address for 0.96" OLED (try 0x3D if it doesn't work)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// SoftwareSerial Pins for AJ-SR04M Mode 4
#define RX_PIN 16   // Connects to Sensor TX
#define TX_PIN 17  // Connects to Sensor RX

SoftwareSerial mySerial(RX_PIN, TX_PIN);

byte packet[4];
int packetIndex = 0;
unsigned long lastByteTime = 0;
unsigned long lastTriggerTime = 0;
const unsigned long triggerInterval = 100; // Trigger every 1 second for Low-Power Mode 4

void setup() {
  Serial.begin(115200);
  mySerial.begin(9600);
 
  // Initialize I2C and OLED Display
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed!"));
    for(;;); // Don't proceed, loop forever
  }

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 10);
  display.println("AJ-SR04M Mode 4");
  display.setCursor(0, 30);
  display.println("Initializing...");
  display.display();
  delay(1500);

  Serial.println("AJ-SR04M Mode 4 (47k ohm) & OLED Initialized");
}

void loop() {
  // Step 1: Send trigger byte 0x01 periodically (non-blocking)
  if (millis() - lastTriggerTime >= triggerInterval) {
    lastTriggerTime = millis();
    mySerial.write(0x01);
  }

  // Step 2: Non-blocking state-machine to read incoming response bytes
  while (mySerial.available() > 0) {
    byte incomingByte = mySerial.read();

    // If there is a pause longer than 100ms, reset the packet index to re-sync
    if (millis() - lastByteTime > 100) {
      packetIndex = 0;
    }
    lastByteTime = millis();

    // Look for the 0xFF header
    if (packetIndex == 0) {
      if (incomingByte == 0xFF) {
        packet[0] = incomingByte;
        packetIndex = 1;
      }
    }
    // Collect the remaining 3 bytes
    else {
      packet[packetIndex] = incomingByte;
      packetIndex++;

      // Once we have a full 4-byte packet
      if (packetIndex == 4) {
        byte d1 = packet[1];
        byte d2 = packet[2];
        byte checksum = packet[3];

        byte calculatedChecksum = d1 + d2;

        // Validate checksum with the module's offset tolerance
        if (checksum == calculatedChecksum || checksum == (calculatedChecksum - 1)) {
          int distance_mm = (d1 << 8) + d2;
          int distance_cm = distance_mm / 10;

          // Print to Serial Monitor
          Serial.print("Distance: ");
          Serial.print(distance_cm);
          Serial.println(" cm");

          // Update OLED Display
          display.clearDisplay();
         
          // Title
          display.setTextSize(1);
          display.setCursor(0, 0);
          display.print("AJ-SR04M Distance");

          // Line separator
          display.drawLine(0, 12, 128, 12, SSD1306_WHITE);

          // Distance Value in Large Text
          display.setTextSize(2);
          display.setCursor(10, 28);
          display.print(distance_cm);
          display.setTextSize(1);
          display.print(" cm");

          display.display();
        }

        // Reset index to look for the next packet header
        packetIndex = 0;
      }
    }
  }
}





Comments