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

Arduino UNO Controlled Brushless Motor with SD Data Logging and Buzzer Alert System

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Brushless Motor with SD Data Logging and Buzzer Alert System

Circuit Documentation

Summary

This circuit is designed to control a brushless motor using an Arduino UNO as the central processing unit. It includes a piezo buzzer for audible feedback, a pushbutton for user input, a speed controller to manage the motor's speed, and a current sensor to monitor the electrical current. Additionally, an SD module is incorporated for data logging purposes. The circuit is powered by a battery, and a resistor is used for pull-down configuration on the pushbutton.

Component List

Arduino UNO

Brushless Motor

Speed Controller

Piezo Buzzer

Pushbutton

Resistor

Battery

SD Module

ACS712 Current Sensor

Wiring Details

Arduino UNO

Brushless Motor

Speed Controller

Piezo Buzzer

Pushbutton

Resistor

Battery

SD Module

ACS712 Current Sensor

Documented Code

#include <SPI.h>
#include <SD.h>
#include <Servo.h>  // Include the Servo library

// Pin Definitions
const int buttonPin = 4;
const int buzzerPin = 2;
const int currentSensorPin = A0;
const int chipSelect = 10;  // Pin for the SD card module

// Program Variables
File dataFile;  // Create a file object to write to the SD card
Servo esc;  // Create a Servo object to control the ESC
bool programStarted = false;
bool buzzerStarted = false; // Ensures buzzer only activates after button press
bool buzzerOn = false; // Tracks if the buzzer should be on
unsigned long lastBuzzerTime = 0;
unsigned long lastThrottleTime = 0;
unsigned long lastSampleTime = 0;
int buzzerCount = 0;
int throttleValue = 1000;  // Start at 1000 µs (0% throttle for ESC)
bool holdingThrottle = false;  // Tracks if we're in the holding phase

void setup() {
  pinMode(buttonPin, INPUT);  // Button with pull-down resistor
  pinMode(buzzerPin, OUTPUT);
  esc.attach(6);  // Attach the ESC to pin 6
  esc.writeMicroseconds(throttleValue);  // Initialize PWM to 1000 µs
  digitalWrite(buzzerPin, LOW);  // Ensure buzzer is OFF initially
  Serial.begin(9600);  // For current data output

  // Initialize the SD card

  // Create a new file (or open an existing one)
  dataFile = SD.open("currentData.txt", FILE_WRITE);
  if (dataFile) {
    Serial.println("Writing to file...");
    dataFile.println("Time (ms), Current (A)"); // Header row
    dataFile.close();
  } else {
    Serial.println("Error opening file!");
  }
}

void loop() {
  // Check if the button is pressed to start the program
  if (digitalRead(buttonPin) == HIGH && !programStarted) {
    if (!SD.begin(chipSelect)) {
      Serial.println("SD card initialization failed!");
      return;
    }
    Serial.println("SD card initialized.");
    programStarted = true;
    buzzerStarted = true;      // Enable buzzer sequence only after button press
    lastBuzzerTime = millis(); // Start the buzzer timing
    buzzerCount = 0;           // Reset buzzer count at program start
  }

  // Buzzer Activation (5 times, 0.25 second ON, 0.5 second OFF) after button press
  if (buzzerStarted && buzzerCount < 5) {
    unsigned long currentMillis = millis();
    
    if (buzzerOn && currentMillis - lastBuzzerTime >= 250) {  // Buzzer ON duration
      digitalWrite(buzzerPin, LOW);  // Turn buzzer OFF
      buzzerOn = false;
      lastBuzzerTime = currentMillis;
      buzzerCount++;  // Increase beep count after each OFF period
    } 
    else if (!buzzerOn && currentMillis - lastBuzzerTime >= 500) {  // Buzzer OFF duration
      digitalWrite(buzzerPin, HIGH);  // Turn buzzer ON
      buzzerOn = true;
      lastBuzzerTime = currentMillis;
    }
  } 
  
  // Throttle Ramp-Up (1000 µs to 1250 µs over 5 seconds) after buzzer completes
  else if (programStarted && buzzerCount >= 5 && throttleValue < 1500 && !holdingThrottle) {
    if (millis() - lastThrottleTime >= 100) {
      lastThrottleTime = millis();
      throttleValue += 15;  // Increase PWM gradually to reach 1250 µs (approx. 25% throttle)
      esc.writeMicroseconds(throttleValue);  // Set ESC throttle
    }
  } 
  
  // Maintain 25% Throttle and Read Current Sensor Data (5 seconds at 50 Hz)
  else if (programStarted && throttleValue >= 1500 && !holdingThrottle) {
    holdingThrottle = true;  // Enter holding phase
    lastThrottleTime = millis();  // Reset timing for the hold duration
  }
  
  // Hold the throttle and read current sensor data for 5 seconds
  if (holdingThrottle && millis() - lastThrottleTime < 5000) {
    if (millis() - lastSampleTime >= 100) {   // Sample current sensor at 50 Hz
      lastSampleTime = millis();
      
      // Read and calculate current from ACS712 sensor
      int currentReading = analogRead(currentSensorPin);
      float current = (currentReading - 512) *-1* (5.0 / 1024.0) / 0.066;
      
      // Print current to Serial Monitor
      Serial.println(current);

      // Open the file to append the current reading with timestamp
      dataFile = SD.open("test.txt", FILE_WRITE);
      if (dataFile