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

Raspberry Pi 4B Controlled DC Motor with OLED Feedback Display

View and Edit Project In Cirkit DesignerImage of Raspberry Pi 4B Controlled DC Motor with OLED Feedback Display

Circuit Documentation

Summary

This circuit is designed to control a DC motor using an L298N motor driver module, with a Raspberry Pi 4B as the main controller. The circuit also includes an OLED display to provide visual feedback on the motor's status. The motor's direction and speed are controlled by the Raspberry Pi 4B through GPIO pins connected to the L298N driver. The OLED display is interfaced with the Raspberry Pi 4B to show the current status of the motor, such as "FORWARD", "BACKWARD", and "STOPPED".

Component List

Raspberry Pi 4B

L298N DC Motor Driver

DC Motor

OLED Display 2.42"

Battery 12V

Wiring Details

Raspberry Pi 4B

L298N DC Motor Driver

DC Motor

OLED Display 2.42"

Battery 12V

Documented Code

/*
 * This Arduino Sketch controls a DC motor using an L298N motor driver and
 * displays status on an OLED display. The Raspberry Pi 4B is used to
 * interface with the motor driver and the OLED display. The motor is
 * controlled via GPIO pins connected to the L298N driver, and the OLED
 * display shows the motor status.
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

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

// Motor driver pins
const int IN1 = 2;
const int IN2 = 3;
const int IN3 = 4;
const int IN4 = 17;
const int ENA = 27;
const int ENB = 22;

void setup() {
  // Initialize motor driver pins
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  // Initialize OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.display();
  delay(2000);
  display.clearDisplay();

  // Display initial message
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println(F("Motor Control Initialized"));
  display.display();
}

void loop() {
  // Motor forward
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 255);
  analogWrite(ENB, 255);

  // Update display
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(F("Motor Status: FORWARD"));
  display.display();
  delay(2000);

  // Motor backward
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENA, 255);
  analogWrite(ENB, 255);

  // Update display
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(F("Motor Status: BACKWARD"));
  display.display();
  delay(2000);

  // Motor stop
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);

  // Update display
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(F("Motor Status: STOPPED"));
  display.display();
  delay(2000);
}

This code is designed to run on a Raspberry Pi 4B, which is not typically programmed using Arduino sketches. However, the code provided is written in the style of an Arduino sketch, which suggests that it may be intended for use with an Arduino-compatible environment on the Raspberry Pi, such as using the Arduino IDE with an add-on for Raspberry Pi GPIO programming.