0% found this document useful (0 votes)
20 views2 pages

Code

The code controls a DC motor using PWM signals on specified pins. It allows the motor to move forward and backward at maximum speed, stop, and gradually increase speed while moving forward. The setup includes pin configuration, PWM properties, and serial communication for testing purposes.

Uploaded by

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

Code

The code controls a DC motor using PWM signals on specified pins. It allows the motor to move forward and backward at maximum speed, stop, and gradually increase speed while moving forward. The setup includes pin configuration, PWM properties, and serial communication for testing purposes.

Uploaded by

flor3kab6pama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Code:

// Motor A
int motor1Pin1 = 14;
int motor1Pin2 = 15;
int enable1Pin = 13;

// Setting PWM properties


const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

void setup() {
// sets the pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);

// configure LEDC PWM


ledcAttachChannel(enable1Pin, freq, resolution,
pwmChannel);

Serial.begin(115200);

// testing
Serial.print("Testing DC Motor...");
}

void loop() {
// Move the DC motor forward at maximum speed
Serial.println("Moving Forward");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
delay(2000);

// Stop the DC motor


Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);

// Move DC motor backwards at maximum speed


Serial.println("Moving Backwards");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
delay(2000);

// Stop the DC motor


Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);

// Move DC motor forward with increasing speed


digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
while (dutyCycle <= 255){
ledcWrite(enable1Pin, dutyCycle);
Serial.print("Forward with duty cycle: ");
Serial.println(dutyCycle);
dutyCycle = dutyCycle + 5;
delay(500);
}
dutyCycle = 200;
}

You might also like