Skip to content

Instantly share code, notes, and snippets.

@Steve-Tech
Created June 7, 2025 08:35
Show Gist options
  • Save Steve-Tech/122ea44462681c819ddf90b59d3959c2 to your computer and use it in GitHub Desktop.
Save Steve-Tech/122ea44462681c819ddf90b59d3959c2 to your computer and use it in GitHub Desktop.
Casablanca IR (SM5021BL) Light & Fan Remote + DHT & LDR Arduino Program
// Casablanca IR Light & Fan Remote Arduino Program
// These remotes use the SM5021BL IC, the values are calculated as per the datasheet.
// This program also incorporates a DHT11 temp sensor and an LDR and prints the values over serial every 2 seconds.
// This program was also written years ago so it might be broken now.
#include <DHT.h>
#define SEND_PWM_BY_TIMER
#define DISABLE_CODE_FOR_RECEIVER
#define EXCLUDE_EXOTIC_PROTOCOLS
#define EXCLUDE_UNIVERSAL_PROTOCOLS
#define NO_LED_FEEDBACK_CODE
#include <IRremote.hpp>
#define debug false
#define DHTPIN 2
#define IR_SEND_PIN 3
#define LDR A0
#define DHTTYPE DHT11
const char sep = ',';
#define IR_PERIOD 1500
#define IR_SHORT IR_PERIOD/4
#define IR_LONG IR_PERIOD - IR_SHORT
#define IR_1 IR_LONG, IR_SHORT
#define IR_0 IR_SHORT, IR_LONG
// Head (3 bits) + Custom Code (2 bits)
#define IR_HEADER IR_1, IR_1, IR_0, IR_1, IR_1
const uint8_t carrier_freq = 38; // 38kHz carrier frequency
const uint8_t send_frames = 4;
// Header + 7 bits data
const uint16_t fanOff[] = {IR_HEADER, IR_0, IR_0, IR_0, IR_0, IR_0, IR_0, IR_1};
const uint16_t fanHi[] = {IR_HEADER, IR_0, IR_0, IR_0, IR_0, IR_0, IR_1, IR_0};
const uint16_t fanMed[] = {IR_HEADER, IR_0, IR_0, IR_0, IR_0, IR_1, IR_0, IR_0};
const uint16_t fanLow[] = {IR_HEADER, IR_0, IR_0, IR_0, IR_1, IR_0, IR_0, IR_0};
const uint16_t lightOff[] = {IR_HEADER, IR_0, IR_0, IR_1, IR_0, IR_0, IR_0, IR_0};
const uint16_t lightOn[] = {IR_HEADER, IR_0, IR_1, IR_0, IR_0, IR_0, IR_0, IR_0};
const uint8_t payloadSize = 24;
// 6 bits empty
const uint16_t synchTime = 9000;
unsigned long previousMillis = 0;
const long interval = 2000;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(LDR, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
#if debug == true
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing IR library version " VERSION_IRREMOTE));
#endif
dht.begin();
IrSender.begin(IR_SEND_PIN, ENABLE_LED_FEEDBACK);
#if debug == true
Serial.print(F("Ready to send IR signals at pin "));
Serial.println(IR_SEND_PIN);
#endif
}
// Send SM5021B Signals
void sendSignal(uint16_t payload[]) {
for (uint8_t i = 0; i < send_frames; i++) {
IrSender.sendRaw(payload, payloadSize, carrier_freq);
IrSender.customDelayMicroseconds(synchTime);
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
float h = dht.readHumidity();
float t = dht.readTemperature();
int l = analogRead(LDR);
Serial.print(t); // Temperature
Serial.print(sep);
Serial.print(h); // Humidity
Serial.print(sep);
Serial.println(l); // LDR
}
if (Serial.available() > 0) {
char command = Serial.read();
#if debug == true
Serial.print(command);
#endif
switch (command) {
case '0':
sendSignal(fanOff);
break;
case '1':
sendSignal(fanHi);
break;
case '2':
sendSignal(fanMed);
break;
case '3':
sendSignal(fanLow);
break;
case '4':
sendSignal(lightOff);
break;
case '5':
sendSignal(lightOn);
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment