ESP32 S3 Camera - Send Photo to Telegram When Motion Detected

This tutorial instructs you how to build a security camera that sends a photo to your phone. A PIR motion sensor watches the room. When somebody walks in front of it, the ESP32 S3 takes a photo and sends it to you with Telegram. You get the photo in a few seconds, anywhere in the world.

ESP32 S3 Camera - Send Photo to Telegram When Motion Detected

What you'll build:

  1. Your own Telegram bot that sends you messages
  2. An ESP32 S3 camera with a PIR motion sensor
  3. A photo on your phone every time the sensor sees a movement
  4. A quiet time, so you do not get 50 photos of the same person

Hardware Preparation

1×ESP32 S3 N16R8 OV5640 Camera
1×Alternatively, ESP32 S3 N16R8 OV2640 Camera
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion Sensor
1×Jumper Wires
1×Optionally, ESP32 S3 44-Pin Screw Terminal Block
1×Optionally, ESP32 S3 44-Pin Breakout Board
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

※ NOTE THAT:

The ESP32 S3 camera board has 40 pins, but the screw terminal block and the breakout board have 44 pins. They still work together. Put the camera board in the center of the 44-pin board, so 2 pins stay free on the left side and 2 pins stay free on the right side.

The screw terminal block lets you connect wires with a screwdriver, with no soldering. The breakout board gives you easy pin headers for a breadboard.

Overview of Telegram

Telegram is a free chat application for your phone and your computer. Telegram lets you make a bot. A bot is a robot friend in your chat list. Your ESP32 S3 sends the photo to the bot, and the bot shows it to you.

You need two secret texts:

NameWhat it isLooks like
Bot tokenThe password of your bot8012345678AAHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Chat IDThe number of your chat123456789

※ NOTE THAT:

Never show your bot token to anybody, and never put it on a website or on GitHub. Somebody with your token can control your bot.

Step 1: Make Your Bot

  1. Install Telegram on your phone, and make an account.
  2. In Telegram, search for @BotFather. It has a blue tick.
  3. Open the chat with BotFather and talk to it like this:
Telegram
Telegram 12:45
Welcome to Telegram!
ArduinoBot 10:19
Chatting with Arduino...
telegram-botfather
BotFather Yesterday
Your bot has been created.
BotFather

BotFather

bot
Today
/newbot
10:15 AM ✓✓
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
10:16 AM
My ESP32 Camera
10:17 AM ✓✓
Good. Now let's choose a username for your bot. It must end in bot.
10:18 AM
my_esp32_camera_bot
10:19 AM ✓✓
Done! Congratulations on your new bot. Use this token to access the HTTP API: 8012345678:AAHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10:20 AM
  1. Copy the bot token from the last message and keep it safe.

Step 2: Find Your Chat ID

  1. In Telegram, search for the name of your new bot and open the chat.
  2. Send any message to your bot, for example hello. This step is necessary. Telegram does not let a bot write to you before you have written to it.
Telegram
Telegram 12:45
Welcome to Telegram!
ArduinoBot 10:19
Chatting with Arduino...
telegram-botfather
BotFather Yesterday
Your bot has been created.

ArduinoBot

bot
Today
hello
10:15 AM ✓✓
  1. Open this address in your web browser. Put your own bot token in the place of <YOUR_BOT_TOKEN>:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
  1. The browser shows a long text. Look for "chat":{"id":. The number after it is your chat ID:
{ "ok": true, "result": [ { "update_id": 700000001, "message": { "message_id": 1, "from": { "id": 123456789, "first_name": "John" }, "chat": { "id": 123456789, "first_name": "John", "type": "private" }, "date": 1790000000, "text": "hello" } } ] }

※ NOTE THAT:

Does the browser show {"ok":true,"result":[]} with nothing inside? Then you did not send a message to your bot yet. Go back to step 2, send hello to your bot, then load the address again.

※ NOTE THAT:

Many other tutorials tell you to ask the bot @myidbot with the message /getid. That bot belongs to somebody else, and very often it does not answer at all. The web address above always works, because it asks Telegram directly.

※ NOTE THAT:

Forgot step 2? Then the ESP32 S3 shows the answer Forbidden: bot can't initiate conversation with a user in the Serial Monitor. Open the chat with your bot and send it any message, then try again.

Step 3: Put Them in the Code

Open the code and change these four lines:

const char *WIFI_SSID = "YOUR_WIFI_SSID"; const char *WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; const char *BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"; const char *CHAT_ID = "YOUR_TELEGRAM_CHAT_ID";

Overview of PIR Motion Sensor

A PIR motion sensor sees the heat of a human body. PIR means "Passive InfraRed". When a warm body moves in front of it, the OUT pin goes HIGH.

PIR Motion Sensor HC-SR501

Key Specifications

ItemValue
Power5V
Output3.3V HIGH when it sees a movement, LOW when it sees nothing
Distance3 to 7 meters (you can change it)
Angleabout 110°
Warm-up time30 to 60 seconds after power on

The sensor has two small orange screws. Turn the time delay screw all the way to the left, so the output stays HIGH for only about 3 seconds.

For more detail about the motion sensor, see the ESP32 S3 - Motion Sensor tutorial. To save the photos on a microSD card instead of sending them to Telegram, see the ESP32 S3 Camera - Save Photo to SD Card When Motion Detected tutorial.

Wiring Diagram between PIR Motion Sensor and ESP32 S3

Connect the PIR motion sensor to your ESP32 S3 with three jumper wires.

The wiring diagram between ESP32 S3 PIR Motion Sensor Telegram

This image is created using Fritzing. Click to enlarge image

PIR Sensor PinESP32 S3 Pin
VCC5V
GNDGND
OUTGPIO21

Safety Notes

The PIR sensor needs 5V on its VCC pin, not 3.3V. With 3.3V it looks like it works, but it gives wrong signals. Its OUT pin gives 3.3V, which is correct for the ESP32 S3.

The camera uses GPIO4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17 and 18. Do not use these pins for the sensor. GPIO21 is free, so we use it.

Arduino IDE Settings for ESP32 S3 Camera

The camera does not work with the default settings of the Arduino IDE. You must change some items in the Tools menu before you upload the code.

Tools Menu ItemSelect This
BoardESP32S3 Dev Module
Flash Size16MB (128Mb)
PSRAMOPI PSRAM
Partition SchemeHuge APP (3MB No OTA/1MB SPIFFS)
CPU Frequency240MHz (WiFi)
USB ModeHardware CDC and JTAG
Upload ModeUART0 / Hardware CDC
Upload Speed921600
USB CDC On BootEnabled

※ NOTE THAT:

PSRAM must be OPI PSRAM, and Partition Scheme must be Huge APP. Change Flash Size first, because the list of Partition Scheme options changes after that.

How To Send a Photo to Telegram

You do not need to install any library. The code talks to Telegram directly.

  • Telegram uses https, so we need a secure client.
#include <WiFiClientSecure.h> WiFiClientSecure client; client.setInsecure(); // do not check the certificate of the Telegram server client.connect("api.telegram.org", 443);

※ NOTE THAT:

setInsecure() means the ESP32 S3 does not check who the server really is. For a small home project this is fine, and it saves a lot of memory. The photo is still sent through an encrypted connection.

  • To send a photo, we use the Telegram address /bot<TOKEN>/sendPhoto. The photo travels inside a multipart message. A multipart message has parts, and a line of text called a boundary separates the parts. This project uses three parts: the chat ID, the text, and the photo.
String head = "--" BOUNDARY "\r\n"; head += "Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n"; head += String(CHAT_ID) + "\r\n"; head += "--" BOUNDARY "\r\n"; head += "Content-Disposition: form-data; name=\"caption\"\r\n\r\n"; head += "Motion detected!\r\n"; head += "--" BOUNDARY "\r\n"; head += "Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n"; head += "Content-Type: image/jpeg\r\n\r\n"; String tail = "\r\n--" BOUNDARY "--\r\n";

※ NOTE THAT:

The caption part is the text under the photo in Telegram. Change Motion detected! to any message you like.

  • The Content-Length must be the size of everything: the first part, the photo, and the last part.
size_t totalLength = head.length() + fb->len + tail.length();

※ NOTE THAT:

A wrong Content-Length is the most common mistake. Telegram then waits for more data and never answers.

  • Send the photo in small pieces. A big write() often fails on a secure connection.
for (size_t i = 0; i < fb->len; i += 1024) { size_t piece = fb->len - i; if (piece > 1024) piece = 1024; client.write(fb->buf + i, piece); }
  • Read the answer. When everything is fine, the answer of Telegram contains "ok":true.
if (answer.indexOf("\"ok\":true") > 0) { Serial.println("The photo was sent to Telegram"); }

※ NOTE THAT:

This project uses a small image size (FRAMESIZE_SVGA, 800×600), because a small photo is sent much faster. A photo of 50 KB takes about 3 seconds. A photo of 200 KB takes about 10 seconds.

ESP32 S3 Code

The following code sends a photo to your Telegram chat every time the sensor sees a movement. The quiet time is 30 seconds. To change it, change the line #define QUIET_TIME 30000.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-camera-send-photo-to-telegram-when-motion-detected */ #include <WiFi.h> #include <WiFiClientSecure.h> #include "esp_camera.h" const char *WIFI_SSID = "YOUR_WIFI_SSID"; // CHANGE IT const char *WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; // CHANGE IT const char *BOT_TOKEN = "1234567890:ABCDefGhIJKlmNoPQRsTUVwXYZ1234567890"; // CHANGE IT const char *CHAT_ID = "1234567890"; // CHANGE IT #define PIR_PIN 21 // the PIR motion sensor is connected to GPIO21 #define QUIET_TIME 30000 // wait 30 seconds before the next message (30000 milliseconds) // Camera pins of the ESP32-S3-N16R8 camera board. // The same pins work for the OV2640, OV3660 and OV5640 versions. #define PWDN_GPIO_NUM -1 #define RESET_GPIO_NUM -1 #define XCLK_GPIO_NUM 15 #define SIOD_GPIO_NUM 4 #define SIOC_GPIO_NUM 5 #define Y9_GPIO_NUM 16 #define Y8_GPIO_NUM 17 #define Y7_GPIO_NUM 18 #define Y6_GPIO_NUM 12 #define Y5_GPIO_NUM 10 #define Y4_GPIO_NUM 8 #define Y3_GPIO_NUM 9 #define Y2_GPIO_NUM 11 #define VSYNC_GPIO_NUM 6 #define HREF_GPIO_NUM 7 #define PCLK_GPIO_NUM 13 #define TELEGRAM_HOST "api.telegram.org" #define BOUNDARY "diyablesphoto" int lastPirState = LOW; unsigned long lastSendTime = 0; // Returns the name of the camera sensor that is installed on the board const char *getSensorName(int pid) { switch (pid) { case OV2640_PID: return "OV2640 (2 MP, max UXGA 1600x1200)"; case OV3660_PID: return "OV3660 (3 MP, max QXGA 2048x1536)"; case OV5640_PID: return "OV5640 (5 MP, max QSXGA 2592x1944)"; default: return "Unknown sensor"; } } bool initCamera() { camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = Y2_GPIO_NUM; config.pin_d1 = Y3_GPIO_NUM; config.pin_d2 = Y4_GPIO_NUM; config.pin_d3 = Y5_GPIO_NUM; config.pin_d4 = Y6_GPIO_NUM; config.pin_d5 = Y7_GPIO_NUM; config.pin_d6 = Y8_GPIO_NUM; config.pin_d7 = Y9_GPIO_NUM; config.pin_xclk = XCLK_GPIO_NUM; config.pin_pclk = PCLK_GPIO_NUM; config.pin_vsync = VSYNC_GPIO_NUM; config.pin_href = HREF_GPIO_NUM; config.pin_sccb_sda = SIOD_GPIO_NUM; config.pin_sccb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; config.frame_size = FRAMESIZE_SVGA; // 800x600 - small, so it is sent quickly config.jpeg_quality = 12; // 0 to 63, a lower number means a better image config.fb_count = 1; config.fb_location = CAMERA_FB_IN_DRAM; config.grab_mode = CAMERA_GRAB_WHEN_EMPTY; if (psramFound()) { config.fb_location = CAMERA_FB_IN_PSRAM; config.jpeg_quality = 10; config.fb_count = 2; config.grab_mode = CAMERA_GRAB_LATEST; } else { config.frame_size = FRAMESIZE_QVGA; Serial.println("WARNING: PSRAM not found. Please set PSRAM to OPI PSRAM in the Tools menu."); } esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x\n", err); return false; } sensor_t *s = esp_camera_sensor_get(); Serial.printf("Camera sensor: %s\n", getSensorName(s->id.PID)); // Each sensor needs slightly different settings switch (s->id.PID) { case OV2640_PID: s->set_vflip(s, 0); s->set_hmirror(s, 0); break; case OV3660_PID: s->set_vflip(s, 1); // the OV3660 image is upside down by default s->set_brightness(s, 1); // make the image a little brighter s->set_saturation(s, -2); // make the colors a little softer break; case OV5640_PID: s->set_vflip(s, 0); s->set_hmirror(s, 0); break; default: break; } return true; } // Sends one photo to your Telegram chat bool sendPhotoToTelegram(camera_fb_t *fb) { WiFiClientSecure client; client.setInsecure(); // do not check the certificate of the Telegram server client.setTimeout(15); Serial.println("Connecting to Telegram..."); if (!client.connect(TELEGRAM_HOST, 443)) { Serial.println("Cannot connect to Telegram"); return false; } // The first part of the message: who receives the photo, and the text String head = "--" BOUNDARY "\r\n"; head += "Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n"; head += String(CHAT_ID) + "\r\n"; head += "--" BOUNDARY "\r\n"; head += "Content-Disposition: form-data; name=\"caption\"\r\n\r\n"; head += "Motion detected!\r\n"; head += "--" BOUNDARY "\r\n"; head += "Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n"; head += "Content-Type: image/jpeg\r\n\r\n"; // The last part of the message String tail = "\r\n--" BOUNDARY "--\r\n"; size_t totalLength = head.length() + fb->len + tail.length(); // Send the HTTP header client.print("POST /bot" + String(BOT_TOKEN) + "/sendPhoto HTTP/1.1\r\n"); client.print("Host: " TELEGRAM_HOST "\r\n"); client.print("Content-Type: multipart/form-data; boundary=" BOUNDARY "\r\n"); client.print("Content-Length: " + String(totalLength) + "\r\n"); client.print("Connection: close\r\n\r\n"); // Send the message client.print(head); // Send the photo in small pieces of 1024 bytes for (size_t i = 0; i < fb->len; i += 1024) { size_t piece = fb->len - i; if (piece > 1024) piece = 1024; client.write(fb->buf + i, piece); } client.print(tail); // Read the answer of Telegram String answer = ""; unsigned long startTime = millis(); while (client.connected() && millis() - startTime < 15000) { while (client.available()) { answer += (char)client.read(); startTime = millis(); } } client.stop(); if (answer.indexOf("\"ok\":true") > 0) { Serial.println("The photo was sent to Telegram"); return true; } Serial.println("Telegram did not accept the photo. The answer was:"); Serial.println(answer); return false; } void takeAndSendPhoto() { camera_fb_t *fb = esp_camera_fb_get(); if (!fb) { Serial.println("Photo capture failed"); return; } Serial.printf("Photo taken: %u bytes\n", fb->len); sendPhotoToTelegram(fb); esp_camera_fb_return(fb); // give the memory back to the camera } void setup() { Serial.begin(115200); delay(1000); pinMode(PIR_PIN, INPUT); if (!initCamera()) { Serial.println("Stopped"); while (true) delay(1000); } WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.print("Connected. IP address: "); Serial.println(WiFi.localIP()); Serial.println("The PIR sensor is warming up. Please wait 30 seconds and do not move."); delay(30000); Serial.println("Ready. Waiting for motion..."); } void loop() { int pirState = digitalRead(PIR_PIN); // LOW to HIGH means the sensor has just seen a movement if (lastPirState == LOW && pirState == HIGH) { if (millis() - lastSendTime >= QUIET_TIME) { Serial.println("Motion detected!"); takeAndSendPhoto(); lastSendTime = millis(); } } lastPirState = pirState; }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Make your Telegram bot and find your chat ID. See the steps above.
  3. Unplug the USB cable, then wire the PIR sensor as in the table above.
  4. Turn the time delay screw of the sensor all the way to the left.
  5. Copy the above code and paste it into the Arduino IDE.
  6. Change YOUR_WIFI_SSID, YOUR_WIFI_PASSWORD, YOUR_TELEGRAM_BOT_TOKEN and YOUR_TELEGRAM_CHAT_ID.
  7. Change the settings in the Tools menu. See the table above.
  8. Compile and upload the code to the ESP32 S3 board by clicking the Upload button in Arduino IDE.
Arduino IDE Upload Code
  1. Open the Serial Monitor in Arduino IDE.
How to open serial monitor on Arduino IDE
  1. Press the RESET button one time, then leave the room for 30 seconds. The sensor is warming up.
  2. Come back and walk in front of the camera.
  3. Look at your phone. The photo arrives in your Telegram chat, with the text under it:
Telegram
Telegram 12:45
Welcome to Telegram!
ArduinoBot 10:19
Chatting with Arduino...
telegram-botfather
BotFather Yesterday
Your bot has been created.

ArduinoBot

bot
Today
Motion detected!
10:15 AM
ESP32 S3 Camera Photo in Telegram

Serial Monitor

The following readings were captured in 2026:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-09-23 13:00:01] Camera sensor: OV5640 (5 MP, max QSXGA 2592x1944) [2026-09-23 13:00:01] Connecting to WiFi... [2026-09-23 13:00:04] Connected. IP address: 192.168.0.2 [2026-09-23 13:00:04] The PIR sensor is warming up. Please wait 30 seconds and do not move. [2026-09-23 13:00:34] Ready. Waiting for motion... [2026-09-23 13:01:12] Motion detected! [2026-09-23 13:01:12] Photo taken: 51353 bytes [2026-09-23 13:01:12] Connecting to Telegram... [2026-09-23 13:01:15] The photo was sent to Telegram
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Troubleshooting

The Serial Monitor shows Forbidden: bot can't initiate conversation with a user.

You never wrote to your bot. Open Telegram, find your bot, and send it any message. Then press RESET on the board.

The Serial Monitor shows Unauthorized.

Your bot token is wrong. Copy it again from BotFather. The token has two parts joined by : and you need all of it.

The Serial Monitor shows chat not found.

Your chat ID is wrong. Ask @myidbot again with /getid. Do not put spaces around the number.

The Serial Monitor shows Cannot connect to Telegram.

  • Check that your WiFi works. The IP address must appear in the Serial Monitor.
  • Some school and company networks block Telegram. Try your phone hotspot.

Telegram never answers, and the board waits.

The Content-Length does not match the real size. Do not change the head and tail texts unless you also change the size.

The photo takes a long time.

Use a smaller image size. FRAMESIZE_VGA (640×480) is about half of SVGA.

The sensor sees a movement all the time.

Wait one full minute after start, turn the sensitivity screw a little to the left, and check that VCC is on 5V.

I get too many photos.

Increase QUIET_TIME from 30000 to 60000 (one minute).

Applications

An ESP32 S3 camera with a motion sensor and Telegram is a complete security camera that costs very little and needs no monthly payment. Here are practical projects you can build with this code:

  1. WiFi home security camera: Get a photo on your phone the moment somebody enters your house, your hall, or your stairs.
  2. Package delivery alert: Know at once when the delivery person comes to your door, and see the parcel that was left.
  3. Baby room monitor: Get a photo when your child gets out of bed at night.
  4. Pet camera for dogs and cats: See what your pet does while you are at work, without paying for a cloud camera.
  5. Holiday house watcher: Watch an empty house or a second home from another country.
  6. Garage and driveway alarm: Get a photo when somebody walks near your car, your bicycle, or your motorbike.
  7. Office and shop after hours: Keep a photo of every person who moves inside after closing time.
  8. Garden and wildlife camera: Photograph birds, cats, foxes, or the animals that eat your vegetables at night.
  9. Elderly care helper: Know that a family member is moving around the house as usual, without a camera running all day.
  10. Warehouse and farm gate: Watch a place far from your house, where a wired alarm system would be too expensive.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenges

  1. Beginner: Send a text message with the photo, for example the time since the board started.
  2. Intermediate: Add a switch that turns the alarm on and off, so you do not get photos when you are at home.
  3. Advanced: Also save every photo to the microSD card, so you keep a copy even when the internet is down.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!