Page 1 of 1

Arduino Code works, but same semantics of ESP-IDF doesn't

Posted: Fri Dec 20, 2024 6:59 am
by AbdusSamadTariq
Below is my arduino code, works perfectly fine with the ESP32 connected to the 28BYJ-48 Stepper Motor with ULN2003 Driver.

Code: Untitled.cs Select all



#include<Arduino.h>

// Define the pins connected to the ULN2003 driver
#define COIL1 25 // Blue wire
#define COIL2 26 // Pink wire
#define COIL3 27 // Yellow wire
#define COIL4 33 // Orange wire

// Define the delay time between steps (in milliseconds)
int stepDelay = 2; // Smaller delay = faster speed

void setup() {
// Set all coil pins as output
pinMode(COIL1, OUTPUT);
pinMode(COIL2, OUTPUT);
pinMode(COIL3, OUTPUT);
pinMode(COIL4, OUTPUT);
}

void loop() {
// Rotate clockwise one full revolution
for (int i = 0; i < 2048; i++) { // 2048 steps for one revolution (28BYJ-48)
stepMotor(1);
delay(stepDelay);
}
delay(1000); // Wait for 1 second

// Rotate counterclockwise one full revolution
for (int i = 0; i < 2048; i++) {
stepMotor(-1);
delay(stepDelay);
}
delay(1000); // Wait for 1 second
}

// Function to move the motor one step
void stepMotor(int direction) {
if (direction == 1) { // Clockwise
digitalWrite(COIL1, HIGH); digitalWrite(COIL2, LOW); digitalWrite(COIL3, LOW); digitalWrite(COIL4, LOW);
delay(stepDelay);
digitalWrite(COIL1, LOW); digitalWrite(COIL2, HIGH); digitalWrite(COIL3, LOW); digitalWrite(COIL4, LOW);
delay(stepDelay);
digitalWrite(COIL1, LOW); digitalWrite(COIL2, LOW); digitalWrite(COIL3, HIGH); digitalWrite(COIL4, LOW);
delay(stepDelay);
digitalWrite(COIL1, LOW); digitalWrite(COIL2, LOW); digitalWrite(COIL3, LOW); digitalWrite(COIL4, HIGH);
delay(stepDelay);
} else if (direction == -1) { // Counterclockwise
digitalWrite(COIL1, LOW); digitalWrite(COIL2, LOW); digitalWrite(COIL3, LOW); digitalWrite(COIL4, HIGH);
delay(stepDelay);
digitalWrite(COIL1, LOW); digitalWrite(COIL2, LOW); digitalWrite(COIL3, HIGH); digitalWrite(COIL4, LOW);
delay(stepDelay);
digitalWrite(COIL1, LOW); digitalWrite(COIL2, HIGH); digitalWrite(COIL3, LOW); digitalWrite(COIL4, LOW);
delay(stepDelay);
digitalWrite(COIL1, HIGH); digitalWrite(COIL2, LOW); digitalWrite(COIL3, LOW); digitalWrite(COIL4, LOW);
delay(stepDelay);
}
}

But for some odd reason, when I convert this to ESP-IDF code, as that's what I use normally, the stepper motor refuses to work. I think it has somtheing to do with the timing, as there are 4 LEDs on the driver board, and with the UNO code, they are all lit up (there is a very small delay), but with the ESP32 I can physically see the pattern moving withg my naked eye, meaning that there is probably a timing error in my ESP-IDF code.

Code: Untitled.c Select all



#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

// Define the pins connected to the ULN2003 driver
#define COIL1 GPIO_NUM_25 // Blue wire
#define COIL2 GPIO_NUM_26 // Pink wire
#define COIL3 GPIO_NUM_27 // Yellow wire
#define COIL4 GPIO_NUM_33 // Orange wire

// Define the delay time between steps (in milliseconds)
#define STEP_DELAY 2

// Define the sequence for a full step drive
const int full_step_sequence[4][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};

void stepMotor(int steps, int direction);

void app_main(void) {
// Configure all coil pins as output
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << COIL1) | (1ULL << COIL2) | (1ULL << COIL3) | (1ULL << COIL4),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&io_conf);

while (1) {
// Rotate clockwise one full revolution (2048 steps)
stepMotor(2048, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait for 1 second

// Rotate counterclockwise one full revolution (2048 steps)
stepMotor(2048, -1);
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait for 1 second
}
}

void stepMotor(int steps, int direction) {
for (int i = 0; i < steps; i++) {
for (int j = 0; j < 4; j++) {
int step_index = (direction == 1) ? j : (3 - j);
gpio_set_level(COIL1, full_step_sequence[step_index][0]);
gpio_set_level(COIL2, full_step_sequence[step_index][1]);
gpio_set_level(COIL3, full_step_sequence[step_index][2]);
gpio_set_level(COIL4, full_step_sequence[step_index][3]);
vTaskDelay(STEP_DELAY / portTICK_PERIOD_MS);
}
}
}


Re: Arduino Code works, but same semantics of ESP-IDF doesn't

Posted: Fri Dec 20, 2024 10:11 am
by AbdusSamadTariq
Hi, I found out the solution, the delays being used in the ESP32 code were unpredictable. I used the esp_rom_delay_us() function instead. :roll:

Re: Arduino Code works, but same semantics of ESP-IDF doesn't

Posted: Fri Dec 20, 2024 10:25 pm
by MicroController
Try

Code: Select all

#define STEP_DELAY 10
.