Saving Electricity by Automating What Uses It

DZone 's Guide to

Saving Electricity by Automating What Uses It

See a quick example that can be expanded on to use a motion sensor and a quick bit of code that turns on and off electrical devices based on movement.

· IoT Zone ·
Free Resource

Our daily activity relies on electricity to power lights, appliances, and the rest of our electronic devices. More electricity usage means more expense. Smart strategies can help us save electricity. It is not only saving a lot of money, but also helping stop global warming.

One of the strategies is to use electricity only as necessary. Restroom lights, alley/staircase lights, or meeting room air conditioners are some examples that usually unnecessarily turned on. Mostly, people do not care about that. They leave the room without turning off the lights or air conditioner.

The Solution Concept

In order to save electricity, we can automate the usage with a simple logic: turn on the lights only if there is a person there. If anyone leaves the room, then turn off the lights automatically. To avoid an incorrect decision, set a certain range of time tolerance. Hold a few minutes before turning off the light, in case the person who leaves the room comes back after just a moment.

Image title

This simple logic can be implemented with a cheap PIR Motion Sensor. That's a passive infrared sensor that detects and sends a signal when it captures any movement. The signal will be received by NodeMCU/Arduino for the next step. If no movement is detected, then NodeMCU will count the timer. Once the time tolerance is exceeded, then the system will send a signal to turn off the electrical connections.

Another important component is a Relay module. That's an electromagnetic switch that can be controlled by sending an electronic signal. This module is responsible for opening or closing the electrical connection based on the signal from the system. This module will act as a mediator between the high voltage devices and the microcontroller.

The Proof of Concept

To proof this concept, we can develop a simple project simulation to turn on/off an LED light. Basically, we can use an AC lamp with a direct electricity connection. But with safety in mind, we use a DC connection. The main purpose of this practice is turning on/off a device by using a Relay module.

Image title

The project can be started with this code snippet:

void loop() {
  statePir = digitalRead(pinPir);

  if (statePir == HIGH) {
    digitalWrite(pinRelay, LOW);
    timer = 0;

    Serial.println("motion detected - turn on");
  } else if (timer > 10000) {
    digitalWrite(pinRelay, HIGH);

    Serial.println("turn off");

  Serial.print("timer: ");

  timer += 100;


The PIR Motion Sensor is responsible for capturing any movement and sending a signal to turn on the light. The Relay module receives the signal and closes the electrical connection. It turns the light on. Once the light is turned on, the timer will be reset to zero.

As long as no motion is detected, the system will count the timer. Image title

Later, when motion is detected, the system then sends a signal to turn on the device and reset the timer.

Image title

If no motion is detected within a certain range of time, the system then sends a signal to turn off the device.

Image title

That's that! With some hardware and a bit of code, you've bene able to create a connected motion sensor to save electricity.

arduino ,iot ,microcontroller

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}