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

Arduino UNO-Based Vibration Monitoring and Control System with ADXL345 and L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Vibration Monitoring and Control System with ADXL345 and L298N Motor Driver

Circuit Documentation

Summary

This circuit is designed to control a vibrating table using an Arduino UNO, an L298N motor driver, a DC motor, a potentiometer, an ADXL345 accelerometer, and a 12V battery. The Arduino UNO reads vibration data from the ADXL345 accelerometer and adjusts the motor speed accordingly. The potentiometer allows manual control of the motor speed, and an emergency stop button is included for safety.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. L298N DC Motor Driver

    • Description: Dual H-Bridge motor driver.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  3. DC Motor

    • Description: Standard DC motor.
    • Pins: pin 1, pin 2
  4. Potentiometer

    • Description: Variable resistor used to control motor speed.
    • Pins: GND, Output, VCC
  5. ADXL345

    • Description: 3-axis accelerometer.
    • Pins: GND, VCC, CS, INT1, INT2, SDO, SDA, SCL
  6. 12V Battery

    • Description: Power source for the motor driver.
    • Pins: -, +
  7. Rocker Switch (SPST)

    • Description: Single-pole single-throw switch for emergency stop.
    • Pins: 1, 2
  8. Resistor (200 Ohms)

    • Description: Resistor with 200 Ohms resistance.
    • Pins: pin1, pin2
  9. Resistor (100 Ohms)

    • Description: Resistor with 100 Ohms resistance.
    • Pins: pin1, pin2

Wiring Details

Arduino UNO

L298N DC Motor Driver

DC Motor

Potentiometer

ADXL345

12V Battery

Rocker Switch (SPST)

Resistor (200 Ohms)

Resistor (100 Ohms)

Code Documentation

#include <Wire.h>
#include <Adafruit_SSD1306.h> // Install via Library Manager

// Define ADXL345 I2C address
#define ADXL345_ADDR 0x53

// OLED display configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// L298N motor driver pins
const int motorIn1 = 7;
const int motorIn2 = 8;
const int motorENA = 6;

// Potentiometer pin
const int potPin = A0;

// Emergency stop button pin
const int stopButtonPin = 2;

// LED indicators
const int greenLED = 10; // Normal vibration
const int redLED = 11;   // Excessive vibration

// Vibration thresholds
const float vibrationMinThreshold = 1000.0;
const float vibrationMaxThreshold = 8000.0;

// Variables for vibration monitoring
int16_t ax, ay, az;  // Acceleration values
float vibrationMagnitude = 0;
bool motorEnabled = true;

void setup() {
  // Initialize Serial Monitor
  Serial.begin(9600);

  // Initialize I2C communication
  Wire.begin();
  setupADXL345();

  // Set L298N motor driver pins as output
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  pinMode(motorENA, OUTPUT);

  // Configure emergency stop button
  pinMode(stopButtonPin, INPUT_PULLUP);

  // Configure LED indicators
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);

  // Initialize OLED display
  if ( !display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
    
    Serial.println("OLED initialization failed!");
    while (1); // Stop here if OLED fails
  }
  display.clearDisplay();
  display.display();

  Serial.println("Vibrating Table Initialized");
}

void loop() {
  // Check emergency stop button
  if (digitalRead(stopButtonPin) == LOW) {
    motorEnabled = false; // Disable motor
    stopMotor();
    displayMessage("Emergency Stop!");
    delay(500);
    return;
  } else {
    motorEnabled = true;
  }

  // Read accelerometer data
  readADXL345(ax, ay, az);

  // Calculate vibration magnitude (RMS)
  vibrationMagnitude = sqrt(sq(ax) + sq(ay) + sq(az));

  // Read potentiometer value to control motor speed
  int potValue = analogRead(potPin);
  int motorSpeed = map(potValue, 0, 1023, 0, 255);

  // Adjust motor speed dynamically based on vibration thresholds
  if (vibrationMagnitude < vibrationMinThreshold) {
    motorSpeed = constrain(motorSpeed + 50, 0, 255); // Increase speed
    digitalWrite(greenLED, HIGH);
    digitalWrite(redLED, LOW);
  } else if (vibrationMagnitude > vibrationMaxThreshold) {
    motorSpeed = constrain(motorSpeed - 50, 0, 255); // Decrease speed
    digitalWrite(greenLED, LOW);
    digitalWrite(redLED, HIGH);
  } else {
    digitalWrite(greenLED, HIGH);
    digitalWrite(redLED, LOW);
  }

  // Control motor speed with PWM
  if (motorEnabled) {