Bird
0
0
Arduinoprogramming~30 mins

Motor speed control with PWM in Arduino - Mini Project: Build & Apply

Choose your learning style9 modes available
Motor speed control with PWM
📖 Scenario: You have a small DC motor connected to an Arduino. You want to control its speed smoothly using PWM (Pulse Width Modulation). This is like adjusting the brightness of a lamp by changing how long it stays on and off quickly.
🎯 Goal: Build a simple Arduino program that sets up the motor pin, defines a speed value, uses PWM to control the motor speed, and prints the speed to the Serial Monitor.
📋 What You'll Learn
Use pin 9 for the motor control output
Create a variable called motorPin and set it to 9
Create a variable called speed and set it to 128 (half speed)
Use analogWrite() to send the speed value to the motor pin
Print the speed value to the Serial Monitor using Serial.println()
💡 Why This Matters
🌍 Real World
Controlling motor speed with PWM is common in robotics, fans, and toys where smooth speed control is needed.
💼 Career
Understanding PWM and motor control is important for embedded systems engineers and anyone working with hardware and electronics.
Progress0 / 4 steps
1
Set up the motor pin
Create an integer variable called motorPin and set it to 9. Then, in the setup() function, set motorPin as an output using pinMode(). Also, start the serial communication at 9600 baud with Serial.begin(9600).
Arduino
Hint

Use pinMode(motorPin, OUTPUT); inside setup() to prepare the pin for output. Use Serial.begin(9600); to start serial communication.

2
Define the motor speed
Create an integer variable called speed and set it to 128 (which is about half speed). Add this variable above the setup() function.
Arduino
Hint

Declare int speed = 128; before setup() so it can be used anywhere in the program.

3
Control the motor speed with PWM
Inside the loop() function, use analogWrite() to send the speed value to motorPin. This will control the motor speed using PWM.
Arduino
Hint

Use analogWrite(motorPin, speed); inside loop() to control motor speed.

4
Print the motor speed to Serial Monitor
Still inside the loop() function, add a line to print the speed value to the Serial Monitor using Serial.println(speed);. This helps you see the speed value on your computer.
Arduino
Hint

Use Serial.println(speed); to print the speed value so you can see it on your computer screen.