Projects / Smart air freshener

Smart air freshener

Beginner1 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. NANO59°motion

    Wire the servo and PIR

    Add the capacitor across the servo supply first.

  2. NANO59°

    Test the press

    Run the servo alone and adjust the 40 and 90 degree angles to press and release your nozzle.

  3. NANOmotion

    Add the sensor

    Wire the PIR and check it with a Serial print.

  4. NANO

    Shorten the times

    Set MIN_PRESENT and LEAVE_WAIT to a few seconds to test.

  5. NANO

    Restore real times

    Put back one minute, two minutes and one hour.

  6. NANO59°

    Mount

    Fix the freshener and servo in a bracket facing away from the seat.

In short

How do you build a smart air freshener?

A servo presses a freshener spray only after someone has used the room and left, and no more than once an hour. You need Arduino Nano, PIR Motion Sensor HC-SR501, SG90 Micro Servo, DS3231 RTC module and Electrolytic capacitor 1000 µF. It takes 6 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
5 V USB adapter +Nano 5V pin, servo red and PIR VCCPower
5 V USB adapter -Nano GND, servo brown and PIR GNDCommon ground
1000 µF capacitorAcross the servo power wires, + to 5 VAbsorbs servo surges
PIR OUTNano D2Presence
Servo signalNano D9Presses the spray
DS3231 SDA, SCLNano A4, A5I2C
DS3231 VCC, GNDNano 5V, GNDPower

Code

Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "RTClib"; Servo and EEPROM are built in. Set the DS3231 once with the RTClib example. Run the servo from a 5 V adapter, not the Nano's USB pin, because it can pull more than a USB port gives. Use a pump or trigger-style freshener meant to be pressed by hand, mount it so the nozzle points away from people, and keep it away from heat, flames and children. Change the servo angles to suit your nozzle.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Servo.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>

Servo servo;
RTC_DS3231 rtc;
const byte PIR = 2;
const unsigned long MIN_PRESENT = 60000UL;      // someone must be there at least a minute
const unsigned long LEAVE_WAIT = 120000UL;      // then gone for two minutes
const unsigned long GAP = 3600000UL;            // at most one spray an hour
unsigned long presentSince = 0, lastSeen = 0, lastSpray = 0;
bool wasPresent = false;
int spraysLeft;

void spray() {
  servo.write(40);  delay(700);                  // press the nozzle
  servo.write(90);  delay(400);                  // release
  spraysLeft--;
  EEPROM.put(0, spraysLeft);
  Serial.print("Sprays left: "); Serial.println(spraysLeft);
  lastSpray = millis();
}

void setup() {
  Serial.begin(9600);
  pinMode(PIR, INPUT);
  servo.attach(9); servo.write(90);
  rtc.begin();
  EEPROM.get(0, spraysLeft);
  if (spraysLeft < 0 || spraysLeft > 500) { spraysLeft = 300; EEPROM.put(0, spraysLeft); }   // new can: about 300 sprays
}

void loop() {
  unsigned long now = millis();
  if (digitalRead(PIR)) {
    if (!wasPresent) { presentSince = now; wasPresent = true; }
    lastSeen = now;
  }
  bool leftRoom = wasPresent && now - lastSeen > LEAVE_WAIT;
  if (leftRoom) {
    bool longEnough = lastSeen - presentSince >= MIN_PRESENT;
    int hour = rtc.now().hour();
    bool daytime = hour >= 6 && hour < 23;       // quiet at night
    if (longEnough && daytime && spraysLeft > 0 && (lastSpray == 0 || now - lastSpray > GAP)) spray();
    wasPresent = false;
  }
  delay(200);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Walk in and out brieflyNo spray
Stay over a minute, then leave for twoOne spray
Visit again within an hourNo second spray
Between 11 pm and 6 amNo spray

If it does not work

Servo twitches

Add the capacitor and use a proper 5 V supply.

Never sprays

Print the timers. The PIR may need warm-up time or the room sensor view is blocked.

Presses too weakly

Change the servo angle and mount it closer.

Sprays count is wrong

Reset the EEPROM value when you fit a new can.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the smart air freshener?

Arduino Nano, PIR Motion Sensor HC-SR501, SG90 Micro Servo, DS3231 RTC module and Electrolytic capacitor 1000 µF.

How long does the smart air freshener take to build?

About 60 minutes for a beginner maker, in 6 steps.

How do I know the smart air freshener works?

Walk in and out briefly: No spray. Stay over a minute, then leave for two: One spray. Visit again within an hour: No second spray.

Servo twitches: what should I check?

Add the capacitor and use a proper 5 V supply.

Never sprays: what should I check?

Print the timers. The PIR may need warm-up time or the room sensor view is blocked.

Presses too weakly: what should I check?

Change the servo angle and mount it closer.

Sprays count is wrong: what should I check?

Reset the EEPROM value when you fit a new can.