Using GPIO Pins With ESPHome & Home Assistant

Using GPIO Pins With ESPHome
In this post, we learn how to interface GPIO pins with ESPHome and home assistant. We start by looking at different input configurations, we then learn about debouncing and finally, we learn about different output configurations.

Step 1: Watch the Video

The video above covers everything you need to get started with using GPIO pins. We look at different configurations and carry out different tests which is best explained through a video.

Step 2: Wire Up & Test

WiringWiring
Use the reference diagram above to wire up the circuit if you plan on testing it.

Automating an RGB Lamp With Your Phone by Using Hassio

Automating RGB LEDs

Step 1: Watch the Video

The video above gives you all the information you need to build this yourself. This post will only provide the final configuration file that can be used as a starting point.

Step 2: Wire the LEDs

WiringWiring
You can use the reference wiring diagram above to connect the LEDs. Please note that you need to use the RX pin, which is also the GPIO3 pin. This is needed as the DMA data transfer defaults to GPIO3.

Step 3: Configuration File

Control PanelControl Panel

You can use the following link to obtain the configuration lines that need to be added to your ESPHome node:

ESPHome – Home Automation Simplified

ESPHome
I was pleasantly surprised when I decided to try to add some DIY sensors to home assistant. Using ESPHome is extremely simple and in this post, we will learn how to control a GPIO pin and also obtain temperature & humidity data from a wireless node, by using Home Assistant. The best part is that it does not require any code to be written as it does this automatically.

Step 1: Watch the Video

It is much easier to see everything being done through a video first and that is why I’d recommend watching the video shown above to get an idea of how everything works. This post will only contain the important steps needed to do this yourself.

Step 2: Install ESPHome

First, we need to install the ESPHome add-on in Home Assistant. Navigate to the add-on store by doing the following:

  • Hass.io -> Add-On Store

Add the following URL to the store so that it can find the ESPHome Add-On:

https://github.com/esphome/hassio

Once done, simply search for ESPHome, click the add-on that shows up and hit the install button. Installation may take some time so please give it a few minutes as this will depend on your internet connection. Once installed, simply hit the START button and wait a few seconds for it to start. Then, click the button that says “OPEN WEB UI” which will take you into the ESPHome screen.

Step 3: Configure the Board (Node)

In ESPHome, devices are called nodes, and we first need to create one so that it can then begin communicating with Home Assistant.

Click the add button to create a new node. Then give it a name by using the characters shown on screen. I will call it “living_room”. Then, select the device type which is “WeMos D1 Mini” for this demo. Finally, add your WiFi network details so that the board can connect to your WiFi network and then click “SUBMIT” to create the node.

ESPHome needs to be restarted after creating a new node. This can be done by navigating to hass.io -> ESPHome and then clicking the RESTART button. Give it a few seconds and then open up the WEB UI.

We now need to configure this node so that we can control the LED that is connected to pin D2. The ESPHome website contains all the information you need to configure different components and since we will be controlling a GPIO pin, we can use the example configuration entity information from the following page:

https://esphome.io/components/switch/gpio.html

Node ConfigurationNode Configuration

Click the EDIT button for the node and then paste the configuration information and update it as shown in the image and then, save the file. Since this is the first time we are using the board with ESPHome, we need to manually upload the code to the board. Once this is done, we can then automatically upload any updates wirelessly using the OTA update feature.

To manually download the code, we first need to compile it. So use the node options menu to compile the code for the node. This will take a while and it took about 100 seconds for me. Once done, simply download the binary file. Then, visit the following link and download the ESPHome flasher tool for your system:

https://github.com/esphome/esphome-flasher/releases

Double click the file to open it. Then plug in your board, select the correct COM port, select the binary file we downloaded and then hit the FLASH button. This will download the code to your board so give it a few seconds to complete. Once done, the board will automatically connect to your WiFi network and you will be able to see this in the logs.

Step 4: Add the Node to Home Assistant

Home Assistant will automatically detect the board and give you a notification for it. You can either click that to get started or you can navigate to the following:

  • Configuration -> Integrations

Card ConfigurationCard Configuration

DashboardDashboard

You will then be able to see the node, so click configure and confirm the addition. Now, all we need to do is add it to the dashboard. So head over to the dashboard/overview section and then select the “Configure UI” option from the options menu in the top right section. Then, click the Add button, select Entities and then give it a new name. You can then use the entity list to select the switch which will have the same name as the node. Click save, close the dashboard configuration view and that’s all you need to do. If you toggle the switch then the LED on the board should toggle as well and the state will be displayed on the dashboard.

Step 5: Interface the DHT11 Sensor

Node WiringNode Wiring

Node BreadboardNode Breadboard

I will be using the DHT11 sensor to obtain temperature and humidity values. Connect it to the board by using the reference wiring diagram. You first need to copy the example configuration from the ESPHome website using the following link:

https://esphome.io/components/sensor/dht.html

Node ConfigurationNode Configuration

Node LogsNode Logs

Then, head over to ESPHome and edit the configuration for the node by adding the configuration. Be sure to update the pin and also add the model name as seen in the image. Once done, simply hit the upload button and wait for it to complete everything. It will automatically create, compile the code and also upload it to the board wirelessly, using the OTA feature. Once complete, you will see the output logs and the board will automatically connect to your network.

Step 6: View the Temperature & Humidity Values

Adding Sensor ValuesAdding Sensor Values

Sensor Values On DashboardSensor Values On Dashboard

The next thing we need to do is add the sensor values to the dashboard. This step is similar to the previous section. Select the Configure UI option to create a new card, then give it a name and add the entities as shown in the image. Click save, exit the configuration screen and you will then be able to view the sensor information on screen.

That’s how easy it is to get started with home automation using ESPHome. We will continue interfacing different modules to home assistant so please do consider subscribing to our YouTube channel as it helps us create projects like this.

Thank you for reading!

Adding AutoConnect To The Network Time Project

Online Weather Widget Using ESP8266
In a recent post, we learned about the AutoConnect feature for the ESP32/ESP8266 boards and one of the questions asked was about adding it to existing sketches. In this post, we will learn how to do just that and we will use the network time project as an example.

Since there is a lot of code that needs to be copied over, I would recommend watching the video to learn more as it’s much more efficient to watch it in action than read about it.

Step 1: Download the Sketches

You will need to download and obtain two sketches as we will be using them for this project. Start by download the network time project sketch (E12) from the following link: https://www.bitsnblobs.com/wp-content/media/fw/diy/diy-e12.zip

Extract the file and rename it to E16 as that will be the final sketch for this project.
Next, download the AutoConnect sketch (E13) using the following link: https://www.bitsnblobs.com/wp-content/media/fw/diy/diy-e13.zip

Extract this file too and open both sketches in the Arduino IDE.

Step 2: Update the Sketch

Now, we need to copy over some code from the AutoConnect sketch to the new sketch (E16). Please watch the video to follow through the steps or else you can download the final sketch using the link at the bottom of this post.

Step 3: Upload and Test

Upload the sketch to the board using the settings mentioned in the sketch. If the network credentials were previously stored in the flash, then the board will automatically connect to the WiFi network. If not, then you will need to connect to the access point and configure the network, just like we did in the AutoConnect video. Everything else is the same so please refer to the original post to learn how to use the AutoConnect library, if needed.

Link to original post: https://www.instructables.com/id/Using-WiFi-AutoConnect-With-ESP8266ESP32-Boards/

Online Weather Widget Using ESP8266

Online Weather Widget Using ESP8266

A couple of weeks ago, we learned how to build an online weather display system that obtained weather information for a particular city and displayed it on an OLED module. We used the Arduino Nano 33 IoT board for that project which is a new board that has a lot of features but is slightly expensive and larger when compared to alternatives. In this post, we will learn how to build this compact and cute little online weather display widget which is similar to the previous project.

The video above covers everything you need to know and also explains how the sketch is put together.

Step 1: Gather the Electronics

We Need The WeMos D1 Mini And A 0.96We Need The WeMos D1 Mini And A 0.96″ OLED Module

We will be using the WeMos D1 mini board that makes use of the ESP8266 chipset and we will use a 0.96” OLED module for the display.

Step 2: Download, Update and Upload the Sketch

Sketch ModulesSketch Modules

OpenWeatherMap ServiceOpenWeatherMap Service

Like the previous project, we will be obtaining the weather information from the OpenWeatherMap service. Download the sketch using the link at the bottom of this post.

We first need to obtain an API key in order to use the OpenWeatherMap service. The OpenWeatherMap service has a free plan which allows for a maximum of 60 API calls per minute. This means that we can request weather information at a maximum rate of once every second which is more than what we need. The video goes over the entire process, but here’s a text version of what needs to be done:

Start by signing up or logging into the OpenWeatherMap service and head over to the API section. Enter the name for a new key and click the generate button. Make a note of this key but do not share it with anyone. It takes a bit of time for this key to get activated so you will have to wait a while. It could take up to an hour for this to complete, but my key was activated within 10 minutes.

Add Your Network CredentialsAdd Your Network Credentials

Add Your API Key And City NameAdd Your API Key And City Name

Start updating the sketch by adding your WiFi credentials as we need to connect to a network for this to work. The other thing you need to do in the sketch is to update the API call with your city information and API key. Make sure you have the correct board support package installed for the ESP8266 board. You will also need to install the ArduinoJSON and u8g2 libraries for the sketch to work. The video takes you through the process of installing all of this.

Select The Right Board SettingsSelect The Right Board Settings

Once completed, connect the board, ensure that you have selected the correct board settings and hit the upload button. Once uploaded, the board will print the status along with the weather information to the serial terminal. You can view this to make sure everything is working as expected, before moving to the next step.

Step 3: Connect the OLED Module and Test

ConnectionsConnections

Now that we have the sketch working correctly, we need to wire up the OLED module. Use the wiring diagram shown above and make sure you connect the correct power supply voltage to the OLED module as some only accept 3.3V.

Wire Up And TestWire Up And Test

Once connected, power on the board and you should see the welcome message. Give the board a few seconds to obtain the weather information and it will print it to the OLED module.

Step 4: Use an Enclosure

All that’s left to do now is to add a suitable enclosure and I will be using this model from Thingiverse (https://www.thingiverse.com/thing:857858). This is the same one that was used for the network time project and I quite like the look of it.

Add Wires And Double Sided Tape To The OLED ModuleAdd Wires And Double Sided Tape To The OLED Module

Place The Board Onto The CoverPlace The Board Onto The Cover

Wire Up The OLED Module To The BoardWire Up The OLED Module To The Board

Add Glue To Seal The EnclosureAdd Glue To Seal The Enclosure

Add wire of suitable length to the OLED module along with some double-sided tape. Side it in place and use some hot glue near the wires to hold it in position. Then, place the board onto the tray/back cover and solder the OLED module to it like before. Finally, push the cover in place and add some glue to hold it in position.

If you liked this build then please consider subscribing to our YouTube channel and following us on social media. It doesn’t cost a thing but your support will help us immensely in continuing to create projects like this.

RTSP Video Streamer – ESP32

RTSP Video Streamer - ESP32
This post will show you how you can build a $9 video streaming device that uses RTSP and the ESP32-CAM board. The sketch can be configured to connect to an existing WiFi network or it can also create its own access point that you can connect to, in order to view the stream.

The video above talks you through the entire process of building this project.

Step 1: Download, Prepare & Upload the Sketch

Changes To GitHub VersionChanges To GitHub Version

Connections For Sketch UploadConnections For Sketch Upload

Specify WiFi Credentials If NOT In AP ModeSpecify WiFi Credentials If NOT In AP Mode

Stream LinkStream Link

Setting The Board Into AP ModeSetting The Board Into AP Mode

Change The Password (optional)Change The Password (optional)

Stream Link When In AP ModeStream Link When In AP Mode

Download the sketch using the link at the end of this post. There are two ways for you to use the sketch:

Connecting To An Existing WiFi Network:

If you want the video streamer to connect to an existing WiFi network then you need to add the network credentials to the wifikeys.h file as shown in the image. You do not have to change anything else in the sketch and you can proceed to the downloading the sketch section below:

Creating A New Access Point:

You can also configure the board to create a standalone access point that you can connect to and view the stream from. To do this, you need to uncomment the “#define SOFTAP_MODE” line in the sketch. Optionally, you can also change the access point password if you want to. Use the image as a reference.

Downloading The Sketch:

The ESP32-CAM board does not have an onboard USB connector so you need to use an external USB to serial converter to upload the sketch. You can use the wiring connections shown above but make sure that the USB to serial converter is connected in the 3.3V mode.

It is recommended to use an external 5V supply to power the board, particularly if you are using an FTDI breakout board. For the external 5V supply, a simple USB breakout board will do just fine. There has been some success in powering the board directly from the CP2102 breakout board so you can try that first. The board also has a 3.3V power pin if needed.

The jumper is needed to put the board in the download mode. Once you have everything connected, power up the board, open a serial terminal (Tools->Serial Monitor) with a baud rate of 115,200 and press the reset button. You should obtain an output as shown in the image and this will indicate that everything is working as expected.

Next, hit the upload button and wait for it to complete the upload. The board will print the stream link to the serial terminal in either case and we need to use this to view the stream.

Step 2: View the Stream

Use The Open Network OptionUse The Open Network Option

Add The Stream Link And Click OpenAdd The Stream Link And Click Open

View The StreamView The Stream

Connect To Network (If In AP Mode)Connect To Network (If In AP Mode)

Enter The PasswordEnter The Password

Type In IP Address To View StreamType In IP Address To View Stream

Access Point Mode:

If you configured the board to create a new access point then you will have to connect to it first before you can view the stream. The default access point name is “devcam” and the default password is “12345678”. Once connected, you can proceed to view the stream below.

Using A Computer:

The easiest way to view the stream on the computer is by using VLC. So download and install it first. Next, select the “Open Network” option from the File menu and then paste or type the stream link (example: rtsp://192.168.1.31:8554/mjpeg/1) into the box that shows up. Hit open and wait for the stream to show up.

Using A Smartphone:

You can download an RTSP client app to view the stream using your smartphone. But typing in the IP address into a web browser will give you a stream that consists of the images.

Step 3: Use an Enclosure for the Camera

Enclosure For This BuildEnclosure For This Build

Wire Up A microUSB BoardWire Up A microUSB Board

Add Some Kapton Or Insulation TapeAdd Some Kapton Or Insulation Tape

Glue The microUSB Board In PlaceGlue The microUSB Board In Place

Add The Back Cover And StandAdd The Back Cover And Stand

I decided to use the following model from Thingiverse for the enclosure: https://www.thingiverse.com/thing:3652452

Start by soldering wires to a microUSB breakout board and connecting the output to the 5V power pins of the ESP32-CAM board. Then, add some kapton or insulation tape to the microUSB board to avoid it shorting out the ESP32-CAM board. Insert a microUSB cable into the board to ensure that it is mounted correctly and use some hot glue to hold the microUSB board in position. Do not apply too much glue towards the edge of the enclosure as the back cover has to sit in place. Finally, add the back cover and you are good to go!

Add WiFi AutoConnect To ESP32/ESP8266 Boards

WiFi AutoConnect For ESP32/ESP8266
We will learn how to use the AutoConnect library that allows us to connect to and manage WiFi access points using a smartphone.

The video above will guide you through the process along with the various screens that you need to access to learn about the AutoConnect library. This written post will only cover it in brief.

Step 1: Obtain & Upload the Sketch

Sketch WorkingSketch Working

Wiring For UploadWiring For Upload

Assigning The HostnameAssigning The Hostname

Board In Download ModeBoard In Download Mode

Trying To Connect To A Network, AP ActiveTrying To Connect To A Network, AP Active

When the ESP32 board boots, it checks to see if any previous network credentials have been stored in the FLASH. By default, it will try to connect to them and if successful then it will print the IP address to the Serial port. Your sketch will then operate as normal. If it is unable to connect to a WiFi network then it will create an access point that you can connect to and manage the WiFi credentials.

Let’s start by installing the required libraries in the Arduino IDE. We need to install the AutoConnect library. Open up the library manager and type in AutoConnect. Install the library that shows up. The AutoConnect library needs the PageBuilder library for it to function so type in PageBuilder and install that as well. Then, download and open up the sketch for this project. You don’t have to change anything in the sketch but if you want to, then you can assign a different hostname for your board. It’s now time to upload the sketch. Connect the board using the diagram above, open up the serial terminal and press the reset button. You will receive a message indicating that the board is ready to receive code. Hit the upload button and wait for it to complete. Keep the serial terminal open, remove the boot jumper and press the reset button.

At this point, either two things will happen. If previous network information was stored in the flash memory then the board would automatically connect to the network and print out the IP address and hostname. In this case, you do not have to do anything. If you want to erase the stored credentials then you will have to erase the FLASH memory and the video has instructions for you to do this. However, if it was a new board or if there was no valid information then it would create an access point.

Step 2: Connect to AP and Manage WiFi

Connect To The esp32ap NetworkConnect To The esp32ap Network

Board StatusBoard Status

Use The Side-Menu And Tap The Configure OptionUse The Side-Menu And Tap The Configure Option

Enter The SSID And Password DetailsEnter The SSID And Password Details

Board Will Connect To The WiFi NetworkBoard Will Connect To The WiFi Network

Management Page Can be Accessed By Using HostnameManagement Page Can be Accessed By Using Hostname

If the board is unable to connect to a WiFi network then it will create an access point named “esp32ap” and this normally shows up after about 30 seconds. Connect to it by using the default password of 12345678. It should automatically redirect you to the management page or else, you can use the IP address 172.217.28.1 to get to it. The page gives you some information about the board like the MAC address, memory status and so on. The menu gives you a number of options: The ability to configure new access points or networks. View saved SSIDs or networks. Disconnect from the current network. Reset or restart the board. Change the time zone. And also go to the home page which simply displays the time.

Tap the configure new AP option. Select the access point from the list and type in the password. Once done, hit apply and the board should connect to the network and give you the network details as shown in the images. The IP address will also be printed to the serial terminal along with the hostname.

The next time you boot the board, it will automatically connect to a WIFI network and your sketch will work as expected.

Step 3: Deleting WiFi Networks

Options To Delete WiFi NetworksOptions To Delete WiFi Networks

I have not found a simple way to erase the stored SSID details from the flash by using the management page. One way to do this is by restoring the board to factory defaults by using the esptool which can be used on Windows and Mac. Doing this means that you will have to upload your sketch again. If you’re running Windows then there’s an easier way for you to do this. You can download and install the ESP32 Flash Download tool. The video shows you how to use this tool. The flash download tool does not work for Mac so the only option you have is using the esptool. You will need to install it first by using the terminal and you can then erase the flash by specifying the port. Again, please refer to the video for the commands and how to use them.

Network Time Using ESP8266

Network Time Using ESP8266

We learn how to build a cute little digital clock that communicates with NTP servers and displays network or internet time. We use the WeMos D1 mini to connect to a WiFi network, obtain the NTP time and display it on an OLED module.

The video above talks you through the entire process of building this project.

Step 1: Gather the Electronics

We Need The WeMos D1 Mini And A 0.96 oled moduleWe Need The WeMos D1 Mini And A 0.96″ OLED Module

To build this project, you will need a WeMos D1 mini or compatible board that uses the ESP8266 chipset along with an OLED module. The sketch should also work with the ESP32 boards but I haven’t tested this.

Step 2: Edit and Upload the Sketch

Sketch ModulesSketch Modules

Add Your WiFi CredentialsAdd Your WiFi Credentials

Set The Time Zone For Your RegionSet The Time Zone For Your Region

Install The U8g2 LibraryInstall The U8g2 Library

Add The ESP8266 BSP URLAdd The ESP8266 BSP URL

Install The ESP8266 BSPInstall The ESP8266 BSP

Select The Correct Board SettingsSelect The Correct Board Settings

Make Sure The Correct Time Is PrintedMake Sure The Correct Time Is Printed

Download the sketch using the link at the bottom of this post and open it using the Arduino IDE.

Start by adding your network credentials as we need to connect to a WiFi network. Then, make sure you add the correct time zone information. You can visit the following link to obtain the relevant time zone string for your region: https://remotemonitoringsystems.ca/time-zone-abbreviations.php

Before you can upload the sketch, make sure that you have installed the U8g2 library along with the board support package for the ESP8266 boards. Check the images for further information or watch the video for detailed instructions. Once completed, plug in the board and make sure you have selected the right board settings as seen in the image. Then, hit the upload and wait for it to complete.

Once uploaded, open up the serial monitor and make sure that the correct time is being displayed. If not, make sure you have selected the right time zone for your region.

Step 3: Connect the OLED Module

Wiring Connections For OLED ModuleWiring Connections For OLED Module

Wire Up The OLED Module And TestWire Up The OLED Module And Test

Use the wiring diagram above to connect the OLED module to the microcontroller board. Power on the board and you should see the time displayed on the module.

Step 4: Add the Modules to the Enclosure

The 3D Printed EnclosureThe 3D Printed Enclosure

Wire The OLED, Add Double Sided Tape And PlaceWire The OLED, Add Double Sided Tape And Place

Wire up The OLED To The WeMosWire up The OLED To The WeMos

Final Build Top ViewFinal Build Top View

Final BuildFinal Build

Once you’re happy with the results, download and 3D print the model from the following link: https://www.thingiverse.com/thing:857858

The board sits on the back cover while the OLED module sits at the end of the enclosure. You can use double-sided tape to hold the OLED module in place and you can also add a bit of hot glue near the wires to keep it in place. Solder the wires from the OLED module to the microcontroller board and then seal the unit by using some glue to hold it together. Plugin the microUSB cable and it should work as expected.

Video Recording With The ESP32-CAM Board

Video Recording With The ESP32-CAM Board

We take a look at a very interesting GitHub repository that enables video recording on the ESP32-CAM board. A video is nothing but a series of carefully timed images, and this sketch is based on that. The team have also added FTP functionality to the sketch which means that you can retrieve the files remotely, over the same WiFi network, without having to retrieve the microSD card.

The video above covers everything you need to know and also explains how to use the FTP feature.

Step 1: Gather the Electronics

Gather The ComponentsGather The Components

The ESP32-CAM board already contains the camera module, and microSD card slot that we need for this sketch. In addition to this, you will need a microSD card, a microUSB breakout board (optional), and also a USB to serial converter to upload the sketch.

Step 2: Edit & Upload the Sketch

Add Network CredentialsAdd Network Credentials

Default Recording SettingsDefault Recording Settings

Connections For Sketch UploadConnections For Sketch Upload

Serial Output When Board Is In Download ModeSerial Output When Board Is In Download Mode

Serial Output With IP Address And StatusSerial Output With IP Address And Status

You can download the sketch using the link at the bottom of this post.

The ESP32-CAM board does not have an onboard USB connector so you need to use an external USB to serial converter to upload the sketch. You can use the wiring connections shown above but make sure that the USB to serial converter is connected in the 3.3V mode.

It is recommended to use an external 5V supply to power the board, particularly if you are using an FTDI breakout board. For the external 5V supply, a simple USB breakout board will do just fine. There has been some success in powering the board directly from the CP2102 breakout board so you can try that first. The board also has a 3.3V power pin if needed.

The jumper is needed to put the board in the download mode. Once you have everything connected, power up the board, open a serial terminal (Tools->Serial Monitor) with a baud rate of 115, 200 and press the reset button. You should obtain the debug output as shown in the image and this will indicate that everything is working as expected.

You can now upload the code by pressing the upload button. Wait for it to complete, then remove the jumper and press the reset button to obtain the final output which will indicate that the recording has started.

Step 3: Disable Flash by Modifying the Board

Disabling The LED Flash (optional)Disabling The LED Flash (optional)

This bit is optional but you can disable the onboard LED flash by lifting a transistor pin on the board. Since the LED flash control line is shared with the microSD card, it will light up and toggle when the microSD card is being accessed. The GitHub page shows you how to make this change and it is completely reversible so you can always enable it later.

If you don’t feel like making this change then you can simply block the LED flash if it is causing a disturbance.

Step 4: Control the Board

Main Control PageMain Control Page

Video Start/Stop PageVideo Start/Stop Page

Video Recording Start PageVideo Recording Start Page

Still Image From Sample VideoStill Image From Sample Video

Still Image From Sample VideoStill Image From Sample Video

The board will print out the IP address once it connects to the WiFi network. You can type this into the web browser to access the control pages. The sketch also associates the hostname desklens.local to the board and you can type this into the address bar instead of the IP address. The pages contain hints to get you started and you can even specify the recording settings directly into the address bar.

The sketch also creates a basic FTP server and you can access the microSD card contents by using this feature. It is recommended to use a FTP client for this and the video walks you through the steps of using FileZilla.

Motion Triggered Image Capture With Email

Motion Triggered Image Capture With Email - ESP32-CAM

We build upon the previous ESP32-CAM projects and build a motion-triggered image capturing system that also sends an email with the image as an attachment. This build uses the ESP32-CAM board along with a PIR sensor module that is based on the AM312 sensor. The board spends most of the time in sleep mode and wakes up to take an image once motion is detected. In part 1, we modify the previous time-lapse sketch to add the motion detection feature. We then update the sketch and add the email feature in part 2.

The video above covers everything you need to know and also explains how the sketch is put together.

Step 1: Gather the Electronics

Required PartsRequired Parts

Build SectionsBuild Sections

The ESP32-CAM board already contains the camera module, and microSD card slot that we need for this sketch. In addition to this, you will need a microSD card, a PIR sensor module (based on the AM312 sensor), a general-purpose NPN transistor (BC547, BC548, BC549 or 2N3904), a microUSB breakout board, a 10K Ohm and 1K ohm resistor and also a USB to serial converter to upload the sketch.

Step 2: Upload the Sketch for Part 1

Connections For UploadConnections For Upload

The ESP32-CAM board does not have an onboard USB connector so you need to use an external USB to serial converter to upload the sketch. You can use the wiring connections shown above but make sure that the USB to serial converter is connected in the 3.3V mode.

It is recommended to use an external 5V supply to power the board, particularly if you are using an FTDI breakout board. For the external 5V supply, a simple USB breakout board will do just fine. There has been some success in powering the board directly from the CP2102 breakout board so you can try that first. The board also has a 3.3Vpower pin if needed.

The jumper is needed to put the board in the download mode. Once you have everything connected, power up the board, open a serial terminal (Tools->Serial Monitor) with a baud rate of 115,200 and press the reset button. You should obtain an output as shown in the image and this will indicate that everything is working as expected.

You can download the sketch using the link at the bottom of this post.

Step 3: Connect the Circuit and Test

Final Circuit ConnectionsFinal Circuit Connections

Test To Make Sure Everything Works As ExpectedTest To Make Sure Everything Works As Expected

Build the circuit using a breadboard and test to make sure everything works as expected. I added a multimeter to the final sensor output to help determine its state. Once you’re happy with the way everything works, move on to part 2.

Step 4: Install the Mail Client Library

We Need To Install This LibraryWe Need To Install This Library

Search And Install ItSearch And Install It

Open up the library manager and type in “ESP32 Mail Client”. Install the library that shows up as we need this for the sketch.

Step 5: Upload the Sketch for Part 2

Sketch ModulesSketch Modules

Add Your Network And Email DetailsAdd Your Network And Email Details

Enable Less Secure AppsEnable Less Secure Apps

Add Multiple Recipients, If NeededAdd Multiple Recipients, If Needed

Serial Output If Everything Is OkSerial Output If Everything Is Ok

Sample Email That Was SentSample Email That Was Sent

Download the sketch using the link at the bottom of this post.

Open it up using the Arduino IDE and then update it with your details. You will need to add the network name and password as the board needs to connect to the WiFI network. You will also need to provide an email address along with the password for the board to send the email. I’d recommend creating a new GMAIL account. Once the account is created, you need to enable less secure apps by visiting the following link:

https://myaccount.google.com/lesssecureapps?pli=1

You also need to specify the recipient and you can have multiple if required. Watch the video to learn more. Once all of this is done, upload the sketch to the board and power it ON. I’d recommend connecting the serial terminal and watching the output as this will notify you if there are any errors.

If everything works as expected, then the board should capture, save an image and also send it across as an email.

Step 6: Add the Electronics to an Enclosure

Attaching The AM312 Sensor To The PCBAttaching The AM312 Sensor To The PCB

Wiring Everything TogetherWiring Everything Together

Glueing In The PIR SensorGlueing In The PIR Sensor

Glueing In The microUSB BoardGlueing In The microUSB Board

Test The Final BuildTest The Final Build

I used the enclosure from the following link: https://www.thingiverse.com/thing:3667886

I added supports and printed it face down, which didn’t give a very good finish but this should work for now. Since the PIR sensor was a little too big for the enclosure, I desoldered the sensor and connected it to the PCB using wires. I then created the interfacing circuit using a protoboard and wired it in place. Use the connection diagram shown earlier to connect it all together and you can add some Kapton tape for insulation.

I started by glueing in the PIR sensor, followed by the PIR PCB. I then placed the ESP32 board inside and realised that the case doesn’t allow you to access the microSD card but this didn’t matter to me as the images would be emailed. I then added the interface board and glued in the microUSB breakout board. Finally, I bent the transistor to allow the cover to close in place. Power on the board and it should take an image when motion is detected.