Page 1 of 1

Using two sensors, want to read every second from one, but only every minute for the other. How can I do this?

Posted: Sun Sep 17, 2023 8:06 pm
by lazloman
Sorry for the long title.
I have two sensors, a BH1750 light sensor and DHT11 temperature and humidity sensor running on an esp32 (wroom32). I want to read from the light sensor every second, but I only want to read from the DHT11 every minute. The methods for reading the sensors are separate. I can add a delay(1000) for the light sensor's method, but how can I delay reading from the DHT11 for one minute? I'm new to the platform, but I've looked around the 'net and can't find any similar situations. I also considered interrupts, but that seems impractical as I'm sure reading from sensors involves interrupts, which would make that problematic. So, is this even possible? If so, how do I go about it?
Thanks

Re: Using two sensors, want to read every second from one, but only every minute for the other. How can I do this?

Posted: Mon Sep 18, 2023 2:41 am
by ESP_Sprite
Simple and stupid way: have a counter and increase it every time you read from the light sensor. Then only read the DHT11 (and reset the counter) once the counter reaches 60.

Re: Using two sensors, want to read every second from one, but only every minute for the other. How can I do this?

Posted: Wed Sep 20, 2023 5:06 pm
by lazloman
Thanks for the reply. I tried something similar and what you recommend is also given as an option here:

https://www.digikey.com/en/maker/blogs/ ... o-sketches

I have yet to try your specific idea, but conceptually, it appears to be effectively the same. But I'll try it anyway.

Specifically what happens is that there are no readings from any of the sensors after the initial read, if any value over 30 seconds is used. (30 seconds is not reliable either)

I wonder if it has to do with the sensors. Do I need to 'kickstart' the sensors again? In setup there is a begin() call for each sensor. Should I instead call begin() just before each read?

Time for more empirical study, I guess.
Thanks again.

Re: Using two sensors, want to read every second from one, but only every minute for the other. How can I do this?

Posted: Fri Sep 22, 2023 12:00 am
by lazloman
Actually, I got it to works using a timer. Not sure what I did wrong before, but I'm getting temp/humidity readings only every 3 minutes, while the lux readings happen every second.
Thanks

Re: Using two sensors, want to read every second from one, but only every minute for the other. How can I do this?

Posted: Fri Sep 22, 2023 3:23 pm
by lbernstone
The url you posted is very avr specific. There are a lot of very useful examples included with the arduino-esp32 board. Here is an esp32 specific example. https://github.com/espressif/arduino-es ... linker.ino

Re: Using two sensors, want to read every second from one, but only every minute for the other. How can I do this?

Posted: Fri Sep 22, 2023 3:36 pm
by lazloman
Thank you, I'll check them out.