Transistor

The pins on your Arduino can only be used to power devices that require a small amount of current (e.g. an LED). To power bigger loads (such as larger DC motors) we need to use an additional power supply other than your USB cable. For this exercise we will use the small DC motor that you were given on week 2. This motor can happily run off the Arduino power supply - but many motors cannot because they either draw too much current or they need a voltage higher than 5 volts. We will use this motor to learn what is needed for more powerful motors.

Inside a DC motor is a coil mounted on a shaft between two magnets. As current is applied, the coil is attracted to one magnet and repelled by the other resulting in a spinning motion. However, this also works in reverse - when you move a coil in a magnetic field it can induce current in the coil. This is called 'back' or 'kickback' voltage and it can constantly reset or even damage your Arduino. You can prevent this by putting a 'snubber' or 'kickback' diode in your circuit any time you are using an inductive load (DC motor, solenoid, etc.)

Here is everything you need to build a circuit that will protect your Arduino when using an inductive load. All these parts are from your Arduino Starter Kit or were in the paper cup of additional parts you were given week 2.


You will also need another power source (such as a switchable bench-top power supply, as pictured) that is appropriate for the load you want to drive.

A transistor is a semiconductor device commonly used to amplify or switch electronic signals. A transistor is made of a solid piece of a semiconductor material, with at least three terminals for connection to an external circuit. A voltage or current applied to one pair of the transistor's terminals changes the current flowing through another pair of terminals. Because the controlled (output) power can be much more than the controlling (input) power, the transistor provides amplification of a signal. The TIP 120  transistor (datasheet attached at the foot of this page) is a 3 pin device that acts like a 'switch'. The base controls the current passing through the collector and emitter. In this exercise we will use the power (+5V) from pin 9 on the Arduino to control the flow of current to a DC motor from an additional power supply. The base is the input that you will use to open and close the 'switch' across the collector and emitter. On this type of transistor you need to make sure the collector is always more positive than the emitter (the TIP 120 is an NPN transistor - for a detailed discussion of how this works see Chapter 3. Semiconductors in MIMS, Forrest M. 2007(4th Edition). Getting Started in Electronics. Lincolnwood, IL: Master Publishing, Inc.) Generally this is done by connecting the emitter to ground.

Once a motor starts turning its inertia keeps it spinning and it can generate 'back voltage'. A kickback diode acts like a one way valve and routes that voltage back into the motor so it can’t damage the rest of the circuit. Diodes are like LED’s in that they have polarity and will only allow electricity to flow in one direction. We will be using a 1N4004 Micro 1 Amp Rectifier Diode (datasheet attached at the foot of this page). This diode can handle currents up to 1 amp. Electricity will flow toward the silver band on one end of the diode.

Plug the 3 pins of the TIP 120 into 3 separate rows of the breadboard. In the orientation shown in the photo (with the printed side toward you) the order of the pins are from left to right: the base, the collector, and the emitter.

Connect the 1N4004 Micro 1 Amp Rectifier Diode in parallel with the collector and emitter of the transistor. Note that the silver band on the diode points away from the emitter that will be connected to ground. Looking ahead, the base of the TIP 120 will be connected to pin 9 on the Arduino with a 1K resistor in between. The high-current load (the DC motor) will be attached to the power supply, and then to the collector of the transistor. The emitter of the transistor will be connected to ground. Be sure to add the diode to your circuit correctly.

Put a 1K resistor in the breadboard connected to the base of the TIP 120.

Connect the emitter of the transistor to the ground rail of the breadboard.

Connect the 1K resistor to pin 9 on the Arduino. This connects the base of the TIP 120 to pin 9 with the resistor in-between.

Connect one pole of the DC motor to the the power rail of the breadboard (this will eventually be connected to the power supply) and the other pole to the collector of the transistor (one pin of the diode is also connected to this row).

Add a potentiometer from your Arduino starter kit to the breadboard.

Here is a reminder of the function of the pins on the potentiometer.

Connect the power pin of the potentiometer to +5v on your Arduino - or to the power rail of the breadboard (as in the photo) since this particular motor runs on +5v.

Connect the signal pin of the potentiometer to analog pin 0 on your Arduino.

Connect the ground pin of the potentiometer to the ground rail of the breadboard.

Connect the ground and power leads of your power supply to the ground and power rails of the breadboard.

Connect the USB to your Arduino and upload the code below. Before turning on the power supply make sure all the knobs are at zero. Turn on the power supply and slowly increase the voltage* to +5v. Control the speed of the motor with the potentiometer.

*Amps and Volts measure totally different things. We measure the rate of flow of electricity as an electric current measured in Amperes, shortened to Amps. We call electrical pressure Voltage and measure it in Volts. If electricity was a water hose Volts = pressure and Amps = volume.

/*
 * TIP120 for Smartsurfaces
 * Sets the speed of a DC motor connected to digital  
 * pin 9. The motor will spin at a rate specified by a 
 * potentiometer on analog pin 0.
 * Based on code from http://luckylarry.co.uk/
 */
 
int potPin = 0; // analog pin 0 connected to the potentiometer
int transistorPin = 9; // connected from digital pin 9 to the base of the transistor
int potValue = 0; // value returned from the potentiometer
 
void setup() { // set the transistor pin as an output
  pinMode(transistorPin, OUTPUT);
}
 
void loop() { // read the potentiometer, convert it to between 0 - 255 for the value accepted by the digital pin.
  potValue = analogRead(potPin) / 4; // potValue alters the supply from pin 9 which in turn controls the power running through the transistor
  analogWrite(9, potValue);
}

Let's change this set up so that the speed of the motor is controlled by numerical input in the Arduino serial monitor. Turn off the power supply. Please be careful. Power supplies can hold a significant charge even after they have been switched off.

Disconnect your USB cable from the Arduino.

Remove the potentiometer from the circuit. Also remove the 3 wires that connected to the potentiometer. Leave everything else in place.

Connect the ground rail of the breadboard to gnd on your Arduino. The ground connection must be shared between the Arduino and the additional power supply.

Reconnect the USB cable to your Arduino and upload the code below.
 
Before turning on the power supply make sure all the knobs are at zero. Turn on the power supply and slowly increase the voltage to +5v. Control the speed of the motor by typing 0-9 in the Arduino serial monitor.


/*
 * SerialMotorSpeed for Smartsurfaces
 * Sets the speed of a DC motor connected to digital  
 * pin 9. The motor will spin at a rate specified by a 
 * single-digit ASCII number read from the serial port.
 * Based on code copyleft 2006 Tod E. Kurt 
 * http://todbot.com/
 */

int motorPin = 9;
int val = 0; // variable to store the data from the serial port

void setup() {
  pinMode(motorPin,OUTPUT); // declare the motor's pin as output
  Serial.begin(9600); // connect to the serial port
  Serial.println("Welcome to SerialMotorSpeed!");
  Serial.println("Enter speed number 0-9:");
}

void loop () {
  val = Serial.read(); // read the serial port
  if (val >= '0' && val <= '9' ) {
    val = val - '0'; // convert from character to number
    val = 28 * val; // convert from 0-9 to 0-255 (almost)
    Serial.print("Setting speed to ");
    Serial.println(val);
    analogWrite(motorPin,val);
    
    Serial.println("Enter speed number 0-9:");
  }
}


Ċ
1N4004.pdf
(171k)
John Marshall,
Oct 7, 2009, 9:34 PM
Ċ
John Marshall,
Oct 7, 2009, 9:33 PM
Comments