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);
}
}
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);
}
}
}