From Processing to Arduino Stepper motor 28BYJ-48 with ULN2003

Hi everyone,
I need some help to figuring out , why I can’t send data for changing the Speed, from Processing to my stepper motor via Arduino.

It’ should be very easy …

I saw in the forum other people doing things with steppers

but, I can’t make it works correctly!

The idea is super simple, I just need a slider using ControlP5 to change the speed of a stepper motor.

Processing

import processing.serial.*;
import controlP5.*;
ControlP5 cp5;
Serial port;


int myColor = color(0, 0, 0);
int sliderValue = 100;



void setup() {
  size(500, 300);
  // printArray(Serial.list());  
  //  "COM5"
  port = new Serial(this, Serial.list()[1], 9600);

  cp5 = new ControlP5(this);
  ;
  // add a horizontal sliders, the value of this slider will be linked
  // to variable 'sliderValue' 
  cp5.addSlider("sliderValue")
    .setPosition(100, 50)
    .setRange(20, 200)
    ;
}



void draw() {
  port.write(sliderValue);

  fill(sliderValue);
  rect(0, 0, width, 100);
}


Arduino:


#include <AccelStepper.h>
int s =10;
int newS;
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{  
   Serial.begin(9600);               // initialise the serial monitor
//Serial.setTimeout(10); // rallenta

  stepper.setMaxSpeed(1000);
  stepper.setSpeed(s);  

}


void loop()
{  
  if(Serial.available() > 0){
   newS = Serial.parseInt();
    stepper.setSpeed(newS);
  }

 stepper.runSpeed();
 }

Could someone pointing me out what I have to change in my code please?

  1. First I run Arduino and the stepper starts spinning
  2. I run the Processing Sketch, and the arduino stops

So, arduino is receiving a wrong data type? Too many?

Thanks in advance for your help,
best

Have you seen this reference:https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/

The following source code was taken from that reference and works on my system. I wired the stepper to an Arduino Uno as shown in the reference and the stepper moves as instructed. Once you get that to work then we can try to get the stepper connected to your Processing control.

// Include the AccelStepper Library
#include <AccelStepper.h>

// Define step constant
#define FULLSTEP 4

// Creates an instance
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);

void setup() {
  // set the maximum speed, acceleration factor,
  // initial speed and the target position
  myStepper.setMaxSpeed(1000.0);
  myStepper.setAcceleration(50.0);
  myStepper.setSpeed(200);
  myStepper.moveTo(2038);
}

void loop() {
  // Change direction once the motor reaches target position
  if (myStepper.distanceToGo() == 0) 
    myStepper.moveTo(-myStepper.currentPosition());

  // Move the motor one step
  myStepper.run();
}

1 Like

I have it working here with a modification of your Processing code, but according to Forum protocol I can’t post the solution if this is ‘homework’. Is it really homework for an educational course?

Hello,

  • First get the stepper motor working independent of Processing.
  • Consider how to send the same control values from Processing to Arduino.
  • Give consideration to timing and each loop on Processing and Arduino side.
  • Get the serial communication working without the hardware.
  • Integrate the serial with the hardware.
  • Troubleshoot.

There are many ways to monitor the serial data you are sending; I provided a simple example that does not require additional hardware and only a few extra lines of code.

This will help you monitor the data you are sending and echo it back:

Processing Code < Click here to open!
import processing.serial.*;
import controlP5.*;
ControlP5 cp5;
Serial port;

int myColor = color(0, 0, 0);
int sliderValue = 100;

int val, counter;

void setup() {
  size(500, 300);
  printArray(Serial.list());  
  //  "COM5"
  port = new Serial(this, Serial.list()[4], 9600);
  delay(1000); // Give the Arduino a chance to reboot before sending data

  cp5 = new ControlP5(this);
  ;
  // add a horizontal sliders, the value of this slider will be linked
  // to variable 'sliderValue' 
  cp5.addSlider("sliderValue")
    .setPosition(100, 50)
    .setRange(20, 200)
    ;
  }

void draw() 
  {
  if (frameCount%60 == 0) // Once a second
    {
    port.write(sliderValue);
    println("Tx: ", sliderValue);
    }
  
  if(port.available() > 0)
    {
    val = port.read();
    println("Count: ", counter++);
    println("Rx: ", val);
    }
  
  fill(sliderValue);
  rect(0, 0, width, 100);
  }
Arduino Code < Click here to open!
void setup() 
  {
  Serial.begin(9600);
  }

void loop() 
  {
  if (Serial.available()) 
    {
    int val = Serial.read();         
    Serial.write(val);             // Echo back to Processing 
    }
  }

References:
https://processing.org/tutorials/electronics
https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all

Once you grasp the serial communications between Processing and Arduino you can control anything.

Enjoy the journey!

:)

2 Likes

There are many ways to help the community arrive at a solution… homework or otherwise.

:)

I think we can agree that there is more than one way to teach. Personally I learned how to write code by studying and writing demos, not by reading a book. If the poster is able to run the Arduino code that I posted we will know that there is not a hardware problem and that everything is wired correctly. The Processing code that was posted is not far off and can be made to work with a few additions/corrections. But first we need to have a little feedback since it’s marked as homework.

1 Like

Nice example. I missed the code the first time I read your post; didn’t occur to me to look under the arrows.

Sorry, my fault! I marked with the flag Homework without thinking too much… I’m just a passionate

Do you have it working, or do you still need help?

1 Like

First of all, thank you very much to all of you, for your time and help!

So It works but with a lot of random behaviours I’ll link a video…

import processing.serial.*;
import controlP5.*;
ControlP5 cp5;
Serial port;

int myColor = color(0, 0, 0);
int sliderValue = 100;

int val, counter;

void setup() {
  size(500, 300);
  printArray(Serial.list());  
  //  "COM5"
  port = new Serial(this, Serial.list()[1], 9600);
  delay(1000); // Give the Arduino a chance to reboot before sending data

  cp5 = new ControlP5(this);
  ;
  // add a horizontal sliders, the value of this slider will be linked
  // to variable 'sliderValue' 
  cp5.addSlider("sliderValue")
    .setPosition(100, 50)
    .setRange(1, 100)
    ;
  }

void draw() 
  {
    
 
    port.write(int(sliderValue));
    println("Tx: ", sliderValue);
    
  
  if(port.available() > 0)
    {
    val = port.read();
    println("Count: ", counter++);
    println("Rx: ", val);
    }
  
  fill(sliderValue);
  rect(0, 0, width, 100);
  }

original example


/*
 Stepper Motor Control - speed control

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.
 A potentiometer is connected to analog input 0.

 The motor will rotate in a clockwise direction. The higher the potentiometer value,
 the faster the motor speed. Because setSpeed() sets the delay between steps,
 you may notice the motor is less responsive to changes in the sensor value at
 low speeds.

 Created 30 Nov. 2009
 Modified 28 Oct 2010
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;  // number of steps the motor has taken

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}

I added the serial code:

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor
Stepper myStepper(stepsPerRevolution, 2, 3, 4, 5);

int stepCount = 0;  // number of steps the motor has taken

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

void loop() 
  {
  if (Serial.available()) 
    {
    int val = Serial.read(); 
        Serial.write(val);             

  // set the motor speed:
  if (val > 0) {
    myStepper.setSpeed(val);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution /100 );
  }           // Echo back to Processing 
    }
  }

Your video was very helpful and shows that it is still not working correctly. I’m going to post the code that works on my system and then perhaps we can debug your code to fix it on your end.

Processing Code:

import processing.serial.*;
import controlP5.*;

ControlP5 cp5;
Serial port;

int myColor = color(0, 0, 0);
int sliderValue = 100;

void setup() {
  size(500, 300);
  printArray(Serial.list());  
  port = new Serial(this, Serial.list()[2], 9600);

  cp5 = new ControlP5(this);
  // value of slider will be linked to variable 'sliderValue' 
  cp5.addSlider("sliderValue")
    .setPosition(100, 50)
    .setRange(20, 200)
    ;
}

void draw() {
  port.write(sliderValue);
  fill(sliderValue);
  rect(0, 0, width, 100);
  
  if(port.available()>0){
    int val = port.read();
    println("Rx: ", val);
    }
}

Arduino Code:

#include <AccelStepper.h>

int s =10;
int inByte;

#define FULLSTEP 4

// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);

void setup() {  
  Serial.begin(9600);
  myStepper.setMaxSpeed(1000);
  myStepper.setSpeed(s);  
}

void loop() {  
  if(Serial.available() > 0){
    inByte = Serial.read();
    myStepper.setSpeed(inByte);
    Serial.write(inByte); // Send back to Processing for verification
  }
  myStepper.runSpeed();
 }

I wired the stepper straight to the Arduino using pins 8,9,10,11 and the other lead to 5v. Note that I used the AccelStepper library and used a lower number for the first parameter. Please let us know what happens.

1 Like

@svan :partying_face: :hearts:
it works like a charm

That’s what we want. Good luck with your project.

Thank you very much indeed!