Thanks for the replies everyone.
usually the DHT22 requires a pull up to VCC:
I have tried that, by connecting the resistor to the vcc pin and the data pin but I’m not sure if that was the right place for it. I can give it another go.
Are you sure it is not detecting you moving around?
Yes, I faced it directly away from me and my room has nothing beside me moving around in it. I even covered it in a cardboard box for a few minutes and it still just regularly switches between 0s and 1s.
The DHT22 protocol is “bit-banged” which is susceptible to timing errors leading to false or invalid values. If you don’t mind “giving up” two GPIO pins plus a third for the actual DHT sensor, the DHT22GEN3_RK library uses the I2S peripheral and DMA to interface with the DHT22 without any bit-banging. For me, it has yielded very good and consistent results.
Thanks, I have plently of free pins so I’ll give that a try. Do you have any links to guides or anything to get me started with that?
Additionally, I am not sure why you use “const” declarations for your local variables.
I’ve just adopted the convention of consting everything. It declares intent and means I don’t accidently change variables I don’t mean to . You can be sure that if something in my code isn’t const that means it’s meant to change. That’s all there is to it really.
Also, in loop(), you may want to consider using “sprintf()” and C-strings instead of
std::to_string() which uses dynamic memory allocation which can lead to heap fragmentation.
Well, I could be wrong but I think what I’m doing will benefit from small string optimization, so there won’t be any heap allocations anyway. In Visual Studio’s implementation of the C++ standard library, strings less than 16 characters are stack allocated. I’m compiling with VS Code and just assuming it’ll be similar. Honestly, I’m not too concerned about performance anyway.
How quickly are you polling the DHT22? I’ve seen issues with that (and the DHT11) if you poll more than once per minute. I get the same using a Pi.
I’m polling ever 100ms so I can certainly try doing it less frequently. I’m only doing it so often because I need to detect motion so I thought I might as well just do the DHT22 at the same time. It won’t be difficult to change the code a little and poll less frequently so I can try that.
Ok, because no one is saying that there’s an issue with using two sensors, so I’ll take that to mean it’s a problem with how I have things connected or with my code. I’ll try using a pullup resistor for DHT22 and poll less frequently. If that doesn’t work, I’ll try what peekay123 suggested. As for what to do about the motion sensor… well I still have no idea I’m afraid. I have tried using a resistor between ground on it and ground on the Argon but that didn’t help.