DHT11 (temp/humidity sensor) error messages while using servo

So I have this issue where whenever I use a servo, my DHT11 sensor starts giving some fail to read messages.

If I remove the servo from the code, the sensor works 100%. But as soon as I attach the servo (without writing a position to it) it will cause these sensor read errors.

I've spent a lot of time experimenting, googling, and trying different stuff out to resolve it, but I just don't seem to get it.

The libraries i use are:
<SimpleDHT.h>
<Servo.h>

And i use a small 9g HEXTRONIC servo.
In my project I also use a 1602 LCD-display (with i2c module), 2 relays and some push buttons.
I use serial.prints in my code. I also use breakpoints in visual studio to follow whats happening.

Some stuff I've tried already:

-It's not because of voltage drops, I use capacitors and I've measured the voltage to float between 5.10 -> 5.15v while the servo is working. I've also tried to power the servo through another power supply.

-I've tried to change the sensor and servo pins on the arduino board.

-I use the DHT11-code from the default-example, with the only difference that I use millis() and not a delay() to control the time between measurements.

-I've tried detaching the servo between movements, but as I mentioned, just attaching it causes sensor failures. And even if I'm not sending any positions to the servo, it still makes small adjustment moves from time to time, so it seems to get signals anyway.

-I've tried to fiddle around with interrupts: for example disabling and enabling it while reading the sensor (caused the program to freeze) or while moving servo (seemed to not change anything). But I've never used interrupts before so I'm not really sure what i'm doing here. I just read somewhere that it might be some clash or whatever so i gave it a try.

-I've tried to alter the code in any way I could imagine would help or change anything.

The failures I get from the sensor are these:
Read DHT11 failed, err=4114
Read DHT11 failed, err=3090
Read DHT11 failed, err=-237
Read DHT11 failed, err=21

I've tried to look at the library to see what it means, but the code there, well it's a bit above my level.

My sketch is pretty big, about 2000 lines, but if anyone think it might help, i can post it.

I been trying so many technical or specific fixes at this point, that I feel a bit too zoomed in at the problem. And Google don't seem to give me much help either, which is suspicious, it's like I don't understand the problem or not googling the right thing. Servos has always been one of my biggest issues with arduinos, they always causes problems for me. But when i google about servo issues, it seems there are not a lot of people having the same issues. So it makes me think I've missed something.

I could imagine the problem being something way more elementary like, "oh, ofc you cant use a servo and the dht11-sensor, they both use 'whatever' which doesn't work"

Ok, anyway, this got a bit long, sorry. Any help would be much appreciated !

greetings from Sweden

I also use breakpoints in visual studio to follow whats happening.

I'm wondering how Visual Studio comes into that equation. You cannot set breakpoints in Visual Studio for Arduino code so it might be interesting what code you're debugging there. At least it must be independent from the Arduino problem.

Please post a wiring diagram of the setup when the servo is "attached" and one when the servo is not attached (but the DHT11 is working).

pylon:
I'm wondering how Visual Studio comes into that equation. You cannot set breakpoints in Visual Studio for Arduino code so it might be interesting what code you're debugging there. At least it must be independent from the Arduino problem.

Please post a wiring diagram of the setup when the servo is "attached" and one when the servo is not attached (but the DHT11 is working).

What do you mean? I use breakpoints and tracepoints all the time for my arduino code. But it has always worked really well so I don't think that's the problem. But what do I know, I guess :slight_smile:

I meant attached as in I've let the program run the servo attach() command ..

As for wiring diagram, the servo and the DHT11 is connected straight to the arduino, easiest way i suppose. I have a power rail though, which they both share with everything else I'm powering, ie displays, relays, buttons and so on.

What do you mean? I use breakpoints and tracepoints all the time for my arduino code. But it has always worked really well so I don't think that's the problem. But what do I know, I guess

That means you're not running the code on the Arduino but in some emulation? The Arduino doesn't support breakpoints and the like as you have only a serial connection to it. I'm not aware of a Visual Studio integration of the Arduino framework. Please provide links to that integration.

As for wiring diagram, the servo and the DHT11 is connected straight to the arduino, easiest way i suppose.

If I'm asking for a wiring diagram I don't expect you to tell me that you did everything a straight way. I want to see every connection you made in your setup.

I meant attached as in I've let the program run the servo attach() command ..

In that case you activate the interrupts. As the SimpleDHT library doesn't disable interrupt during the DHT read out it may loose some data. Try the DHT_sensor_library from Adafruit as it correctly disables interrupts during timing critical code parts. As a drawback you may get more noise from the servo as the timing there isn't kept anymore.
To fix that completely, buy an I2C sensor (I can recommend the SHT31 for temperature/humidity, as a benefit you get much more accuracy). The timing on the I2C bus is enforced by the hardware so you don't get conflicts there.

@OP

What are the devices of the following you are using in your project?

Arduino UNO
or
Arduino NANO
or
Arduino MEGA
or
Arduino DUE

Arduino IDE
+
DHT11
+
Servo
+
I2CLCD

If you are using one or more of the devices, please post your full program codes with code tags.