Problem with servo and temperature sensor

Hi
i am building an egg incubator
i am encountering a problem when:
When the servo motor is on i do not get readings from my dht22 temp & hum sensor.
Once the servo stops i get readings normaly.
I browsed in here for a bit and found out that i need a separate power supply for the servo, most people suggest a 4x AA battery holder while others say that this is more than enough and it will burn the servo. So my question is is it safe to use the 4x AA battery holder?
Below is my code (i have all the parts finished but i add little by little t my main code and test)

//Libraries
#include <Wire.h>
//This library allows you to communicate with I2C/TWI devices.
#include <LiquidCrystal_I2C.h>
//This library allows you to communicate with your LiquidCrystalDevice
#include <dht.h>
//This library allows you to communicate with your DHT22 sensor
#include <Servo.h>
//This library allows you to communicate with your servo motor

//Variables
float hum;  //Stores   humidity value
float temp; //Stores temperature value
int degrees;//Stores the value of degrees







//Constants
#define DHTPIN 2 //The pin 2 is used for DHT22
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define DHTTYPE DHT22 //The type of DHT sensor we are using  
dht DHT; //Initialize the DHT sensor
Servo SERVO;
#define RELAYPIN 10 //The pin 10 is used for the Relay
#define SERVOMOTOR 11 //The pin 11 is used for the servo motor 

void setup() {
  Serial.begin(9600); //Allows us to vie our temp and hum values from the serial Monitor
  lcd.begin(16, 2); // This function initiates our lcd 16 is the number of columns and 2 is the number of rows.
  lcd.init(); //This function initializes the interface to the LCD
  lcd.clear(); // This function clears the LCD screen and positions the cursor in the upper-left corner
  lcd.backlight(); //This function turns on the LCD backlight
  pinMode(RELAYPIN, OUTPUT); //Declaring the relay pin as an output
  pinMode(SERVOMOTOR, OUTPUT); //Declaring the servomotor pin as an output
  SERVO.attach(11);
}
void loop() {
  byte CelsiusSymbol[] = {
    0b11100,
    0b10100,
    0b11100,
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00000
  };
  int chk = DHT.read22(DHTPIN);
  //Read data and store it to variables hum and temp
  hum = DHT.humidity;
  temp = DHT.temperature;
  //Print temp and humidity values to LCD
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temp);
  lcd.print('c');
  lcd.createChar(0, CelsiusSymbol ); // create a new custom character
  lcd.setCursor(11, 0); // move cursor to (11, 0)
  lcd.write((byte)0);  // print the custom char at (11, 0)
  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(hum);
  lcd.print("%");
  delay(2000); //Delay 2 sec between temperature/humidity check.


    if (millis()/1000 < 60) {
      for (degrees = 90; degrees <= 135; degrees += 9) {
        SERVO.write(degrees);
        delay(2000);
      }
      delay(8000);
      for (degrees = 135; degrees >= 90; degrees -= 9) {
        SERVO.write(degrees);
        delay(2000);
      }
      for (degrees = 90; degrees >= 45; degrees -= 9) {
        SERVO.write(degrees);
        delay(2000);
      }
      delay(8000);
      for (degrees = 45; degrees <= 90; degrees += 9) {
        SERVO.write(degrees);
        delay(2000);
      }

    } else {
      SERVO.write(90);

    }


  
  //Real times for egg incubator above times are test times
  /*if (millis()/1000 < 1555200) {
      for (degrees = 90; degrees <= 135; degrees += 9) {
        SERVO.write(degrees);
        delay(2000);
      }
      delay(180000);
      for (degrees = 135; degrees >= 90; degrees -= 9) {
        SERVO.write(degrees);
        delay(2000);
      }
      for (degrees = 90; degrees >= 45; degrees -= 9) {
        SERVO.write(degrees);
        delay(2000);
      }
      delay(180000);
      for (degrees = 45; degrees <= 90; degrees += 9) {
        SERVO.write(degrees);
        delay(2000);
      }

    } else {
      SERVO.write(90);

    }*/

    


  
}

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


Using delay( ) in your sketches is almost never satisfactory.

A non blocking delay using the millis( ) function is what you should be using.

Four AA batteries is 4 * 1.5v = 6 volts, most servos work with 4.8V to 6V.

here is the servo i am using

Four AA batteries will work.

Yes, that is a very popular power supply for a servo if the servo is rated for 6V. Typical servos are rated 4.8V to 6V or 4.8V to 7.2V. Consult the data sheet for your servo to confirm.

thank you do i just conect the vcc and ground directly to the battery holder or is the wireing more complex. Will four batteries be enough to get me through the 21 days required to hatch the chicken eggs?

If you used a 5V cellphone charger for the servo, you could connect the charger directly to the servo; the servo would then be constantly powered.

Connecting batteries permanently will eventually kill the batteries.

Battery operation for permanent operation might include a way of turning the servo power off when the servo is not needed.

Remember, batteries will eventually die :frowning:

It’s a guess, but since you are heating the unit, then you must have AC power available, if so, suggest you use a 5v cellphone charger etc.

Connect battery power source ground to Arduino ground and servo ground.

Thank you so much
Also i will add 1 pc fan to spread the heat l.
Will i encounter the same problems with the servo? Should i have 3 celllphone chargers?
( 1 for the arduino 1 for the seevo and 1 for the pc fan)

If the fan is 5v the same 5v power supply can be used for all 3 components.

  • Do you have any transistors ?

  • What fan are you using ?

  • What Arduino do you have ?

I do have transistors an am using arduino uno r3
Processing: 20230423_212406.jpg...

Not able to see the link to the fan.

  • What transistors do you have ?

  • Why have you undertaken this project ?

It is a picture that shows the fan and the transistors

Your fan is 12v @ 200mA.

If you insist on using this fan, you will need a 12v and 5v power supply.

  • What transistors do you have ?

  • Show us your proposed schematic?

  • Why have you undertaken this project ?

I do not know the things in the picture were given to me by a friend. I took this project because last year my peahen laid eggs but stoped incubating the eggs once the first one hatched and aperantly its the way with all peahens. But just to be safe i will first try using the machine with chicken eggs and then ajust it to take peacock eggs

You will need a 12v and 5v power supply.

  • What transistors do you have ?

  • Show us your proposed schematic?

  • How will you control the heating element ?


That servo is not the best of quality for this project.

Metal gear servos would be better.

Show us how you will connect the servo to the mechanism.

I found out that the transistor is a BC108
I will heat the eggs by using a heating lamp that will be connected to the arduino via relay and there will be a cable that will connect the relay to a wall outlet

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.