Controlling a DC motor with Ultrasonic sensor

Grumpy_Mike:
You can't drive a motor direct from an arduino pin you need a driver, like a transistor.

To control the speed feed the motor with a PWM signal, you get it from an analogWrite function, this only works with certain pins, see your arduino model for which.

For information on PWM see:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Its alright, the code is safe due to:

  pinMode(motorPin, INPUT);

:slight_smile: