Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP8266 and INA219-Based Current Spike Detector with Buzzer and Door Reset

View and Edit Project In Cirkit DesignerImage of ESP8266 and INA219-Based Current Spike Detector with Buzzer and Door Reset

Circuit Documentation

Summary

This circuit is designed to interface with an INA219 current sensor and a NodeMCU V3 ESP8266 microcontroller. The circuit measures current spikes above 500mA. If 10 such spikes are detected, a buzzer is triggered. The system resets when a door switch is opened.

Component List

  1. NodeMCU V3 ESP8266

    • Description: A microcontroller based on the ESP8266 Wi-Fi module.
    • Pins: A0, GND, VU, S3, S2, S1, SC, S0, SK, 3V3, EN, RST, Vin, D0, D1, D2, D3, D4, D5, D6, D7, D8, RX, TX
  2. INA219

    • Description: A current sensor that measures the current, voltage, and power.
    • Pins: VCC, GND, SCL, SDA, VIN -, VIN +
  3. DC Power Source

    • Description: Provides the necessary power for the circuit.
    • Pins: Ground, Positive
  4. Passive Buzzer

    • Description: A buzzer that produces sound when activated.
    • Pins: S output signal, +VCC, GND
  5. Door MC-38

    • Description: A magnetic door switch used to detect the opening of a door.
    • Pins: S, GND

Wiring Details

NodeMCU V3 ESP8266

INA219

DC Power Source

Passive Buzzer

Door MC-38

Code Documentation

/*
 * This Arduino Sketch is designed to interface with an INA219 current sensor
 * and a NodeMCU V3 ESP8266 microcontroller. The circuit measures current
 * spikes above 500mA. If 10 such spikes are detected, a buzzer is triggered.
 * The system resets when a door switch is opened.
 */

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

const int buzzerPin = D5; // Pin connected to the buzzer
const int doorSwitchPin = D6; // Pin connected to the door switch

int spikeCount = 0;
const int maxSpikes = 10;
const float currentThreshold = 0.5; // 500mA

const int debounceDelay = 50; // 50 milliseconds debounce delay
unsigned long lastDebounceTime = 0;
int lastDoorSwitchState = HIGH;
bool spikeDetected = false;

unsigned long buzzerOnTime = 0;
const unsigned long buzzerDuration = 500; // 500 milliseconds
bool buzzerState = false;

void setup() {
  Serial.begin(115200);
  Wire.begin(); // Remove explicit pin arguments
  ina219.begin();
  pinMode(buzzerPin, OUTPUT);
  pinMode(doorSwitchPin, INPUT_PULLUP);
  digitalWrite(buzzerPin, LOW);
}

void loop() {
  float current_mA = ina219.getCurrent_mA();
  Serial.print("Current: ");
  Serial.print(current_mA);
  Serial.println(" mA");

  if (current_mA > currentThreshold * 1000 && !spikeDetected) {
    spikeCount++;
    spikeDetected = true;
    Serial.print("Spike count: ");
    Serial.println(spikeCount);
  } else if (current_mA <= currentThreshold * 1000) {
    spikeDetected = false;
  }

  if (spikeCount >= maxSpikes) {
    if (!buzzerState) {
      buzzerState = true;
      buzzerOnTime = millis();
      digitalWrite(buzzerPin, HIGH); // Turn on buzzer
    } else if (millis() - buzzerOnTime >= buzzerDuration) {
      buzzerState = false;
      digitalWrite(buzzerPin, LOW); // Turn off buzzer
    }
  }

  int doorSwitchState = digitalRead(doorSwitchPin);
  if (doorSwitchState != lastDoorSwitchState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (doorSwitchState == LOW) {
      spikeCount = 0;
      digitalWrite(buzzerPin, LOW); // Reset buzzer
      Serial.println("System reset.");
    }
  }

  lastDoorSwitchState = doorSwitchState;

  // Use non-blocking delay
  static unsigned long lastLoopTime = 0;
  if (millis() - lastLoopTime >= 1000) {
    lastLoopTime = millis();
  }
}

This code interfaces with the INA219 current sensor and the NodeMCU V3 ESP8266 microcontroller. It measures current spikes above 500mA and triggers a buzzer if 10 such spikes are detected. The system resets when a door switch is opened.