First, thank you for taking the time to read my post.

I am attempting to use an ultrasonic sensor to control a DC motor using the PWM output of an arduino. Fairly simple, I know, but I'm just a beginner. :)

Here's my code, with an explanation of my theory and my error afterwards.

#define trigPin 7
#define echoPin 2
#define motorPin 9

void setup(){
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(motorPin, OUTPUT);

void loop(){
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); 
  distance = (duration/2) /29.1; 
  if (distance <5)
    analogWrite(motorPin, 0);
  else if(5 <= distance <15)
    analogWrite(motorPin, 50);
  else if(15 <= distance <25)
    analogWrite(motorPin, 100);
  else if(25 <= distance < 35)
    analogWrite(motorPin, 150);
  else if(35 <= distance < 45)
    analogWrite(motorPin, 200);
  else if(45 <= distance)
    analogWrite(motorPin, 255);
    analogWrite(motorPin, 0);
  Serial.print (distance);
  Serial.println (" cm");

So, this is incredibly simple. I haven't yet begun differential drive nor braking, and soon I want to learn to change out the ultrasonic sensor programming with the NewPing library for more sensors and less delay.

Right now, I am hoping to have the arduino interpret the ultrasonic ping as a distance in centimeters and based of that distance set the DC motor to a defined speed. The PWM output goes to a 210 Ohm resistor connected to the base pin of a transistor, on the collector/emitter is the 18V circuit connected to the DC motor. On a separate circuit is the arduino and ultrasonic sensor.

The circuit successfully gives me accurate distances in the serial monitor window. However, the motor only goes at 100% speed. It does not throttle its speed regardless of the distance variable's value.

It startles me that the default motor speed for this setup is 100%, it makes me think the speed is acting digitally. I tried deleting the final elseif conditional for the distance being greater than 45 cm set speed at 100% and the circuit still powered the motor at 100%.

Any help would be greatly appreciated!

  • \$\begingroup\$ It would be nice to see a schematic too. and the component datasheets/part numbers. Put the robot up on a block so it's wheels do not touch the ground, and then just test by moving something in front of the sensor and checking the response of the motor speed. If it's not changing at all, then it's probably a hardware problem, or a programming issue. The code seems okay, but I think you should make your control loop much shorter, by changing the "delay(500)" at the end to more like delay(100). That's just for performance, the analogWrite value not working is strange. \$\endgroup\$
    – KyranF
    Commented Apr 12, 2015 at 3:31
  • \$\begingroup\$ On line 43 , you don't need the ";" after the else. \$\endgroup\$ Commented Apr 12, 2015 at 5:47
  • \$\begingroup\$ According to your code, motor should never reach 100%, since there is logical mistake in line 39. Distance less than 45cm is already covered in above conditions, So the control will never reach line 41 to make motor operate at 100%. Unless the distance stays exactly 45cm. \$\endgroup\$ Commented Apr 12, 2015 at 6:00

1 Answer 1


You may have discovered not all pins can do PWM, and the Arduino "IDE" doesn't tell you. AnalogWrite a value larger than 0 and it's 100% on.

Write a test sketch that steps the pin through 25%, 50%, 100% and check that it works at all.

Then, try to get some equations going for the motor control; . Have three regions, selected by an If statement:

  • distance > 100 : full speed
  • distance < 20 : stop
  • 20 < distance < 100 : speed=3*(distance-20)

edit Not So Undocumented... the AnalogWrite page says

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

edit2 seems pin 9 should work on almost any Arduino. Perhaps your problem is in the transistor circuit.

  • \$\begingroup\$ But hes using Pin 9 and it supports analogwrite. \$\endgroup\$ Commented Apr 12, 2015 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.