Ultrasonic sensor sampling using millis()

Hi everyone, I am currently trying to make my sensor work without delay and with these parameters: sampling frequency of 100Hz using Serial.println (for testing purposes, this will serve to plot with Matlab later on) and a baudrate of 9600.

My code is here:

const int trigger = 2;
const int echo = 3;
long t;
long d;
int trigger_actual = 0;
int muestreo_actual = 0;
int pulso;
bool inicio = 1;

void setup(){
  Serial.begin(9600);
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  digitalWrite(trigger, LOW);
}

void loop(){
  if(inicio == 1){
    digitalWrite(trigger, HIGH);
    inicio = 0;      
  }
  if((micros() - trigger_actual) >= 10){
    pulso = micros() - trigger_actual;
    digitalWrite(trigger, LOW);    
    t = pulseIn(echo, HIGH); // Obtengo el ancho de pulso.
    d = t*0.034/2;  // Velocidad_sonido = 2d/t;
    digitalWrite(trigger, HIGH);
    trigger_actual = micros();
  }
  if((millis() - muestreo_actual) >= 10){
    Serial.println(d);
    muestreo_actual = millis();
  }
}

The problem is the following:
I get coherent readings when the distance is above 20cm, below this value these get weird, and ataround 15cm the RX led turns off and I start getting zeros for an interval, same happens for 5cm (zeros for a couple seconds before getting decent readings).

It is only after a minute or so of continuous testing with my hand (moving it closer and farther from the sensor at a certain speed) that the system stabilizes and I get accurate readings.

What is wrong with my code? Thanks so much for your attention in advance.

Edit: The SensorKalman.h tab that appears in the screesnshots is for later on, doesnt matter yet.

Echoes are going to linger in the air for up to 30 milliseconds. That means you shouldn't sample much faster than 30 Hz. If you want a faster sample rate, use Time-of-Flight optical distance sensors instead of ultrasonics.

Cell phone using helpers can't read the code....
Post it the proper way.

Sorry for that, already fixed it!

Hmm I changed the frequency to 25Hz, however the problem persists, at around 15cm the Rx led turns off and I get zeros, any other ideas? :frowning:

I'm not sure that code is okey.
An example code I used:

#define trigPin 12
#define echoPin 11

uint16_t getEchoDistance (void);

// --- Setup -----------------------------------
void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.begin(115200);
}

// --- Loop -----------------------------------
void loop() {
  Serial.println(getEchoDistance());
  delay(100);
}

uint16_t getEchoDistance (void) {
  unsigned long duration;
  uint16_t distance;
  digitalWrite(trigPin, LOW);
  delay(5);

  // trigger the sensor
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(8);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034 / 2);
  return distance;
}


You should have "trigger_actual", "muestreo_actual", and "pulso" declared as "unsigned long" to match the data type returned by micros(), millis(), and pulseIn() respectively.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.