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

**URL:** <https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [September 16, 2022, 2:11pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804 "2022-09-16T14:11:33Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rand\_colors](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@rand\_colors](https://discourse.processing.org/u/rand_colors)\
**Post date:** [September 16, 2022, 2:11pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/1 "2022-09-16T14:11:33Z")

</div>

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

> [@Program slower when I send data to Arduino](https://discourse.processing.org/t/program-slower-when-i-send-data-to-arduino/19815):
>
> Hello, I make turning 5 motors with a potar with Arduino. It works perfectly, I make turning them with a simulation from Processing, it all turn but the simulation is slower than if I don’t send datas to Arduino. What did I do wrong? I send datas from Processing like this ( my program is lightened) import java.util.Arrays; import sync.\*; // MANAGE ARDUINO import processing.serial.\*; Serial arduinoport; void setup() { frameRate(80); //perspective setting size(1000, 800, P3D); …

> [@\[SOLVED\] Problems sending data from Processing to Arduino through serial port](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138):
>
> Hello everybody! I’m newbie in Processing. I’m writing here hoping that someone will be able to help me… Thanks in advance;) I’m trying to communicate two XBEE S1 modules between the computer and an Arduino Mega, in AT mode. The configuration of the XBEEs were done, and works well, I just need to program the communication from Processing to Arduino through the Serial Port. I had found a lot of examples, were the Arduino send data to Processing, but neither in the other direction (From Process…

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

```auto
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:

```auto

#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

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 16, 2022, 7:01pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/2 "2022-09-16T19:01:22Z")

</div>

Have you seen this reference:[https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/](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.

```auto
// 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();
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 16, 2022, 7:44pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/3 "2022-09-16T19:44:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 16, 2022, 10:25pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/4 "2022-09-16T22:25:39Z")

</div>

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!**
>
> ```auto
> 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!**
>
> ```auto
> 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://processing.org/tutorials/electronics)  
[https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/d/1ddfd70496897d5a1de9821834cb81afeb897459.png)

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

Enjoy the journey!

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 17, 2022, 12:56am UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/5 "2022-09-17T00:56:15Z")

</div>

> [@svan](#):
>
> 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?

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

> [@Guidelines—Answering Questions](https://discourse.processing.org/t/guidelines-answering-questions/2145):
>
> If you’re here, it means you’re thinking about contributing to the Processing community by answering questions on the forum. That’s great! This guide is meant to help you answer questions in a way that’s most helpful. Be nice. Remember that you’re talking to a person! Follow the guidelines in the [FAQ](https://discourse.processing.org/faq). Also read through the [P5.js community guidelines](https://p5js.org/community/). Specifically, avoid making judgments like “this code is bad” or “that’s a dumb way to do this”. Be mindful of how your tone comes across over the…

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 17, 2022, 2:49am UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/6 "2022-09-17T02:49:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 17, 2022, 3:01am UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/7 "2022-09-17T03:01:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rand\_colors](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@rand\_colors](https://discourse.processing.org/u/rand_colors)\
**Post date:** [September 19, 2022, 11:12am UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/8 "2022-09-19T11:12:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 19, 2022, 11:57am UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/9 "2022-09-19T11:57:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rand\_colors](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@rand\_colors](https://discourse.processing.org/u/rand_colors)\
**Post date:** [September 19, 2022, 12:16pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/10 "2022-09-19T12:16:09Z")

</div>

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…

```auto
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

```auto

/*
 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:

```auto
#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 
    }
  }

```

[![](https://img.youtube.com/vi/PlCJ7mpa6Bc/maxresdefault.jpg "processing stepper problems") ](https://www.youtube.com/watch?v=PlCJ7mpa6Bc)

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 19, 2022, 12:43pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/11 "2022-09-19T12:43:11Z")

</div>

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:**

```auto
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:**

```auto
#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.

---

<div class="post-metadata">

**Author:** ![rand\_colors](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@rand\_colors](https://discourse.processing.org/u/rand_colors)\
**Post date:** [September 19, 2022, 1:09pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/12 "2022-09-19T13:09:12Z")

</div>

@svan 🥳 ♥  
**it works like a charm**

[![](https://img.youtube.com/vi/0Ypka34ex7I/maxresdefault.jpg "from processing to arduino stepper motor 28byj 48 with uln2003") ](https://www.youtube.com/watch?v=0Ypka34ex7I)

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 19, 2022, 1:11pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/13 "2022-09-19T13:11:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rand\_colors](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@rand\_colors](https://discourse.processing.org/u/rand_colors)\
**Post date:** [September 19, 2022, 1:12pm UTC](https://discourse.processing.org/t/from-processing-to-arduino-stepper-motor-28byj-48-with-uln2003/38804/14 "2022-09-19T13:12:21Z")

</div>

Thank you very much indeed!
