Ultrasonic Sensor and Temperature Sensor Working Simultaneously

Hello World,

I am trying to combined both ultrasonic sensor and temperature sensor on my Arduino, I want to make these two sensor working simultaneously. For example, i set the ultrasonic detect range is 20cm, while waiting the ultrasonic sensor to detect the object, the temperature sensor is able to print the data on serial monitor. Any idea on this? what function should i use to make these two sensor work at the same time?

Thanks

hi,

I am trying to combined both ultrasonic sensor and temperature sensor on my Arduino, I want to make these two sensor working simultaneously. For example, i set the ultrasonic detect range is 20cm, while waiting the ultrasonic sensor to detect the object, the temperature sensor is able to print the data on serial monitor. Any idea on this? what function should i use to make these two sensor work at the same time?

Thanks

Look for "arduino nonblocking pulsein" for the ultrasonic sensor.

It is a mistake to try to print while monitoring sensors, because printing is slow and will interfere with sensor timing.

I'm really having a hard time seeing a relationship between temperature and distance.

Depending on what temperature sensor you are using, it can take quite a long time to read the temperature. Reading the temperature when the distance is 20, not 19 and not 21, might not be possible.

What temperature sensor ARE you using? What IS the relationship between temperature and distance? Specifically, why would you expect the temperature to be different because the distance is 20 vs. 19 or 21?

An ultrasound ping takes about 1.5 ms for a 20 cm distance round trip. Or about 20 ms for 3 meters (the typical limit).
Of course you can try and do a temperature reading and printing it to the Serial monitor (assuming your temperature sensor reacts that fast and the print command returns in time).
But isn't it easier to just do the two one after the other?

Any idea on this?

Yes. Stop cross-posting. Stick with ONE post in ONE part of the forum.

Indeed.
There are already some quite amusing alternative interpretations of what you wan with your quite ambiguous post in the other thread.

PaulS:
I'm really having a hard time seeing a relationship between temperature and distance.

Depending on what temperature sensor you are using, it can take quite a long time to read the temperature. Reading the temperature when the distance is 20, not 19 and not 21, might not be possible.

What temperature sensor ARE you using? What IS the relationship between temperature and distance? Specifically, why would you expect the temperature to be different because the distance is 20 vs. 19 or 21?

Hi, thanks for the reply, basically there is no relationship between temperature and distance. i want them to work independently. i just want the ultrasonic sensor to be ready to detect any object that passes through the sensor with less than 20cm of distance. And at the same time the temperature sensor can keep update on the surrounding temperature for every 10 minutes. By the way im using LM35 temperature sensor

This IS the Programming section of the forum. One of the requirements for posting HERE is that you POST YOUR CODE.

What doesn't seem to be working for you?

PaulS:
This IS the Programming section of the forum. One of the requirements for posting HERE is that you POST YOUR CODE.

What doesn't seem to be working for you?

Sorry i am a newbie in this forum and i will just focus on this thread. Here is my code :

void loop() {

     int value = analogRead(inPin); //temp
    float millivolts =(value/1024.0)* 5000; //temp
    float celsius = millivolts/10; //temp

  // Calculating the distance
  distance = duration * 0.034 / 2;

  safetyDistance = distance;
  if (safetyDistance <= 20) {
   
    Serial.print("Distance: ");
    Serial.println(distance)

  
    delay(1000);
 
  }
  else {
 
    Serial.print(celsius); //temp
    Serial.println("  degree Celsius"); //temp   
   
  delay(60000);
  }

}

I want to update on the temperature for every 1 minute so i put a delay(60000), but then after i put a delay , ultrasonic can't function, it can only be functioned after a minute. So how should i do so that i can make both sensors working simultaneously?

Thanks in advance!

Try this (first sticky at the top of this forum):
Using millis() for timing. A beginners guide

Threads merged.

So how should i do so that i can make both sensors working simultaneously?

You need to stop using the term "simultaneously", because the Arduino can not do two ore more things simultaneously, having only one processor.

It can do things in VERY quick succession, so that to us slow humans they appear to be happening "simultaneously".

kelvinswee94:
I want to update on the temperature for every 1 minute so i put a delay(60000), but then after i put a delay , ultrasonic can't function, it can only be functioned after a minute.

Of course. You ask your processor to sit and do nothing for 1 minute.

Use millis() based timing. See "blink without delay" for an example on how this works.