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

Speed Controller

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

Speed Controller

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

DC motor Speed controller with arduino

To control the speed of a DC motor using an Arduino, you can use Pulse Width
Modulation (PWM). PWM allows you to adjust the average voltage supplied to the
motor by rapidly switching the power on and off. The duty cycle of this PWM signal
determines the effective voltage and thus the speed of the motor.

Here’s a basic example of how to control the speed of a DC motor using an Arduino.
This example assumes you are using an H-bridge motor driver (like the L298N or
L293D) to control the motor.

### Components Needed:


- Arduino board (e.g., Arduino Uno)
- DC Motor
- Motor Driver (e.g., L298N or L293D)
- Power supply for the motor
- Connecting wire

### Circuit Diagram:


1. **Motor Driver:**
- Connect the motor terminals to the output pins of the motor driver.
- Connect the input pins of the motor driver to the Arduino PWM pins.
- Connect the power supply to the motor driver’s power inputs.
- Connect the ground of the motor driver to the Arduino ground.

2. **Arduino Connections:**
- **PWM Pin (e.g., D9)** to one of the input pins of the motor driver.
- **DIR Pin (e.g., D8)** to the direction control pin of the motor driver.
- **GND** to the ground of the motor driver.

### Arduino Code Example:

```cpp
// Define pins for the motor driver
const int pwmPin = 9; // PWM control pin
const int dirPin = 8; // Direction control pin

void setup() {
// Set the motor control pins as outputs
pinMode(pwmPin, OUTPUT);
pinMode(dirPin, OUTPUT);

// Set initial motor direction (optional)


digitalWrite(dirPin, HIGH); // Set direction to forward
}

void loop() {
// Example to control motor speed from 0 to 255

// Gradually increase motor speed


for (int speed = 0; speed <= 255; speed++) {
analogWrite(pwmPin, speed); // Set the motor speed
delay(20); // Wait for 20 ms to see the change
}

// Gradually decrease motor speed


for (int speed = 255; speed >= 0; speed--) {
analogWrite(pwmPin, speed); // Set the motor speed
delay(20); // Wait for 20 ms to see the change
}

// Pause for a second before repeating


delay(1000);
}
```

### Explanation:

1. **`pwmPin` and `dirPin`**: These are the Arduino pins connected to the motor
driver. `pwmPin` controls the speed, and `dirPin` controls the direction.

2. **`pinMode()`**: Configures the pins as outputs.

3. **`digitalWrite(dirPin, HIGH);`**: Sets the direction of the motor. You can use
`HIGH` or `LOW` depending on your motor driver's wiring and the desired direction.

4. **`analogWrite(pwmPin, speed);`**: Writes a PWM value to `pwmPin`. The `speed`


value ranges from 0 (stopped) to 255 (full speed). This controls the motor speed.

5. **`delay()`**: Provides a delay to make the speed change visible and allows the
motor to adjust to the new speed.

### Notes:

- **Power Supply**: Ensure that the power supply for the motor is adequate for your
motor's voltage and current requirements. The Arduino itself should not be used to
power the motor directly.

- **Motor Driver**: Make sure to use a motor driver suitable for the current and
voltage rating of your motor.

- **Direction Control**: If you want to change the direction, you can set
`digitalWrite(dirPin, LOW);` or `digitalWrite(dirPin, HIGH);` depending on your
needs.

This example demonstrates a simple way to control motor speed with PWM. You can
adjust the code to suit your specific requirements, such as varying the speed based
on sensor input or other conditions.

You might also like