Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Bluetooth-Controlled DC Motor with ESP32 and NPN Transistor

View and Edit Project In Cirkit DesignerImage of Bluetooth-Controlled DC Motor with ESP32 and NPN Transistor

Circuit Documentation

Summary

This circuit is designed to control a DC motor using an ESP32 microcontroller and an NPN transistor. The motor can be turned on and off via Bluetooth commands sent to the ESP32. The circuit includes a 9V battery to power the motor, a resistor to limit the current to the transistor's base, and the ESP32 to handle Bluetooth communication and control signals.

Component List

DC Motor

NPN Transistor (ECB)

Resistor

ESP32 DEVKIT V1 (30 pins)

9V Battery

Wiring Details

DC Motor

NPN Transistor (ECB)

Resistor

ESP32 DEVKIT V1 (30 pins)

9V Battery

Code Documentation

Bluetooth Motor Control Code

#include "BluetoothSerial.h" // Include BluetoothSerial library

// Define motor control pin
#define MOTOR_PIN 15

// Create a BluetoothSerial object
BluetoothSerial ESP_BT;

// Variable to track motor state
bool motorState = false;

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  Serial.println("Starting ESP32 Bluetooth Motor Control...");

  // Initialize Bluetooth
  if (!ESP_BT.begin("ESP32_Motor_Control")) {
    Serial.println("Bluetooth initialization failed!");
    while (1);
  }
  Serial.println("Bluetooth initialized. Waiting for connection...");

  // Set up motor pin as output
  pinMode(MOTOR_PIN, OUTPUT);
  digitalWrite(MOTOR_PIN, LOW); // Motor off by default
}

void loop() {
  // Check if data is available via Bluetooth
  if (ESP_BT.available()) {
    String command = ESP_BT.readString(); // Read the incoming command
    command.trim(); // Remove any extra whitespace
    Serial.print("Received command: ");
    Serial.println(command);

    // Turn motor ON
    if (command == "ON") {
      motorState = true;
      digitalWrite(MOTOR_PIN, HIGH); // Turn motor on
      ESP_BT.println("Motor is ON");
    }
    // Turn motor OFF
    else if (command == "OFF") {
      motorState = false;
      digitalWrite(MOTOR_PIN, LOW); // Turn motor off
      ESP_BT.println("Motor is OFF");
    }
    // Invalid command
    else {
      ESP_BT.println("Invalid command. Use 'ON' or 'OFF'.");
    }
  }
}

This code initializes the ESP32 for Bluetooth communication and sets up a motor control pin. It listens for Bluetooth commands to turn the motor on or off and responds accordingly.