Running a DC motor using SPI, bit operations, and arrays

Greetings!

I am currently a first year student in Electronic Maintenance. We've been learning Arduino for a while and we just started talking about Serial Peripheral Interface. We also made our own H-Bridge in order to run motors. I've been messing around and finding out how to make a DC run slowly accelerate through SPI and arrays, as I've already managed to do it through conventional methods, using an L2938N motor driver. I made a first array for the motor speed comprised of just pure bits (0-16). Then I made a second array that maps the first array and stores the speed as a float.

This is the code :

#include <SPI.h>

byte velo_motor[] = {
  B00000000,
  B00000001,
  B00000010,
  B00000011,
  B00000100,
  B00000101,
  B00000110,
  B00000111,
  B00001000,
  B00001001,
  B00001010,
  B00001011,
  B00001100,
  B00001101,
  B00001110,
  B00001111,
};

const int IN1 = 3;
const int IN2 = 4; 
const int ENA = 5;
const int ESPERA = 6;
const int SS_PIN = 10;


void setup() {
  Serial.begin(9600);
  SPI.begin();

  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ESPERA, OUTPUT);

}

void loop() {

  float velo_motor2[16]; // Second array which stores the speed as a float
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(ESPERA, HIGH);

  for (int i = 0; i < 16; i++) {
    velo_motor2[i] = map(velo_motor[i], 0, 255, 0, 4500);  // Map to range 0-255
    digitalWrite(ENA, map(velo_motor[i], 0, 255, 0, 4500));
    Serial.println(velo_motor2[i]); // Print each mapped value
    delay(800); // Wait for 400 milliseconds before printing the next value
  }
}

I hooked up the motor but it doesnt slowly accelerate despite the values being quite correct. Is there something I'm doing wrong? I am a complete beginner at this and am eager to learn, so any input would help. Thank you!

Welcome to the forum

        digitalWrite(ENA, map(velo_motor[i], 0, 255, 0, 4500));

digitalWrite() will write either HIGH or LOW to the ENA pin. Is that what the code is intended to do ?

1 Like

note also that map() won't give you a float, it will truncate to get an integral value.

Post a connection and wiring diagram of the project.

Hello,

I just edited the code and I instead of digitalWrite(), I made it analogWrite(), as I intended to slowly accelerate the motor through the ENA port (by writing 0-255 but with the bits I assigned to velo_motor[].

Oh ok then so declaring velo_motor2 as a float wont do anything regardless?

You can not simply control motor speed, it depends on many other parameters. For a smooth start you have to overcome sticking friction, then continue with lower energy, depending on the motor load. For real control you need rpm feedback and a PID controller.

You’ll get an int value in a float

Thank you for your reply, I was using an L2938N motor driver the first time, and it worked. The time I wanted to do the same but using SPI and converting bits from the motor array into integers

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