DC motor control (speed but not direction) with an Arduino and Processing




You will need:
Arduino
A small DC motor
TIP120 transistor
1N4004 rectifier diode (or something similar)
1K resistor (brown black red)
power supply for the DC motor
50K pot

This circuit allows us to control the speed of a DC motor with a potentiometer. Weeeee!

The TIP 120 is a power transistor. The power flows from the Collector to the Emitter and is controlled by the input coming in from the base. The base pin is connected to a digital out pin on the Arduino (via a 1k resistor). The wiring diagram make it look like the base is in the middle, but it's not - and if you do it wrong it flat out won't work at all (at best) so pay attention.

The other new component is a diode. This diode is here to prevent the motor from sending a charge back through the circuit. It acts, if you will, as a valve. This particular diode is called a Rectifier diode, and like LEDs have a correct orientation and an incorrect one. Notice that the motor has it's own power supply, but the motor and the arduino have a "common" ground.

You can't run house voltage though this circuit! The TIP120 and diode are not equipped for that, but it can handle small DC motors. You use the same circuit with a low power light bulb for the motor, in fact if you do you won't even need the diode.

To test this circuit, first make sure the arduino has the standard_Firmata software loaded onto it.
Double check your circuit.
Then, in Processing run this code.


int potPin = 0; // Analog in 0 connected to the potentiometer
int transistorPin = 9; // connected to the base of the transistor
int potValue = 0; // value returned from the potentiometer
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

void setup() {

arduino = new Arduino(this, Arduino.list()[0]);
arduino.pinMode(transistorPin, Arduino.OUTPUT); // set the transistor pin as output:
}

void draw() {

potValue = arduino.analogRead(potPin) /4;
println (potValue);
arduino.analogWrite(9, potValue);
}
You want to keep your eyes on this one, and your nose too. If you smell something funny, unplug the motor power and stop the program.
If you want to change the direction of a motor you'll need an H bridge.