Gentiam Electronics
Published © CC BY

IoT Automatic Dog Feeder

We are going to build a simple IoT dog feeder that costs less than $30 and can be controlled remotely via an IoT software!

BeginnerFull instructions provided2 hours1,410
IoT Automatic Dog Feeder

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Adafruit PIR Motion Sensor
×1
DFRobot SER0011 Servo Motor
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1
LED (generic)
LED (generic)
×1
Resistor 100 ohm
Resistor 100 ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Blynk
Blynk
The free plan will work for this project.

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
This is for the WeMos D1 Mini board.

Story

Read more

Schematics

Final Circuit Diagram

Code

FInal Project Code

Arduino
/*************************************************************************************
    FILENAME: automatic_dog_feeder.ino
    
    DESCRIPTION:
    This is a script for a automatic dog feeder project using a WeMos D1 Mini, PIR 
    sensor, servo motor, and the Blynk IoT Platform. You can find the full 
    instructions to this project here: 

    DATE: May 5, 2022

    LEARNING HUB:
    For more accessible education materials, please check out the Gentiam Electronics
    Learning Hub at:    
    https://www.gentiam.com/learning-hub/

    DISCLAIMERS:
    YOU AGREE TO USE THIS CODE, OR ANY OTHER OBTAINED FROM ANY GENTIAM ELECTRONICS 
    SITE OR SOCIAL MEDIA LOCATION, AT YOUR OWN DISCRETION AND RISK. YOU WILL BE 
    SOLELY RESPONSIBLE FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR LOSS OF DATA THAT 
    RESULTS FROM THE DOWNLOAD OF ANY SUCH MATERIALS.  

    THE COMPLETE TERMS OF USE FOR ANY MATERIALS OBTAINED FROM ANY GENTIAM ELECTRONICS
    SITE, INCLUDING THIS CODE, CAN BE FOUND AT 
    https://www.gentiam.com/legal/

**************************************************************************************/

#define BLYNK_TEMPLATE_ID "Replace with your template ID"
#define BLYNK_DEVICE_NAME "Quickstart Template"

#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
#include <SimpleTimer.h>

// Define pins for the PIR Motion Sensor
int PIR_pin = D7; 
int PIR_status = 0;

// Servo Motor
Servo ServoMotor; 
int ServoPin = D4;

const int ServoInitialPosition = 0; // Starting position of servo motor
const int ServoFinalPosition = 135; // Final position of servo motor

// LED for PIR motion sensor
int ledPin = D2;

SimpleTimer timer;
int counter = 0;
int flag = 1; // Set a flag to make sure the program only sends one notification if motion is detected continuously

// ESP8266
char auth[] = "Your Blynk Auth Token"; //Blynk token
char ssid[] = "Replace with your WiFi SSID"; //Your WiFI SSID
char pass[] = "Replace with your WiFi password"; //Your Wifi password

// Create a button to control the servo motor
// This is a standalone function and can't be used inside of any loop or function.
// Go to Blynk documentation for more details: https://docs.blynk.io/en/blynk.edgent-firmware-api/virtual-pins
BLYNK_WRITE(V1) 
{
  int pinValue = param.asInt(); // Assign incoming value from pin V1 to the variable pinValue
  
  if (pinValue == 1)
  {
    ServoMotor.attach(ServoPin); // Enable control signal of the servo motor
    ServoMotor.write(ServoFinalPosition); // Rotate the shaft to the final position
    delay(1000);
    ServoMotor.write(ServoInitialPosition); // Rotate the shaft to the initial position
    delay(1000);
    ServoMotor.detach(); // Disable control signal 
    Blynk.logEvent("food_dispensed", "Food Dispensed!"); // Send a notification to the user
  } 
  //Serial.print("V1 button value is: ");
  //Serial.println(pinValue);
}

void PIR_sensor(){
  counter++;
  Serial.println(counter);

  // Turn off LED if no motion has been detected for 30 seconds
  if (counter == 10) 
  {
    digitalWrite(ledPin, LOW);
    counter = 0;
    flag = 1;
  }
  
  PIR_status = digitalRead(PIR_pin);
  //Serial.println(PIR_status);

  if (PIR_status == HIGH) 
  {
      if (flag == 1) 
      {
        Serial.println("Motion Detected!");
        Blynk.logEvent("motion_detected", "Motion Detected!");
        // If motion is detected, turn on LED 
        digitalWrite(ledPin, HIGH);
        counter = 0; 
        flag = 0;
      }
  } 
  else 
  {
      Serial.println("Motion Stopped!");
  }
}

void setup() {
  Serial.begin(9600);
  Serial.println("Start!");
    
  Serial.print("Connecting...");
  
  Blynk.begin(auth, ssid, pass); 

  pinMode(PIR_pin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  timer.setInterval(3000L,PIR_sensor); // Detects motion every 3 seconds
}

void loop() {
  Blynk.run();
  timer.run();
}
///////////////////////////////////////////////////////////////////
//   Copyright 2022 Gentiam Electronics - All Rights Reserved   //
///////////////////////////////////////////////////////////////////

Credits

Gentiam Electronics

Gentiam Electronics

0 projects • 0 followers

Comments