0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program for Home Automation System

An Embedded C program for home automation can control devices like lights using GPIO pins and input sensors; for example, use if statements to turn on/off lights based on sensor input with code like if(sensor == ON) light = ON;.
📋

Examples

InputSensor input = ON
OutputLight turned ON
InputSensor input = OFF
OutputLight turned OFF
InputManual switch = ON
OutputLight turned ON
🧠

How to Think About It

To create a home automation program, first identify inputs like sensors or switches and outputs like lights or fans. Then write code to read inputs and control outputs accordingly using simple if conditions. The program runs in a loop to keep checking and updating device states.
📐

Algorithm

1
Initialize input and output pins
2
Continuously read sensor or switch input
3
If input indicates ON, set output pin HIGH to turn device ON
4
Else, set output pin LOW to turn device OFF
5
Repeat the process indefinitely
💻

Code

embedded_c
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB |= (1 << PB0); // Set PB0 as output (light)
    DDRC &= ~(1 << PC0); // Set PC0 as input (sensor)

    while(1) {
        if (PINC & (1 << PC0)) { // If sensor is ON
            PORTB |= (1 << PB0); // Turn light ON
        } else {
            PORTB &= ~(1 << PB0); // Turn light OFF
        }
        _delay_ms(100);
    }
    return 0;
}
Output
Light turns ON when sensor input is HIGH, OFF when LOW
🔍

Dry Run

Let's trace sensor input ON and OFF through the code

1

Initialize pins

PB0 set as output, PC0 set as input

2

Sensor input ON

PINC & (1 << PC0) is true, so PORTB |= (1 << PB0) sets PB0 HIGH

3

Light ON

Light connected to PB0 turns ON

4

Sensor input OFF

PINC & (1 << PC0) is false, so PORTB &= ~(1 << PB0) clears PB0

5

Light OFF

Light connected to PB0 turns OFF

StepSensor Input (PC0)Light Output (PB0)
1N/AConfigured
21 (ON)1 (ON)
31 (ON)1 (ON)
40 (OFF)0 (OFF)
50 (OFF)0 (OFF)
💡

Why This Works

Step 1: Pin Setup

We set the sensor pin as input and the light pin as output using DDRx registers to control hardware pins.

Step 2: Reading Sensor

The program reads the sensor state by checking the input pin with PINC & (1 << PC0).

Step 3: Controlling Light

If the sensor is ON, the light pin is set HIGH to turn the light ON; otherwise, it is set LOW to turn it OFF.

🔄

Alternative Approaches

Using Interrupts
embedded_c
#include <avr/io.h>
#include <avr/interrupt.h>

ISR(PCINT1_vect) {
    if (PINC & (1 << PC0)) {
        PORTB |= (1 << PB0);
    } else {
        PORTB &= ~(1 << PB0);
    }
}

int main(void) {
    DDRB |= (1 << PB0);
    DDRC &= ~(1 << PC0);
    PCICR |= (1 << PCIE1);
    PCMSK1 |= (1 << PCINT8);
    sei();
    while(1) {}
    return 0;
}
Uses interrupts to react instantly to sensor changes, saving CPU cycles but more complex.
Using Timer to Poll
embedded_c
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB |= (1 << PB0);
    DDRC &= ~(1 << PC0);
    while(1) {
        if (PINC & (1 << PC0)) {
            PORTB |= (1 << PB0);
        } else {
            PORTB &= ~(1 << PB0);
        }
        _delay_ms(500);
    }
    return 0;
}
Polls sensor every 500ms, simpler but less responsive.

Complexity: O(1) time, O(1) space

Time Complexity

The program runs in a continuous loop checking sensor input and updating output without loops dependent on input size, so time complexity is constant O(1).

Space Complexity

Uses fixed memory for registers and variables, no dynamic memory allocation, so space complexity is O(1).

Which Approach is Fastest?

Interrupt-driven approach is fastest in response time but more complex; polling is simpler but slower to react.

ApproachTimeSpaceBest For
Polling LoopO(1)O(1)Simple projects, easy to implement
InterruptsO(1)O(1)Fast response, efficient CPU usage
Timer PollingO(1)O(1)Balanced simplicity and responsiveness
💡
Always configure your microcontroller pins correctly as input or output before using them.
⚠️
Beginners often forget to set the data direction registers, causing pins not to work as expected.