0% found this document useful (0 votes)
11 views5 pages

Stepper Motor Control with ADC & Frequency

Uploaded by

Lokesh M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Stepper Motor Control with ADC & Frequency

Uploaded by

Lokesh M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Day 2 Exercise Programs:

1. Rotate Stepper motor based on the ADC input:


const int pwm1 = 8; // Coil 1 Side A

const int pwm2 = 9; // Coil 1 Side B

const int pwm3 = 10; // Coil 2 Side A

const int pwm4 = 11; // Coil 2 Side B

const int adcPin = A0; // ADC input

const float stepAngle = 1.8; // Degrees per step

const int maxAngle = 1000; // Max rotation angle

int currentStep = 0; // Keep track of the current step position

void setup() {

pinMode(pwm1, OUTPUT);

pinMode(pwm2, OUTPUT);

pinMode(pwm3, OUTPUT);

pinMode(pwm4, OUTPUT);

[Link](9600);

void stepMotor(int step) {

switch(step % 4) {

case 0:

analogWrite(pwm1, 255);

analogWrite(pwm2, 0);

analogWrite(pwm3, 255);

analogWrite(pwm4, 0);

break;

case 1:

analogWrite(pwm1, 0);

analogWrite(pwm2, 255);

analogWrite(pwm3, 255);

analogWrite(pwm4, 0);
break;

case 2:

analogWrite(pwm1, 0);

analogWrite(pwm2, 255);

analogWrite(pwm3, 0);

analogWrite(pwm4, 255);

break;

case 3:

analogWrite(pwm1, 255);

analogWrite(pwm2, 0);

analogWrite(pwm3, 0);

analogWrite(pwm4, 255);

break;

void moveToStep(int targetStep) {

while (currentStep != targetStep) {

if (currentStep < targetStep) {

currentStep++;

} else {

currentStep--;

stepMotor(currentStep);

delay(10); // Adjust speed

void loop() {

int adcValue = analogRead(A0);

// Map ADC value (0-1023) to angle (0-180)

int targetAngle = map(adcValue, 0, 1023, 0, maxAngle);

// Calculate target step


int targetStep = targetAngle / stepAngle;

moveToStep(targetStep);

delay(100); // Small delay to stabilize

2. Rotate Stepper motor based on the frequency by (configuring input capture PIN):
const int pwm1 = 4; // Coil 1 Side A
const int pwm2 = 9; // Coil 1 Side B
const int pwm3 = 10; // Coil 2 Side A
const int pwm4 = 11; // Coil 2 Side B
const int PWM_ot = 6; // Generrate PWM
int Input_Cpin = 8;
unsigned long duration = 0;

const int adcPin = A0; // ADC input


const float stepAngle = 1.8; // Degrees per step
const int maxAngle = 1000; // Max rotation angle
int var_duty_cyl=0;
volatile unsigned int lastCapture = 0;
volatile unsigned int currentCapture = 0;
volatile bool newCapture = false;
volatile int pulse_count=0;
float frequency = 0;
int currentStep = 0; // Keep track of the current step position
int speed = 0;
void setup() {
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(pwm3, OUTPUT);
pinMode(pwm4, OUTPUT);
pinMode(PWM_ot, OUTPUT);
pinMode(Input_Cpin, INPUT);

TCCR1A = 0;
TCCR1B = 0;

// Set timer with no prescaler (fastest resolution)


TCCR1B |= (1 << ICES1) | (1 << CS10); // Capture on rising edge, no prescaler

// Enable input capture interrupt


TIMSK1 |= (1 << ICIE1);
[Link](9600);
}

void stepMotor(int step) {


switch(step % 4) {
case 0:
analogWrite(pwm1, 255);
analogWrite(pwm2, 0);
analogWrite(pwm3, 255);
analogWrite(pwm4, 0);
break;
case 1:
analogWrite(pwm1, 0);
analogWrite(pwm2, 255);
analogWrite(pwm3, 255);
analogWrite(pwm4, 0);
break;
case 2:
analogWrite(pwm1, 0);
analogWrite(pwm2, 255);
analogWrite(pwm3, 0);
analogWrite(pwm4, 255);
break;
case 3:
analogWrite(pwm1, 255);
analogWrite(pwm2, 0);
analogWrite(pwm3, 0);
analogWrite(pwm4, 255);
break;
}
}

void moveToStep(int targetStep) {


while (currentStep != targetStep) {
if (currentStep < targetStep) {
currentStep++;
} else {
currentStep--;
}
stepMotor(currentStep);
delay(10); // Adjust speed
}
}

void loop() {

var_duty_cyl=var_duty_cyl>0XFF?0:var_duty_cyl+1;
analogWrite(PWM_ot, var_duty_cyl);

if (newCapture) {
noInterrupts();
unsigned int period = currentCapture - lastCapture;
newCapture = false;
interrupts();

// Assuming 16 MHz clock with no prescaler


frequency = 16000000.0 / period;

[Link]("pulse_count: ");
[Link](pulse_count);
//[Link](" Hz");
if(pulse_count>5000) // consider as 1KM/h
{
pulse_count=0;
speed = speed+1;
}
}
speed*=speed; // for stepper angle change

int targetAngle = map(speed, 0, 1023, 0, maxAngle);

// Calculate target step


int targetStep = targetAngle / stepAngle;

moveToStep(targetStep);
}
// Input Capture Interrupt
ISR(TIMER1_CAPT_vect) {
lastCapture = currentCapture;
currentCapture = ICR1;
pulse_count++;
newCapture = true;
}

You might also like