Esp32 Stepper motor problems

Hi, im doing a project where i'm using the data from a soil moisture to turn a stepper motor into 3 different positions. This is my first project and i'm kinda trash at coding. I coded made everything working on an Arduino Uno and now i need to transfer the code to an Esp32. I am well aware that the pins are different and i have changed them acordingly. But now my stepper motor wont work. I have tested if it was a power issue and it is now. so it have something to do with the pins and i have no clue why it is not working lmao.

here is the code, please ignore any weird parts of the code. I only need help with the stepper motor :smiley:

#include <Stepper.h>
#include <EEPROM.h>

const int stepsPerRevolution = 2048;

int location = 0;

/* Change these values based on your calibration values */
#define soilWet 600   // Define max value we consider soil 'wet'
#define soilDry 800
#define soilTooDry 900   // Define min value we consider soil 'dry'

// Sensor pins
//#define sensorPower 7
#define sensorPin 26

Stepper motor(stepsPerRevolution, 35, 33, 32, 25);

void setup() {
	//pinMode(sensorPower, OUTPUT);
  motor.setSpeed(5);
	// Initially keep the sensor OFF
//	digitalWrite(sensorPower, LOW);
	Serial.begin(9600);
  pinMode(14, OUTPUT);
  setupLocation(); // Initialize location from EEPROM
}

void loop() {

	//get the reading from the function below and print it
	int moisture = readSensor();
	Serial.print("Analog Output: ");
	Serial.println(moisture);

  

	if (moisture < soilWet) {
	Serial.println("Status: Soil is too wet");

  if (location != 0){
    Serial.end();
    if (location == 1){
      //digitalWrite(sensorPower, LOW);
      updateLocation(0);
      motor.step(-(stepsPerRevolution / 3));
    }
    if (location == 2){
          //digitalWrite(sensorPower, LOW);
          updateLocation(0);
          motor.step((stepsPerRevolution / 3));
    }
  Serial.begin(9600);
    }

    while (moisture < soilWet) {
      digitalWrite(14, HIGH);  // turn the LED on (HIGH is the voltage level)
      delay(1000);                      // wait for a second
      digitalWrite(14, LOW);   // turn the LED off by making the voltage LOW
      delay(1000); 
      int moisture = readSensor();
      if (moisture > soilWet){
        break;
        }
    }   


	} 
  
if (moisture > soilTooDry) {
	Serial.println("Status: Soil is very dry - time to water!");

  if (location != 0){
    Serial.end();
    if (location == 1){
      //digitalWrite(sensorPower, LOW);
      updateLocation(0);
      motor.step(-(stepsPerRevolution / 3));
    }
    if (location == 2){
         // digitalWrite(sensorPower, LOW);
          updateLocation(0);
          motor.step((stepsPerRevolution / 3));
    }
  Serial.begin(9600);
    }

    
	} 

  else if (moisture >= soilWet && moisture < soilDry) {
		Serial.println("Status: Soil moisture is perfect");
    
    
    if (location != 1){
    Serial.end();
    if (location == 0){
      //digitalWrite(sensorPower, LOW);
      updateLocation(1);
      motor.step(stepsPerRevolution / 3);
    }
    if (location == 2){
          //digitalWrite(sensorPower, LOW);
          updateLocation(1);
          motor.step(-(stepsPerRevolution / 3));
    }
    Serial.begin(9600);
    }
   
	} 
  
  else {
		Serial.println("Status: Soil is dry - time to water!");

    
 if (location != 2){
   Serial.end();
  if (location == 0){
      //digitalWrite(sensorPower, LOW);
      updateLocation(2);
      motor.step(-(stepsPerRevolution / 3));
  }
   if (location == 1){
     // digitalWrite(sensorPower, LOW);
      updateLocation(2);
      motor.step(stepsPerRevolution / 3);
   }
    Serial.begin(9600);

   }
    
	}
	
	delay(5000);
					// Normally you should take reading perhaps once or twice a day
	Serial.println();
}

//  This function returns the analog soil moisture measurement
int readSensor() {
	//digitalWrite(sensorPower, HIGH);	// Turn the sensor ON
	delay(10);							// Allow power to settle
	int val = analogRead(sensorPin);	// Read the analog value form sensor
	//digitalWrite(sensorPower, LOW);		// Turn the sensor OFF
	return val;							// Return analog moisture value
}

void updateLocation(int newLocation) {
  EEPROM.write(0, newLocation);
  location = newLocation;
}

void setupLocation() {
  int storedLocation = EEPROM.read(0);
  if (storedLocation >= 0 && storedLocation <= 2) {
    location = storedLocation;
  }
} 

The UNO is a 5V device, while the ESP32 is a 3.3V device. Maybe the output voltage of the ESP32 pins isn't enough for your stepper driver.
Please show a schematic and links to the used parts.

as said, i have already tested the power. I hooked up my stepper motor to an external power supply keeping my "control" pins in the esp32, this didnt work. I also switched it up, hooking my control pins to the code running on arduino uno keeping the power through the esp32 this did work, so its most likely not a power issue

I didn't talk about the power for your stepper, but the logic level of the output pins of the processor and the input pins of your driver. A circuit diagram showing the components used is still needed.

Sorry for misunderstanding, here is a diagram of my current setup. I hope it makes sense :smiley:

OK, the ULN2003 should work with an input voltage of >3V.
Is there really no resistance in series with your LED? You definitely need one.
But:

GPIO 35 is input only on ESP32 Board

Btw: Your coloring of the wires is very confusing. Usually black is Gnd, and red is Vcc/Vdd. You did it the other way round.

I've tried switching it around also using 16,17,5,18 pins. I've used these pins in another project where they worked, but not now. Thats why i tried to switch it to the ones im using now.

The main problem is that there isnt even light in the ULN, its as if its not turning on. I 've also tried using the AccelStepper library instead to make sure that it isnt something with the library. But it still doesnt work. Then i found this code to test the motor and when i uploaded this, it seemed to at least turn on the ULN as there was light in the diodes. It didnt move but that might be because of the 35 pin being input only.

#include <AccelStepper.h>

const int stepsPerRevolution = 2048;

AccelStepper motor(AccelStepper::FULL4WIRE, 35, 33, 32, 25);

void setup() {
  motor.setMaxSpeed(1000);
  motor.setSpeed(500);
}

void loop() {
  motor.runSpeed();
}

Hi,
Do you have a DMM? Digital MultiMeter.

Measure the 5V going into the ULN PCB.

Then read and use this example, note the external supply needed for the stepper.

The provider of this series of tutorials is very good and gives excellent discussion and theory of operation.

All I did was Google;

esp32 and uln2003

Tom... :smiley: :+1: :coffee: :australia:

Thanks for the input, but i cant really use this tutorial. I use all the same functions as the tutorial but mine is still not working in my setup. It is working when using a basic setup and disregarding all my other code.

Why????
Have you tried it?
At least try it and see if your hardware is okay?
Its all there for you, even the code.

Note the external power supply.
HINT.. HINT.. External Power Supply.

Tom.. :smiley: :+1: :coffee: :australia:

Since the code is working flawlessly on Arduino uno and i really cant find a solution i will make I2C between my uno and esp32

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