#6 — The ESP32 Automatic Feeder; A Design

Carissa Aurelia
I learn ESP32 (and you should too).
7 min readMar 7, 2020

Worry no more!

Before we begin, here’s a picture of a cat to brighten up your day :)

A cat. Photo by Manja Vitolic on Unsplash

Ok, moving on to the problem.

The problem

Everybody loves cat (or so I guess). We have tons of cats here in the campus — I think they’re wild cats wandering around and gets comfy in the territory and becomes some sort of a “pet”. I’ve seen some people carrying cat food for them and feed them every morning. The other day, I joked around with some friends if in the future the campus would have some sort of a cat breeding area — a place dedicated to keeping and caring all of the cats where people can study them (Biology major, I’m talking about you guys), or just see them, or even, most importantly, play with them (hehe 😁). The overall campus environment would be much nicer when the cats are kept in one place instead of being let wandering around and messing with the trash bins.

Ok, the *real* problem

…is that the joke gets serious. If we’re to keep the cats in a place, there are some things that we must take into account. One thing among all is how and when to feed them. The cats usually scour the trash bins for food leftovers (or relying on that one good samaritan guy who feeds them every morning with Whiskas). And now that we’re going to keep them, we’ll have the chance to feed them properly. Having someone to deliver them food every morning and afternoon will be a tedious task to do. Here comes our hero:

The automatic feeder!

In a nutshell, an automatic feeder, as its name suggests, is a device that will automatically provide food without having to rely on direct human control. In that way, we can always be sure that the cats are always fed on time without having someone around to physically do the checking. There are tons of automatic feeder out there, which is not only pricey but also lacking some essential features. What this automatic feeder can do (and hence makes it different) is as follows:

  • Dispenses cat food every morning and afternoon.
  • Sends a notification to the cat’s caretaker email, telling that the food has been dispensed at a certain date and time stamp.

In order to do that, the device must be able to measure the intensity of sunlight in order to determine if it’s day or afternoon. The data will then be used by the microcontroller to move the motor in a certain way that dispenses the cat food. And when the cat food is dispensed, the microcontroller will send an email to the caretaker. The caretaker will be able to see on their phone an email containing date and time stamps of when the food is dispensed and thus, letting them now that the cat has been fed.

The specs

I’ll be using the most familiar microcontroller that I’ve used in my previous projects: the ESP32. It has all the specification that I need to be able to control a luminosity sensor, a motor, and send email notifications. The measurement unit that I’ll use is the SI unit of light intensity called Lux (lx). The luminosity sensor and the motor that I’ll be using as an example in this project is the Adafruit TSL2561 Luminosity Sensor and the DC motor with L298N motor driver. The sensor will communicate with the ESP32 using the I2C communication and the ESP32 will use the digital to analog peripherals to rotate the motor. Finally, the ESP32 will get date and time stamp using the NTP server, then using the PHP script to send email notifications to the caretaker’s phone, thus implying that the notification will be displayed on the phone/PC screen.

Disclaimer: there will be no coding in this post. But I will show you the overall design of the device and flow chart on how it will be implemented.

Scenario

I’ll take one cat as an example of the application scenario. As I’ve mentioned before, the cat will be fed twice a day: at day and afternoon. I’ll be using this outdoor light levels table as a reference.

Source: https://www.engineeringtoolbox.com/light-level-rooms-d_708.html

1. Starting up

For first time use, the device will get the current date, store it in a variable, and set the boolean variable hasRotateDay = False and hasRotateNight = False .

2. Daytime

Daytime scenario.

According to the outdoor light levels table, the intensity of light at daytime will be greater than 100 lx. When the light sensor detects that the intensity of light is greater than 100 lx and the value of hasRotateDay = False, it will call a function to rotate the motor forward in a period of time (say 2 seconds) to dispense the cat food. The ESP32 will get a date and time stamp and set the hasRotateDay = True. The ESP32 would then send an email to the caretaker including the said date and time stamp and an info that the cat has been fed. After that, the sensor will continue to collect light intensity reading. When the value is still higher than 100 lx and the value of hasRotateDay = True, the motor will not rotate.

Sample of daytime report.

3. Afternoon

Afternoon scenario.

When the light sensor detects the value is less than 100 lx and hasRotateNight = False, it will trigger a function to rotate the motor forward in the same period of time to dispense the cat food. The ESP32 will get a date and time stamp and set the hasRotateNight = True. The ESP32 would then send an email to the caretaker including the said date and time stamp and info that the cat has been fed. Same as daytime scenario, the sensor will continue to collect light intensity reading. When the value is still lower than 100 lx and the value of hasRotateNight = True, the motor will not rotate.

Sample of afternoon report.

4. Reset

At the end of the loop, a reset function will be called. It will get the current date and compares it to the date stored in the variable (collected when the device first start). When the current date is greater than the variable date (meaning it’s tomorrow already), the current date will be stored in the variable and the boolean variable hasRotateDay and hasRotateNight will be set to False. But when the current date is the same as the variable date (meaning the day hasn’t ended), nothing will be done.

Hardware design

Hardware design.

The image above roughly pictured how the hardware would be connected. The DC motor will be attached to the food dispenser and connected to the L298N Motor Driver. Both the motor driver and the TSL2561 Lux Sensor will be connected to ESP32 GPIO pins. The ESP32 will be connected to the WiFi to be able to send email notification via the internet.

Software Design

Flow chart depicting the program flow.

The flowchart above explains the program logical flow. As mentioned, it will start by getting the current date and store it in a variable. Then it will set the hasRotateDay = False and hasRotateNight = False . Next it will call getLuxValue() which will get the current lux value. There are three kinds of if conditions that will happen. Both the if(lux > 100 & hasRotateDay = False) and the else if (lux < 100 & hasRotateNight = False) conditions will call a function rotateMotor() , set the hasRotateDay and hasRotateNight to be True , and call sendEmail(). The last condition will do nothing. The three of them will then call reset() function which will have an either True or False value. Depending on that value, the program will execute the next step. All happens in a neat loop.

Epilogue

At the end of the day, this is just a project design full of what-ifs and whatnots. It is far from perfect and there’s a lot of inputs needed. But with the right tools, it can possibly be implemented, even for your own pets at home. What do you think? Leave a comment below :)

--

--