Smart Home Control — Arduino Mega simulation

Smart home control system with various sensors, buttons, and outputs controlled by an ESP32.

An interactive Arduino Mega circuit simulation you can run free in your browser on Velxio, by tareq-neoaz.

Components

Sketch code

#include <SPI.h>
#include <Wire.h>
#include <Keypad.h>
#include <Servo.h>
#include <DHT.h>
#include <SparkFun_AK975X_Arduino_Library.h>
// #include <Adafruit_PN532.h> // Uncomment when you add NFC

// --- TFT PINS ---
#define TFT_CS   53
#define TFT_DC   49
#define TFT_RST  48

// --- KEYPAD ---
const byte ROWS = 4; const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'}, {'4','5','6','B'},
  {'7','8','9','C'}, {'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 23, 24, 25}; 
byte colPins[COLS] = {26, 27, 28, 29}; 
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// --- SENSORS ---
#define DHT_PIN 2
DHT dht(DHT_PIN, DHT22);
#define PIR_PIN 3
#define DOOR_PIN 4
#define TRIG_PIN 5
#define ECHO_PIN 6
#define LDR_PIN A0
#define RAIN_PIN A1

// --- OUTPUTS ---
#define OUT_LIGHTS 7
#define OUT_FAN 8
#define OUT_AC 9
#define OUT_LOCK 10
#define OUT_BUZZER 11

Servo doorLock;
AK975X presenceSensor;

// --- STATES ---
bool lightsOn = false, fanOn = false, acOn = false;
bool isArmed = false;
bool manualLights = false, manualFan = false, manualAC = false;
bool personHome = false;
int nfcFails = 0;

// ==========================================
// RAW SPI & UI FUNCTIONS
// ==========================================
void cmd(uint8_t c) { digitalWrite(TFT_DC, LOW); digitalWrite(TFT_CS, LOW); SPI.transfer(c); digitalWrite(TFT_CS, HIGH); }
void data(const uint8_t *d, size_t n) { digitalWrite(TFT_DC, HIGH); digitalWrite(TFT_CS, LOW); for(size_t i=0; i<n; i++) SPI.transfer(d[i]); digitalWrite(TFT_CS, HIGH); }
void data8(uint8_t d) { data(&d, 1); }
void setWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
  uint8_t c[4] = { (uint8_t)(x0 >> 8), (uint8_t)x0, (uint8_t)(x1 >> 8), (uint8_t)x1 };
  uint8_t p[4] = { (uint8_t)(y0 >> 8), (uint8_t)y0, (uint8_t)(y1 >> 8), (uint8_t)y1 };
  cmd(0x2A); data(c, 4); cmd(0x2B); data(p, 4); cmd(0x2C);               
}
void fillRect(int x, int y, int w, int h, uint8_t r, uint8_t g, uint8_t b) {
  static uint8_t line[320 * 3];
  for (int i = 0; i < w; i++) { line[i*3]=r&0xFC; line[i*3+1]=g&0xFC; line[i*3+2]=b&0xFC; }
  setWindow(x, y, x + w - 1, y + h - 1);
  digitalWrite(TFT_DC, HIGH); digitalWrite(TFT_CS, LOW);
  for (int row = 0; row < h; row++) { for(int i=0; i<w*3; i++) SPI.transfer(line[i]); }
  digitalWrite(TFT_CS, HIGH);
}

struct Glyph { char ch; uint8_t col[5]; };
static const Glyph FONT[] = {
  { '0', { 0x3E, 0x51, 0x49, 0x45, 0x3E } }, { '1', { 0x00, 0x42, 0x7F, 0x40, 0x00 } },
  { '2', { 0x42, 0x61, 0x51, 0x49, 0x46 } }, { '3', { 0x21, 0x41, 0x45, 0x4B, 0x31 } },
  { '4', { 0x18, 0x14, 0x12, 0x7F, 0x10 } }, { '5', { 0x27, 0x45, 0x45, 0x45, 0x39 } },
  { '6', { 0x3C, 0x4A, 0x49, 0x49, 0x30 } }, { '7', { 0x01, 0x71, 0x09, 0x05, 0x03 } },
  { '8', { 0x36, 0x49, 0x49, 0x49, 0x36 } }, { '9', { 0x06, 0x49, 0x49, 0x29, 0x1E } },
  { 'A', { 0x7E, 0x11, 0x11, 0x11, 0x7E } }, { 'C', { 0x3E, 0x41, 0x41, 0x41, 0x22 } },
  { 'E', { 0x7F, 0x49, 0x49, 0x49, 0x41 } }, { 'F', { 0x7F, 0x09, 0x09, 0x

More projects by tareq-neoaz