In this tutorial, we will explore how to control a DC motor using the ESP32-CAM module. The DC motor is connected via a 5V relay, and the relay is controlled using a 2N2222 transistor and a 1kΩ resistor connected to GPIO16 of the ESP32-CAM. This project is a simple yet effective way to integrate motor control into your ESP32-CAM projects.
Components Needed
- ESP32-CAM module
- DC motor
- 5V relay
- 2N2222 NPN transistor
- 1kΩ resistor
- Breadboard and connecting wires
Circuit Diagram
The circuit setup is straightforward:
- Connect the DC motor to the 5V relay.
- Connect the relay control pin to the collector of the 2N2222 transistor.
- Connect the emitter of the transistor to the ground.
- Connect the base of the transistor to GPIO16 of the ESP32-CAM through a 1kΩ resistor.
See the circuit diagram of connecting a dc motor with esp32-cam module, relay and transistor.
Code for Motor Control
Here is the Arduino code to control the motor:
const int motorControlPin = 16;
void setup(){
pinMode(motorControlPin, OUTPUT);
}
void loop(){
digitalWrite(motorControlPin, HIGH);
delay(1000);
digitalWrite(motorControlPin, LOW);
delay(1000);
}
How It Works
The ESP32-CAM module controls the relay via GPIO16. When GPIO16 is set HIGH, the relay is activated, powering the DC motor. When GPIO16 is LOW, the relay is deactivated, and the motor stops. The code toggles the motor state every second, demonstrating a simple on/off control mechanism.
Further Reading
For more detailed tutorials on related topics, you can check out these posts on my blog:
- DC Motor Speed Control with Potentiometer and PWM using Arduino: Learn how to control the speed of a DC motor using PWM and a potentiometer.
- Arduino Push Button LED On/Off Tutorial: A basic tutorial on controlling an LED with a push button, which can be adapted for motor control applications.
These resources will provide you with more insights into motor control and other Arduino projects.
Conclusion
Controlling a DC motor with an ESP32-CAM module is simple and efficient. By using a relay and a transistor, you can easily integrate motor control into your ESP32-CAM projects. Whether you are building a surveillance system with the ESP32-CAM or any other project, adding motor control can significantly enhance its capabilities.