Using a potentiometer to control motor speed activated by IR Sensor

Hello, I hope someone can help me.

I am trying to improve the sketch below by adding a potentiometer to control the motor speed but I am not sure how to do that. I have tried a few things without success. Can someone give me some tip or maybe point me in the right direction? The motor is activated by a IR Sensor at full speed. Due the nature of the project it would be better if I could fine tune the speed.

Here’s the actual sketch:

This is the Arduino code I am using:

#include <DistanceGP2Y0A21YK.h>

DistanceGP2Y0A21YK Dist;

int distance;
const int motorPin = 9;
//int distMeasure = 0;

void setup()
  pinMode(motorPin, OUTPUT);
  pinMode(distance, INPUT);

void loop()
  distance = Dist.getDistanceCentimeter();
  Serial.print("\nDistance in centimers: ");
//  delay(500); //make it readable

  if(distance <= 55) {
    digitalWrite(motorPin, HIGH);
  else {
    digitalWrite(motorPin, LOW);

Thank you in advance,

As it stands your sketch just turns the motor on or off.

If you use analogWrite() the Arduino will produce a pulse-width-modulated signal that will control the motor speed. You haven't said what sort of circuit you have between the motor pin and the motor so I have no idea whether it would be suitable for a PWM signal.

If you just want the motor to run at a pre-programmed speed rather than full speed you don't need a potentiometer.

If you want to be able to vary the speed by turning the potentiometer you need to connect its ends to 5v and gnd and the centre connection to one of the analog inputs. Then, in your program loop you would use analogRead to detect the setting of the pot and use the number to set the value for analogWrite(). By the way analogRead and analogWrite are not related to each other - rather confusing naming. AnalaogWrite works on the digital outputs.


Hi Robin,

Thanks for the reply. Please forgive my ignorance as I am totally new to Arduino and circuits, in other words I am learning as I go along. What do you mean by 'sort of circuit'? This sketch is based on an exercise from the Arduino Starter Kit and my own modifications based on a IR Sensor library. It is very possible that I have made unnecessary conections or even mistakes.

Regarding the PWM, do I need to connect the the motor to one of the PWM pins and call the code analogWrite() trough it?


You need to tell (or show a diagram of) how the motor is controlled by the Arduino - the stuff that's between the Arduino and the motor. The Arduino pins can't produce enough current to drive a motor so, whether using PWM or not, there must be something between the Arduino and the motor. For your on/off example a relay would work, but that wouldn't be suitable for PWM which would at least need one or more transistors or something similar. An electronic circuit called a h-bridge is commonly used.

I'm not familiar with the Arduino starter kit.


Hey Robin,

Sorry about that dude. For some reason the sketch image was not loading in the first post. You should be able to see it now.

Based on this setup, what do you think is the best solution?

Thanks, D

That looks ok, but you don't show the pinouts of your transistor.

I presume the other device is your IR detector.

If you add a potentiometer with its output pin connected to another analog pin then you would use analogRead(potentiometerPin) to get a number (0-1023 I think) that represents the voltage on the pot output pin and you would use that number to decide what speed to send to the transistor controlling the motor with analogWrite(transistorPin, speed) where speed is a number from 0-255.

With the transistor arrangement you have it would not be possible to make the motor run in reverse as well as forward.