28byj-48 stepper motor and mpu 6050 accelerometer sensor

Hey there,
I'm making a project about lane-keep assets for cars using Arduino nano
I connected my 28byj-48 stepper motor with uln2003 driver to an mpu 6050 accelerometer

Anyway, my main issue is that whenever I compile the code, the motor won't move, or just move super slow that its like no enough power is delivered to the motor although the motor is connected to a 9v battery and the motor only needs 5v to run. however, the mpu sensor runs OK.

I also tried to common the ground but no hope.

How can I solve this issue?
Here's the code

#include <Stepper.h>
#include <Wire.h>
#include "Kalman.h" // Source: https://github.com/TKJElectronics/KalmanFilter

#define RESTRICT_PITCH // Comment out to restrict roll to ±90deg instead - please read: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf

Kalman kalmanX; // Create the Kalman instances
Kalman kalmanY;

/* IMU Data */
double accX, accY, accZ;
double gyroX, gyroY, gyroZ;
int16_t tempRaw;

double gyroXangle, gyroYangle; // Angle calculate using the gyro only
double compAngleX, compAngleY; // Calculated angle using a complementary filter
double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter

uint32_t timer;
uint8_t i2cData[14]; // Buffer for I2C data

Stepper stepper2(100, 8,9,10,11);
Stepper stepper1(100, 7,6,5,4);

int cantidad = 1;

int previousx = 0;
int previousy = 0;

void setup() {
 
  stepper2.setSpeed(200);
    stepper1.setSpeed(200);
  
  Serial.begin(115200);
  Wire.begin();
  TWBR = ((F_CPU /400000L) - 16) / 2; // Set I2C frequency to 400kHz

  i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
  while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode

  while (i2cRead(0x75, i2cData, 1));
  if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
    Serial.print(F("Error reading sensor"));
    while (1);
  }

  delay(100); // Wait for sensor to stabilize

  /* Set kalman and gyro starting angle */
  while (i2cRead(0x3B, i2cData, 6));
  accX = (i2cData[0] << 8) | i2cData[1];
  accY = (i2cData[2] << 8) | i2cData[3];
  accZ = (i2cData[4] << 8) | i2cData[5];

  // Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
  // atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
  // It is then converted from radians to degrees
#ifdef RESTRICT_PITCH // Eq. 25 and 26
  double roll  = atan2(accY, accZ) * RAD_TO_DEG;
  double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
#else // Eq. 28 and 29
  double roll  = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
  double pitch = atan2(-accX, accZ) * RAD_TO_DEG;
#endif

  kalmanX.setAngle(roll); // Set starting angle
  kalmanY.setAngle(pitch);
  gyroXangle = roll;
  gyroYangle = pitch;
  compAngleX = roll;
  compAngleY = pitch;

  timer = micros();
}

void loop() {
  /* Update all the values */
  while (i2cRead(0x3B, i2cData, 14));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);
  tempRaw = (i2cData[6] << 8) | i2cData[7];
  gyroX = (i2cData[8] << 8) | i2cData[9];
  gyroY = (i2cData[10] << 8) | i2cData[11];
  gyroZ = (i2cData[12] << 8) | i2cData[13];

  double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
  timer = micros();

  // Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
  // atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
  // It is then converted from radians to degrees
#ifdef RESTRICT_PITCH // Eq. 25 and 26
  double roll  = atan2(accY, accZ) * RAD_TO_DEG;
  double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
#else // Eq. 28 and 29
  double roll  = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
  double pitch = atan2(-accX, accZ) * RAD_TO_DEG;
#endif

  double gyroXrate = gyroX / 131.0; // Convert to deg/s
  double gyroYrate = gyroY / 131.0; // Convert to deg/s

#ifdef RESTRICT_PITCH
  // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
  if ((roll < -90 && kalAngleX > 90) || (roll > 90 && kalAngleX < -90)) {
    kalmanX.setAngle(roll);
    compAngleX = roll;
    kalAngleX = roll;
    gyroXangle = roll;
  } else
    kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter

  if (abs(kalAngleX) > 90)
    gyroYrate = -gyroYrate; // Invert rate, so it fits the restriced accelerometer reading
  kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);
#else
  // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
  if ((pitch < -90 && kalAngleY > 90) || (pitch > 90 && kalAngleY < -90)) {
    kalmanY.setAngle(pitch);
    compAngleY = pitch;
    kalAngleY = pitch;
    gyroYangle = pitch;
  } else
    kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt); // Calculate the angle using a Kalman filter

  if (abs(kalAngleY) > 90)
    gyroXrate = -gyroXrate; // Invert rate, so it fits the restriced accelerometer reading
  kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
#endif

  gyroXangle += gyroXrate * dt; // Calculate gyro angle without any filter
  gyroYangle += gyroYrate * dt;
  //gyroXangle += kalmanX.getRate() * dt; // Calculate gyro angle using the unbiased rate
  //gyroYangle += kalmanY.getRate() * dt;

  compAngleX = 0.93 * (compAngleX + gyroXrate * dt) + 0.07 * roll; // Calculate the angle using a Complimentary filter
  compAngleY = 0.93 * (compAngleY + gyroYrate * dt) + 0.07 * pitch;

  // Reset the gyro angle when it has drifted too much
  if (gyroXangle < -180 || gyroXangle > 180)
    gyroXangle = kalAngleX;
  if (gyroYangle < -180 || gyroYangle > 180)
    gyroYangle = kalAngleY;

  /* Print Data */
#if 0//et to 1 to activate
Serial.print(accX); Serial.print("\t");
Serial.print(accY); Serial.print("\t");
//Serial.print(accZ); Serial.print("\t");
//rial.print(gyroX); Serial.print("\t");
 //erial.print(gyroY); Serial.print("\t");
//Serial.print(gyroZ); Serial.print("\t");
  Serial.print("\t");
#endif

//Serial.print(roll); Serial.print("\t");
  //Serial.print(gyroXangle); Serial.print("\t");
  //Serial.print(compAngleX); Serial.print("\t");
  Serial.print(kalAngleX); Serial.print("\t");

  Serial.print("\t");

  //Serial.print(pitch); Serial.print("\t");
  //Serial.print(gyroYangle); Serial.print("\t");
  //Serial.print(compAngleY); Serial.print("\t");
  Serial.print(kalAngleY); Serial.print("\t");

#if 0 // Set to 1 to print the temperature
  Serial.print("\t");
  double temperature = (double)tempRaw / 340.0 + 36.53;
  Serial.print(temperature); Serial.print("\t");
#endif

  Serial.print("\r\n");
  //delay(2);

int valuex = kalAngleX;  
stepper2.step(valuex - previousx);
  previousx = valuex;
  
int valuey = kalAngleY;
stepper1.step(valuey - previousy);
  previousy = valuey;



}

I suggest you calculate the value that the stepper is being asked to move and print the value

stepper2.step(valuex - previousx);

...R

the problem is that when i connect everything to power on

the motor driver lights up, but after few moments, the lights of the drivers fades away and the motor stoped working

will these piece of code fix the issue?

worker_:
the motor driver lights up, but after few moments, the lights of the drivers fades away and the motor stoped working

I had not inferred that from your Original Post.

That new information strongly suggests that the motor power supply is inadequate. Perhaps you are using one of the PP3 style smoke-alarm batteries - they can't provide enough current for a motor. If you really need 9v then use a pack of 6 x AA alkaline cells. In my experience a 28BYJ-48 will run nicely from 3 x AA alkaline cells (4.5v).

...R

well, yea i AM using pp3 battery.
and i will try using the AA battery.

although i used a 12v 1A power supply and it didn’t work

worker_:
although i used a 12v 1A power supply and it didn’t work

You have not told us how the 12v power supply is connected. You should NOT be drawing power for the motor from your Aduino.

Did it work with the batteries?

...R

it improved the speed just a little bit
but not enough for me
i tried the batteries using multimeter and they are full

At this stage I suggest you write a short program that does nothing but make the motor move two turns in one direction, pause, and do two turns in the other direction.

See if the motor works at the desired speed.

...R

Try this simple test

#include <Stepper.h> //include the function library  <<<< Is this the standard Arduino library?
#define STEPS 64 // 64 steps per rev
Stepper stepper0(STEPS, 5, 3, 4, 2); //create the stepper0  <<< Order here is important

void setup()
{
  Serial.begin(9600); // initialize serial communication:
}
void loop()
{
  stepper0.setSpeed(120); //set speed to 10 rpm  <<< can this go in setup()?
  stepper0.step(640); //move 360 deg one direction
  delay(500); //pause for effect
  stepper0.step(-640); //move 360 deg in the other direction
  delay(500); //pause
}

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