// DC Motor Speed Control using PWM and Potentiometer
// Components: Arduino UNO, L298N Motor Driver, DC Motor, Potentiometer

int potPin = A0;       // Potentiometer connected to analog pin A0
int enA = 9;           // Enable pin (PWM) connected to D9 on Arduino
int in1 = 8;           // Input pin 1 of L298N
int in2 = 7;           // Input pin 2 of L298N

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  
  // Initialize motor direction
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
}

void loop() {
  int potValue = analogRead(potPin);       // Read potentiometer value (0–1023)
  int motorSpeed = map(potValue, 0, 1023, 0, 255); // Map to PWM range (0–255)

  analogWrite(enA, motorSpeed);  // Control motor speed with PWM
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: