Arduino Projects

Pulse Width Modulation (PWM) motor speed controller using 2 different voltage sources and a micro rotary potentiometer to adjust speed

The Arduino can do PWM at about 500 Hz, from 0 to 100% duty cycle on certain digital pin outputs, up to 40mA max.  

40mA is not enough to drive a motor, so a TIP120 transistor is used as a gate for the higher power source (a 9V battery).

I wanted to adjust the motor speed live, so I used a potentiometer to vary the analog voltage input on pin 3 from 0 to 5V.  The voltage in is communicated as a value from 0 to 1023, but PWM needs a value from 0 to 255, therefore we divide by 4 in the code.

Also, the DC hobby motor starts to run rough or intermittent when the PWM value is below 55, so I included the "if then else" statement, which works nice for having a real motor stop value also when the PWM is less than 55.

The cap and diode are safe guards against arcing from the motor brushes and reverse current from the motor spin down after drive current is stopped.

 

This is my schematic:

This is my working breaker board:

Oscilloscope data:

OFF

SLOWEST SPEED

ALMOST FULL SPEED

FULL SPEED

This is my code: