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

Arduino-Controlled OLED Display and DC Motor with Relay and BTS7960 Driver

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled OLED Display and DC Motor with Relay and BTS7960 Driver

Circuit Documentation

Summary of the Circuit

This circuit is designed to control a DC motor using an Arduino UNO microcontroller and a BTS 7960 motor driver. It also includes an OLED display for user interface, a 2-channel relay module for additional control capabilities, an LED indicator, and a 12V battery as the power source. The Arduino UNO communicates with the OLED display via I2C protocol and controls the motor driver and relay module through its digital I/O pins. The motor driver is connected to the DC motor, allowing for speed and direction control. The relay module is used to switch an external circuit, which is indicated by the LED.

Component List

OLED 128x64 I2C Monochrome Display

Arduino UNO

DC Motor

BTS 7960 Motor Driver

2-Channel Relay Module

LED: Two Pin (red)

12V Battery (mini)

Wiring Details

OLED 128x64 I2C Monochrome Display

Arduino UNO

DC Motor

BTS 7960 Motor Driver

2-Channel Relay Module

LED: Two Pin (red)

12V Battery (mini)

Documented Code

#include <U8glib.h>
#include <Wire.h>

// OLED setup
U8GLIB_SH1106_128X64 oled(U8G_I2C_OPT_NONE);

// Pins for the AC relay
const int relayPin = 7;

// Timing intervals for the AC relay (in milliseconds)
const unsigned long relayOnTime = 10000;   // 10 seconds = 10,000 milliseconds
const unsigned long relayOffTime = 10000;  // 10 seconds = 10,000 milliseconds

// Pins for motor control
int r_en = 2;
int l_en = 3;
int r_pwm = 5;
int l_pwm = 6;

// Timing intervals for the motors (in milliseconds)
const unsigned long motorRunTime = 10000; // 10 seconds = 10,000 milliseconds
const unsigned long motorOffTime = 10000; // 10 seconds = 10,000 milliseconds

// Variables to keep track of time
unsigned long relayStartMillis = 0;
unsigned long motorStartMillis = 0;
bool relayState = false;
bool motorState = false;

void setup(void) {
  // Initialize the relay pin as an output
  pinMode(relayPin, OUTPUT);

  // Initialize motor control pins as outputs
  pinMode(r_en, OUTPUT);
  pinMode(l_en, OUTPUT);
  pinMode(r_pwm, OUTPUT);
  pinMode(l_pwm, OUTPUT);

  // Start with relay off and motors off
  digitalWrite(relayPin, LOW);
  digitalWrite(r_en, LOW);
  digitalWrite(l_en, LOW);
  analogWrite(r_pwm, 0);
  analogWrite(l_pwm, 0);

  // Start motor immediately for testing
  motorStartMillis = millis();
  motorState = true;
  digitalWrite(r_en, HIGH);
  digitalWrite(l_en, HIGH);
  analogWrite(r_pwm, 45);  // Example PWM value for RPM calculation
  analogWrite(l_pwm, 0);
}

void loop(void) {
  unsigned long currentMillis = millis();

  // Relay control
  if (relayState && currentMillis - relayStartMillis >= relayOnTime) {
    relayState = false;
    relayStartMillis = currentMillis;
    digitalWrite(relayPin, LOW);
  } else if (!relayState && currentMillis - relayStartMillis >= relayOffTime) {
    relayState = true;
    relayStartMillis = currentMillis;
    digitalWrite(relayPin, HIGH);
  }

  // Motor control
  if (motorState && currentMillis - motorStartMillis >= motorRunTime) {
    motorState = false;
    motorStartMillis = currentMillis;
    analogWrite(r_pwm, 0);
    analogWrite(l_pwm, 0);
    digitalWrite(r_en, LOW);
    digitalWrite(l_en, LOW);
  } else if (!motorState && currentMillis - motorStartMillis >= motorOffTime) {
    motorState = true;
    motorStartMillis = currentMillis;
    digitalWrite(r_en, HIGH);
    digitalWrite(l_en, HIGH);
    analogWrite(r_pwm, 45);  // Example PWM value for RPM calculation
    analogWrite(l_pwm, 0);
  }

  // Display the info on the OLED screen
  oled.firstPage();
  do {
    displayInfo(currentMillis);
  } while (oled.nextPage());

  delay(1000);  // Update the display every second
}

// Function to display RPM and timer info on the OLED screen
void displayInfo(unsigned long currentMillis) {
  // Calculate elapsed times
  unsigned long relayElapsedTime = relayState ? (currentMillis - relayStartMillis) : (relayOffTime - (currentMillis - relayStartMillis));
  unsigned long motorElapsedTime = motorState ? (currentMillis - motorStartMillis) : (motorOffTime - (currentMillis - motorStartMillis));
  
  // Calculate RPM (example: pwmValue is fixed at 45 for this case)
  int pwmValue = 45;
  int rpm = pwmValue / 8;

  // Page 1: Display RPM
  oled.setFont(u8g_font_profont12);
  oled.setPrintPos(0, 10);
  oled.print("RPM = ");
  oled.print(rpm);

  // Page 2: Display Motor ON/OFF Time
  oled.setPrintPos(0, 25);
  oled.print(motorState ? "Motor ON = " : "Motor OFF = ");
  oled.print(motorElapsedTime / 1000); // Display in seconds