Automatic AC Control Using Arduino and DS18B20 Temperature Sensor

Make your air conditioner turn on automatically when your room gets too hot? In this project, we’ll build a simple Automatic AC Controller using an Arduino Uno, a DS18B20 temperature sensor, and an IR LED.

The Arduino continuously monitors the room temperature. When the temperature rises above a limit (set by you in the code), it automatically sends the AC’s power command using an infrared signal, just like your remote control. This project is beginner-friendly and a great introduction to home automation.

Components Required

Code1:

#include <IRremote.hpp>

#define IR_RECEIVE_PIN 2

void setup() {
  Serial.begin(115200);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

  Serial.println("Ready to capture AC remote...");
}

void loop() {
  if (IrReceiver.decode()) {

    Serial.println();
    IrReceiver.printIRResultShort(&Serial);

    Serial.println();
    IrReceiver.printIRSendUsage(&Serial);

    Serial.println();
    Serial.println("Raw Timing Data:");

    IrReceiver.compensateAndPrintIRResultAsCArray(&Serial, true);

    Serial.println("\n--------------------------");

    IrReceiver.resume();
  }
}

Code 2:

#include <IRremote.hpp>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <TM1637Display.h>

// Pin Definitions
#define IR_SEND_PIN     4
#define ONE_WIRE_BUS    2   // DS18B20 data pin
#define TM1637_CLK      5   // TM1637 CLK pin
#define TM1637_DIO      6   // TM1637 DIO pin

// Temperature threshold set YOUR TEMPARATURE
#define TEMP_THRESHOLD  29.0

// How often to check temperature (milliseconds)
#define CHECK_INTERVAL  5000

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
TM1637Display display(TM1637_CLK, TM1637_DIO);

bool acIsOn = false;
unsigned long lastCheckTime = 0;

void sendACOn() {
  uint64_t tRawData[] = {
    0x6008A7836,
    0x5800000082800
  };

  IrSender.sendPulseDistanceWidthFromArray(
    38,
    950,
    650,
    1000,
    2600,
    1000,
    600,
    tRawData,
    119,
    PROTOCOL_IS_LSB_FIRST,
    0,
    0
  );
}

void setup() {
  Serial.begin(115200);

  IrSender.begin(IR_SEND_PIN);
  sensors.begin();

  display.setBrightness(7);  // Max brightness
  display.clear();

  Serial.println("System started. Monitoring temperature...");
}

void loop() {
  unsigned long currentTime = millis();

  if (currentTime - lastCheckTime >= CHECK_INTERVAL) {
    lastCheckTime = currentTime;

    sensors.requestTemperatures();
    float tempC = sensors.getTempCByIndex(0);

    if (tempC == DEVICE_DISCONNECTED_C) {
      Serial.println("ERROR: Temperature sensor not found!");
      display.clear();
      return;
    }

    // Display temperature in format 29:35
    int tempWhole = (int)tempC;                          // e.g., 29
    int tempDecimal = (int)((tempC - tempWhole) * 100);  // e.g., 35
    int tempDisplay = (tempWhole * 100) + tempDecimal;   // e.g., 2935
    display.showNumberDecEx(tempDisplay, 0b01000000, true, 4, 0); // shows 29:35

    Serial.print("Temperature: ");
    Serial.print(tempWhole);
    Serial.print(".");
    Serial.print(tempDecimal);
    Serial.println(" °C");

    // AC ON logic
    if (tempC >= TEMP_THRESHOLD && !acIsOn) {
      Serial.println("Temp >= 29°C → Turning AC ON");
      sendACOn();
      acIsOn = true;
    } else if (tempC < TEMP_THRESHOLD && acIsOn) {
      Serial.println("Temp < 30°C → Waiting for AC OFF command");
    } else {
      Serial.println(acIsOn ? "AC is already ON" : "AC is OFF, temp below threshold");
    }
  }
}

Circuit Connections

IR Receiver

IR Transmitter PinArduino Uno
OUTDigital Pin 2
GNDGND
Vcc5v

IR Transmitter

IR Transmitter PinArduino Uno
SDigital Pin 4
GNDGND
Vcc5v

DS18B20 Temperature Sensor

DS18B20 PinArduino Uno
VCC5V
GNDGND
SDigital Pin 2

Connect a 4.7kΩ resistor between the DATA and VCC pins.

TM1637 Display Module

TM1637 PinArduino Uno
VCC3.3V
GNDGND
CLKDigital Pin 5
DIODigital Pin 6

Applications

This project can be used in:

  • Smart home systems
  • Server rooms
  • Small offices
  • Electronics laboratories
  • Temperature-controlled environments

It can also be expanded to control fans, exhaust systems, or other appliances based on temperature conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *

More Articles & Posts