ESP32 C3 Super Mini - Motion Sensor - Relay

This ESP32 C3 Super Mini motion sensor relay tutorial shows you how to switch a device on automatically when someone moves nearby. The ESP32 C3 Super Mini turns the relay on when the HC-SR501 detects motion, and turns it off when the motion stops.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Motion Sensor - Relay

Hardware Preparation

1×ESP32 C3 Super Mini
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×Relay
1×Optionally, Solenoid Lock
1×Optionally, 12V Active Buzzer
1×Optionally, 12V Cooling Fan
1×Optionally, 12V Warning Light Bright Waterproof
1×12V Power Adapter
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

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 .

Overview of Relay and Motion Sensor

A relay is a switch your code can control, and the HC-SR501 is a sensor that detects moving people and animals.

Wiring Diagram

Connect the motion sensor and the relay to your ESP32 C3 Super Mini as shown below.

  • Note: Both the HC-SR501 and the relay use the 5V pin for power
  • Note: The OUTPUT pin of the HC-SR501 gives about 3.3V, so it is safe for GPIO5
  • Note: Turn off all power before you change any wire
The wiring diagram between ESP32 C3 Super Mini Motion Sensor Relay

This image is created using Fritzing. Click to enlarge image

HC-SR501 Pin ESP32 C3 Super Mini Pin
VCC 5V
GND GND
OUTPUT GPIO5
Relay Pin ESP32 C3 Super Mini Pin
DC+ 5V
DC- GND
IN GPIO6

Initial Setting

Set the HC-SR501 adjusters before you run the code.

Setting Configuration
Detection Range Adjuster Fully screw it in the clockwise direction.
Time Delay Adjuster Fully screw it in the anti-clockwise direction.
Repeat Trigger Selector Put jumper like the below image.
ESP32 C3 Super Mini motion sensor initial setting

ESP32 C3 Super Mini Code

This code turns the relay on and off based on the motion sensor.

What This Code Does:

  • Reads the HC-SR501 motion sensor on GPIO5
  • Detects when motion starts (LOW to HIGH)
  • Turns the relay on with GPIO6 when motion starts
  • Detects when motion stops (HIGH to LOW)
  • Turns the relay off and prints each event to the Serial Monitor
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-motion-sensor-relay */ #define MOTION_SENSOR_PIN 5 // The ESP32 C3 SuperMini pin GPIO5 connected to the OUTPUT pin of motion sensor #define RELAY_PIN 6 // The ESP32 C3 SuperMini pin GPIO6 connected to Relay's pin int motionStateCurrent = LOW; // current state of motion sensor's pin int motionStatePrevious = LOW; // previous state of motion sensor's pin void setup() { Serial.begin(115200); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 C3 SuperMini pin to input mode pinMode(RELAY_PIN, OUTPUT); // set ESP32 C3 SuperMini pin to output mode } void loop() { motionStatePrevious = motionStateCurrent; // store old state motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!, turns relay ON"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!, turns relay OFF"); digitalWrite(RELAY_PIN, LOW); // turn off } }

Detailed Instructions

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Wire It Up: Do the wiring as shown in the image above.
  • Connect the Board: Connect the ESP32 C3 Super Mini to your PC with a USB Type-C cable.
  • Open the IDE: Open Arduino IDE on your PC.
  • Select the Board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Select the Port: Choose the right COM port in Tools > Port.
  • Paste the Code: Copy the code above and paste it into Arduino IDE.
  • Upload: Click the Upload button on Arduino IDE to compile and upload the code.
Arduino IDE Upload Code
  • Open Serial Monitor: Set the baud rate to 115200.
  • Test It: Move your hand in front of the sensor.
  • Check the Result: See the relay change state and read the message in the Serial Monitor.
  • Pro Tip: If the Serial Monitor stays empty, set Tools > USB CDC On Boot to Enabled and upload again.

Line-by-line Code Explanation

The ESP32 C3 Super Mini code above has a comment on each important line.

  • Read the comments: They explain what each line does, line by line.

Serial Monitor Output

When you move in front of the sensor and then stop, you will see this:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
Motion detected!, turns relay ON Motion stopped!, turns relay OFF Motion detected!, turns relay ON Motion stopped!, turns relay OFF
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

Relay vs MOSFET Module

A relay is not the only way to switch a device. If your device runs on DC power, such as a 12V LED strip, a 12V solenoid lock or a 12V fan, a MOSFET module can do the same job.

Relay MOSFET Module
What it can do turn the device on or off turn the device on or off, and set its power (PWM)
Noise when switching a click every time no sound
How fast it switches slow, a few milliseconds very fast
Life the contacts wear out after many switches nothing moves, so it lasts long
Type of power AC or DC DC only
Device power supply any voltage the relay is rated for about 6V or more
Isolation from the ESP32 C3 Super Mini yes, between coil and contacts yes, by the PC817 optocoupler
Good for AC devices, or a simple ON/OFF DC devices, or dimming and speed control

Both the relay and the MOSFET module turn on when the ESP32 C3 Super Mini pin is HIGH. The pin gives only 3.3V, but that is enough: the LED of the optocoupler on the module needs just a few milliamps. So for a simple ON/OFF, the code above stays the same and only the wiring changes.

Project Ideas

You can connect many devices to this ESP32 C3 Super Mini motion sensor relay project.

  • Automatic Light: Turn on a LED strip or light bulb when someone walks in
  • Intruder Siren: Sound a 12V buzzer or siren when motion is detected
  • Warning Light: Flash a bright warning light near a door or gate
  • Smart Fan: Run a 12V cooling fan only when someone is in the room
  • Automatic Lock: Trigger a solenoid lock when someone comes close
  • Moving Display: Start an actuator when a visitor walks past

Challenge Yourself

Try these ideas to grow your ESP32 C3 Super Mini skills.

  • Easy: Change the Serial Monitor messages to your own text
  • Easy: Add an LED on GPIO3 that follows the relay state
  • Medium: Keep the relay on for 30 seconds after the motion stops
  • Medium: Count how many times motion is detected and print the number
  • Advanced: Add a button that turns the motion control on and off
  • Advanced: Replace the relay with a MOSFET module and fade the device on with PWM

※ 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!