Oscillation movement with arduino and stepper motor

Hello, I started using Arduino for the first time. My first project is to give oscillation movement to a stepper motor. I tried a code but I can't get the exact result I want. Right now I can increase and decrease from 30 degrees to 90 degrees with a potentiometer. But I want to control the speed of the oscillation movement by putting a potentiometer instead of the button in the circuit. I would be very happy if you help.

I am leaving the code below. I am also adding the electronic diagram as a visual.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// LCD Ayarları (0x27 adresi)
LiquidCrystal_I2C lcd(0x27, 16, 2); 

// Pin Tanımlamaları
const int reverseSwitch = 2;  // Yön değiştirme butonu (D2)
const int driverPUL = 7;      // PUL pini (Step sinyali)
const int driverDIR = 6;      // DIR pini (Yön sinyali)
const int spd = A0;           // Potansiyometre pini (Analog giriş)

// Hareket Değişkenleri
int pd = 500;                // Pulse Delay süresi (Hız)
boolean setdir = LOW;        // Motorun yön durumu
int stepCount = 0;           // Adım sayısını başlat

// Potansiyometre Aralığı (0-1023 → 30°-90° dönüşümü)
const int minPotValue = 0;    // Potansiyometre min değeri
const int maxPotValue = 1023; // Potansiyometre max değeri
const int minAngle = 30;      // Minimum açı
const int maxAngle = 90;      // Maksimum açı

// Önceki potansiyometre değeri (değişimi takip etmek için)
int previousPotValue = 0;

void revmotor() {
  setdir = !setdir;  // Yönü tersine çevir
}

void setup() {
  pinMode(driverPUL, OUTPUT);
  pinMode(driverDIR, OUTPUT);
  pinMode(reverseSwitch, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);

  // LCD Başlat
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Aci: ");
  
  // İlk potansiyometre değerini al
  previousPotValue = analogRead(spd);
}

void loop() {
  // Potansiyometre değerini oku
  int currentPotValue = analogRead(spd);

  // Potansiyometre değerini 30° - 90° arasına eşitle
  int angle = map(currentPotValue, minPotValue, maxPotValue, minAngle, maxAngle);

  // Adım sayısını 100 ile eşleştir (100 adım için 30°-90° arası)
  int steps = map(angle, minAngle, maxAngle, 0, 100);  // 30° - 90° arasındaki açıyı 100 adım olarak map et

  // LCD’ye açıyı yazdır (her değişimde)
  lcd.setCursor(5, 0);
  lcd.print("     "); // Önce boşluk bırak, eski değer silinsin
  lcd.setCursor(5, 0);
  lcd.print(angle);
  lcd.print((char)223); // Derece sembolü
  
  // Adım sayısını da ekranda yazdır
  lcd.setCursor(0, 1);
  lcd.print("Adim: ");
  lcd.print(steps);

  // Motoru ileri hareket ettir
  digitalWrite(driverDIR, setdir);
  for (int i = 0; i < steps; i++) {
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
  }

  delay(500); // Kısa bekleme

  // Motoru geri hareket ettir
  digitalWrite(driverDIR, !setdir);
  for (int i = 0; i < steps; i++) {
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
  }

  delay(500); // Kısa bekleme
}

Data sheet of the motor driver I used

Use auto-format, then Please edit your code, and add CODE TAGS to make it easier to,read and copy in the post

1 Like
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// LCD Ayarları (0x27 adresi)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Pin Tanımlamaları
const int reverseSwitch = 2; // Yön değiştirme butonu (D2)
const int driverPUL = 7; // PUL pini (Step sinyali)
const int driverDIR = 6; // DIR pini (Yön sinyali)
const int spd = A0; // Potansiyometre pini (Analog giriş)

// Hareket Değişkenleri
int pd = 500; // Pulse Delay süresi (Hız)
boolean setdir = LOW; // Motorun yön durumu
int stepCount = 0; // Adım sayısını başlat

// Potansiyometre Aralığı (0-1023 → 30°-90° dönüşümü)
const int minPotValue = 0; // Potansiyometre min değeri
const int maxPotValue = 1023; // Potansiyometre max değeri
const int minAngle = 30; // Minimum açı
const int maxAngle = 90; // Maksimum açı

// Önceki potansiyometre değeri (değişimi takip etmek için)
int previousPotValue = 0;

void revmotor() {
setdir = !setdir; // Yönü tersine çevir
}

void setup() {
pinMode(driverPUL, OUTPUT);
pinMode(driverDIR, OUTPUT);
pinMode(reverseSwitch, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);

// LCD Başlat
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Aci: ");

// İlk potansiyometre değerini al
previousPotValue = analogRead(spd);
}

void loop() {
// Potansiyometre değerini oku
int currentPotValue = analogRead(spd);

// Potansiyometre değerini 30° - 90° arasına eşitle
int angle = map(currentPotValue, minPotValue, maxPotValue, minAngle, maxAngle);

// Adım sayısını 100 ile eşleştir (100 adım için 30°-90° arası)
int steps = map(angle, minAngle, maxAngle, 0, 100); // 30° - 90° arasındaki açıyı 100 adım olarak map et

// LCD’ye açıyı yazdır (her değişimde)
lcd.setCursor(5, 0);
lcd.print(" "); // Önce boşluk bırak, eski değer silinsin
lcd.setCursor(5, 0);
lcd.print(angle);
lcd.print((char)223); // Derece sembolü

// Adım sayısını da ekranda yazdır
lcd.setCursor(0, 1);
lcd.print("Adim: ");
lcd.print(steps);

// Motoru ileri hareket ettir
digitalWrite(driverDIR, setdir);
for (int i = 0; i < steps; i++) {
digitalWrite(driverPUL, LOW);
delayMicroseconds(pd);
digitalWrite(driverPUL, HIGH);
delayMicroseconds(pd);
}

delay(500); // Kısa bekleme

// Motoru geri hareket ettir
digitalWrite(driverDIR, !setdir);
for (int i = 0; i < steps; i++) {
digitalWrite(driverPUL, LOW);
delayMicroseconds(pd);
digitalWrite(driverPUL, HIGH);
delayMicroseconds(pd);
}

delay(500); // Kısa bekleme

Add another pot to A1

Use it's value the change speed

  // Motoru ileri hareket ettir
  digitalWrite(driverDIR, setdir);
  for (int i = 0; i < steps; i++) {
    digitalWrite(driverPUL, LOW);
    //****************************
    // Use the pot value to change this delay (speed)
    delayMicroseconds(speed);
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd); 
  }

1 Like

Are you giving the stepper motor time to stop turning in one direction before you turn in the opposite direction? What is connected to the armature of the stepper motor?

No, I want it to oscillate at a certain angle. I want to put a potentiometer instead of a button and adjust the speed.

To me oscillation means moving from one point to another and back. At the completion of one direction, the thing must stop before it begins moving in the opposite direction. Describe what you think oscillation at a certain angle is, as far as movements.

When I say specific angle, I wanted it to go up to 30 degrees and then do 30 degrees in the opposite direction.

Convert that to steps for the stepper you have chosen.

1 Like

If you want the potentiometer to control speed, then remove this code

// Potansiyometre değerini 30° - 90° arasına eşitle
int angle = map(currentPotValue, minPotValue, maxPotValue, minAngle, maxAngle);

// Adım sayısını 100 ile eşleştir (100 adım için 30°-90° arası)
int steps = map(angle, minAngle, maxAngle, 0, 100); // 30° - 90° arasındaki açıyı 100 adım olarak map et

and instead calculate an appropriate delay value pd used in this code:

digitalWrite(driverPUL, LOW);
delayMicroseconds(pd);
digitalWrite(driverPUL, HIGH);
delayMicroseconds(pd);
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.