0

So I am using a servo motor and 2 DHT11s in my Uno, when I attach them separately it works well. But when I try to attach both, servo working correctly but DHT11 giving weird number(DHT11 was side by side, one gives 22oC and another gives 30oC sometimes -999).

I am powering both devices by using 5V and GND from arduino to a breadboard and then DHT11 and Servo power pin to the breadboard. All signal pin are direct from devices to arduino.

Am I power both devices wrong? or is there's a way to solve this?

Thanks

EDIT: this is the code

#include <Servo.h>
#include <dht.h>

dht DHT;

#define DHT11_PIN 7
#define DHT11_PIN2 6
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }

  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  int chk2 = DHT.read11(DHT11_PIN2);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

For the servo, I used GWServo S03N STD.

  • Maybe the moving servo drains too much the power, so the DHT are underfed. Try powering the servo separately, or at least add a capacitor on the 5V on the breadboard (maybe 100nF in parallel to a 100+ uF) – frarugi87 Jun 9 '17 at 9:07
  • It could be a code error, but my crystal ball isn't working today and I can't see your code. It could be a circuit issue, but as you know my crystal ball is down so I don't know. It could be a power problem, but the force is not with me and I don't know what servo model you have so I don't know. The more complete the information you supply is, the faster and more accurate the answers will be. – Code Gorilla Jun 9 '17 at 11:25
  • @frarugi87 ah yeah, I will try that,thanks for the suggestions – Naufal B Jun 9 '17 at 11:38
  • 1
    @CodeGorilla sorry, I added the code and servo model. – Naufal B Jun 9 '17 at 11:39
  • @NaufalB - sorry to be a pain, but what DHT library are you using, it doesn't look like the AdaFruit one. Is it this one playground.arduino.cc/Main/DHT11Lib ? – Code Gorilla Jun 9 '17 at 11:43
1

Your code looks like there is no bug in there. You might want to look at using a library that lets you assign a pin in the constructor, so you don't need to read from a specific pin. There might be something internal going on that you don't know (or want to know) about. As for the movement of the servos do you realise you a rotating 181 degrees int p = 0; p <= 180 is 181 degrees, drop the = if you wanted 180. Also pos++ is the same as pos += 1 but requires less of those tiring key presses :)

You say that the DHT11s work on their own. The servo works on its own. So it not a hardware fault. I assume its not a wiring fault, but please double check. That means it has to be something to do with the power. If you are powering the servo from a digital pin then this is unlikely to work, try powering it from the 5V output pin on the other side of the board.

However if you are powering it from the 5V output pin and it doesn't work then you need to power it from a external source, a mobile phone charger will do (if you have one you can chop the plug off). The problem with using external PSU is you must tie the grounds together. Doing this means the Arduino and the external device agree what zero volts is and things work. This is easily done by taking the gnd of the PSU and putting it in the GND connector of the Arduino and the taking the GND for the servo from the other GND on the Arduino.

I hope that all makes sense, if not tell me what you need clarifying.

  • 1
    Yeah, I will try to power it from external source. Thank you for the suggestion, really help me :) – Naufal B Jun 9 '17 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.