Projects / Livestock shed heat index

Livestock shed heat index

An ESP32 with a DHT22 works out a temperature and humidity index for a cattle or goat shed and shows green, yellow or red lights as the heat stress risk rises.

Intermediate2.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Wire the sensor

    Pin 1 to 3V3, pin 2 to GPIO4, pin 4 to GND, 10 kilo-ohms from pin 2 to 3V3.

  2. ESP328.6 mA

    Wire the lights

    Green to GPIO25, yellow to GPIO26, red to GPIO27, each through 330 ohms, short legs to GND.

  3. ESP32

    Install the library

    DHT sensor library by Adafruit.

  4. ESP32

    Upload the sketch

    Open the Serial Monitor at 115200.

  5. ESP32

    Breathe on the sensor

    The humidity and the index rise.

  6. ESP328.7 mA

    Warm it with your hand

    The light moves towards yellow.

  7. ESP32

    Mount it

    In a ventilated box at animal height, in the shade.

In short

How do you build a livestock shed heat index?

An ESP32 with a DHT22 works out a temperature and humidity index for a cattle or goat shed and shows green, yellow or red lights as the heat stress risk rises. You need ESP32 DevKit V1, DHT22 Sensor, Resistor 10 kΩ, LED 5 mm red, LED 5 mm yellow, LED 5 mm green, Resistor 330 Ω × 3 and Breadboard 830 Tie Points. It takes 7 steps and about 140 minutes.

Level
Intermediate
Time
About 140 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
DHT22 pin 1 (VCC)ESP32 3V3Power at 3.3 V
DHT22 pin 2 (data)ESP32 GPIO4One-wire style data
DHT22 pin 4 (GND)ESP32 GNDCommon ground
10 kilo-ohm resistorBetween DHT22 data and 3V3Pull-up for the data line
Green LED long leg330 ohm resistor to ESP32 GPIO25Safe light
Yellow LED long leg330 ohm resistor to ESP32 GPIO26Alert light
Red LED long leg330 ohm resistor to ESP32 GPIO27Danger light
All LED short legsESP32 GNDCommon return

Code

Animals lose heat by panting and sweating, and both work less well when the air is warm and humid, so heat stress depends on temperature and humidity together. The temperature-humidity index, THI, puts the two in one number. The formula in the program is the one used for cattle. Below about 72 the animals are usually comfortable, between 72 and 78 they are under mild stress and giving less milk or gaining less weight, and from 79 up it is serious, so the program lights green, yellow or red. The numbers are rough guides that differ between breeds, goats and buffalo, and they are not a veterinary tool, so watch the animals as well, and talk to a vet about limits for yours. The DHT22 reads about every two seconds, and the program also remembers the highest value in each day so you can see how bad the hot spells are. Hang the sensor at the height of the animals, in the shade and away from walls that are hot in the sun, in a small box with air holes, out of reach of the animals, with the board and wires kept dry and protected from dust. When the light turns yellow, open vents, turn on fans and give water.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <DHT.h>

const int DHT_PIN = 4, GREEN = 25, YELLOW = 26, RED = 27;
DHT dht(DHT_PIN, DHT22);

float dayMax = 0;
unsigned long dayStart = 0;

// Temperature-humidity index, with temperature in degrees Celsius and humidity in percent
float thi(float c, float rh) {
  return (1.8 * c + 32.0) - (0.55 - 0.0055 * rh) * (1.8 * c - 26.0);
}

void setup() {
  pinMode(GREEN, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(RED, OUTPUT);
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  float c = dht.readTemperature();
  float rh = dht.readHumidity();
  if (isnan(c) || isnan(rh)) {
    Serial.println("Sensor read failed, check the wires");
    delay(2000);
    return;
  }

  float index = thi(c, rh);
  if (index > dayMax) dayMax = index;

  // Rough bands often used for dairy cattle: below 72 comfortable, 72 to 78 alert, 79 and over danger
  digitalWrite(GREEN, index < 72);
  digitalWrite(YELLOW, index >= 72 && index < 79);
  digitalWrite(RED, index >= 79);

  Serial.print(c, 1); Serial.print(" C  ");
  Serial.print(rh, 0); Serial.print(" %  THI ");
  Serial.print(index, 1);
  Serial.print("  highest today ");
  Serial.println(dayMax, 1);

  if (millis() - dayStart > 86400000UL) {          // a new day: report and restart the maximum
    Serial.print("DAY MAXIMUM ");
    Serial.println(dayMax, 1);
    dayMax = 0;
    dayStart = millis();
  }
  delay(5000);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

26.5 C  55 %  THI 74.3  highest today 74.3

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Cool dry roomGreen light, index below 72
Breathe on the sensorIndex rises for a moment
Warm hand near the sensorIndex goes up
Index above 72Yellow light
Index above 79Red light

If it does not work

Sensor read failed

Check the data wire on GPIO4 and the pull-up resistor.

No light

Check the LED leg direction and the resistors.

Index looks high

Keep the sensor out of the sun and away from hot walls.

Both lights on

The index is on a boundary, wait for the next reading.

Temperature reads too high

Keep the sensor away from the ESP32 and any heat.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the livestock shed heat index?

ESP32 DevKit V1, DHT22 Sensor, Resistor 10 kΩ, LED 5 mm red, LED 5 mm yellow, LED 5 mm green, Resistor 330 Ω × 3 and Breadboard 830 Tie Points.

How long does the livestock shed heat index take to build?

About 140 minutes for a intermediate maker, in 7 steps.

How do I know the livestock shed heat index works?

Cool dry room: Green light, index below 72. Breathe on the sensor: Index rises for a moment. Warm hand near the sensor: Index goes up.

Sensor read failed: what should I check?

Check the data wire on GPIO4 and the pull-up resistor.

No light: what should I check?

Check the LED leg direction and the resistors.

Index looks high: what should I check?

Keep the sensor out of the sun and away from hot walls.

Both lights on: what should I check?

The index is on a boundary, wait for the next reading.