Lab: Using a Transistor to Control a High Current Load

In this tutorial, you’ll learn how to control a high-current load with a transistor.

Introduction

Transistors are often used as electronic switches, to control loads which require high voltage and current from a lower voltage and current. The most common example you’ll see of this in a physical computing class is to use an output pin of a microcontroller to turn on a motor or other high current device. The output pins of a microcontroller can only produce a small amount of current and voltage. But when coupled with a transistor, they can control much more.

What You’ll Need To Know

You should have read the notes on high current loads before doing this lab. In order to get the most out of this lab, you should know the basics of electronics, as well as how to use a solderless breadboard. It would help to do some reading on DC motors as well.

Microcontrollers aren’t the only integrated circuits that produce a low voltage and current on their output pins. There are many components that do this. You’ll see a whole range of so-called logic ICs that can’t produce very much current or voltage, but can produce a small change on their output pins that can be read as a data or control signal. The output voltage from devices is often referred to as a logic or a control voltage, as opposed to the supply or load voltage needed to control the high-current device. You can use transistors from circuits like these. For example, you might put a transistor on the output pin of a 555 timer IC (which produces a variable timing pulse), or a shift register IC (which allows you to produce multiple control signals in parallel) to control high current loads from those devices.

Things You’ll Need

Figures 1-12 are the parts you’ll need for this exercise.

Connect a 7805 5V voltage regulator to your board, and power it from a 9-12V DC power supply. Connect the ground rows on the sides together. Don’t connect the two red rows on the side of the breadboard to each other, though. Wire the breadboard so that the right side of the board receives the 5V output from the regulator, but the left side gets 9-12V directly from your DC power supply. The 5V line is the 5-volt bus or logic supply and the 9-12V line is the high-voltage bus or load supply. The two ground lines are ground. Figure 13 shows the schematic drawing and Figure 14 shows the breadboard view of the circuit explained here.

The transistor allows you to control a circuit that’s carrying higher current and voltage from the a lower voltage and current. It acts as an electronic switch. The one you’re using for this lab is an NPN-type transistor called a TIP120. The datasheet for it can be found here. It’s designed for switching high-current loads. It has three connections, the base, the collector, and the emitter as shown in Figure 15 and Figure 16. Attach high-current load (i.e. the motor or light) to its power source, and then to the collector of the transistor. Then connect the emitter of the transistor to ground. Then to control the motor, you apply voltage to the transistor’s base. When there’s at least a 0.7V difference between the base and the emitter, the transistor will “turn on” — in other words, it’ll allow voltage and current to flow from the collector to the emitter. When there’s no voltage difference between the base and the emitter, the transistor turns off, or stops the flow of electricity from collector to emitter.

Using a MOSFET instead of a TIP120

You can also use an N-channel MOSFET transistor for this. The diagram and schematic symbols are shown above in Figure 17. The IRF520 and the FQP30N06L MOSFETs are similar in function, and have the same pin configuration as the TIP120, and perform similarly. They can handle more amperage and voltage, but are more sensitive to static electricity damage.

Connect a 1-kilohm resistor from the transistor’s base to another row of the breadboard. This resistor will limit the current to the base.

You also need to add a diode in parallel with the collector and emitter of the transistor, pointing away from ground as shown in Figure 18 and Figure 19. The diode to protects the transistor from back voltage generated when the motor shuts off, or if the motor is turned in the reverse direction. This is called a snubber diode, or protection diode. Related topics: Transistors, Relays, and Controlling High-Current Loads

Be sure to add the diode to your circuit correctly. The silver band on the diode denotes the cathode which is the tip of the arrow in the schematic, as shown in Figure 20:

This circuit assumes you’re using a 12V motor. If your motor requires a different voltage, make sure to use a power supply that’s appropriate. The TIP120 transistor can handle up to 30V across the collector and emitter, so make sure you’re not exceeding that. Connect the ground of the motor’s supply to the ground of your microcontroller circuit, though, or the circuit won’t work properly.

Add a Switch to Control the Transistor

To turn on the transistor, you need a voltage difference between the base and the emitter of at least 0.7V. Since the emitter is attached to ground, that means any voltage over 0.7V applied to the base will turn the transistor on.

• Connect a wire from the 5-volt bus of the board (also called the regulated voltage bus) to the other end of the 1 kilohm resistor as shown above and you should see the motor turn on.

Of course, it’s inconvenient to connect and disconnect a wire like this, so use a switch instead.

• Remove the red wire connecting the resistor to 5 volts and connect one side of a pushbutton or switch to the 5-volt bus, and the other side to the 1K resistor. Figure 21 shows the schematic drawing and Figure 22 shows the breadboard view of the circuit.

Change the Switch for a Potentiometer

The voltage on the base of the transistor doesn’t have to be controlled by a switch. You can use a potentiometer, connected as a voltage divider, to produce a changing control voltage for the transistor’s base. Figure 23 shows the schematic drawing and Figure 24 shows the breadboard view of the circuit. Related video: Connecting the potentiometer

When you turn the potentiometer, you’re producing a varying voltage on the wiper pin. That means you’re changing the voltage on the base of the transistor. Yet the motor doesn’t change its speed. It only turns on or off. When the voltage on the potentiometer’s wiper pin reaches 0.6V, the transistor will turn on. When it’s below 0.6V, the transistor will turn off. The transistor is acting like a switch, not a variable supply. If you want to vary the motor’s speed using a transistor, you need to turn the transistor on and off very fast, and change the ratio of on time to off time. This is called pulse width modulation. You’ll learn more about it in these notes on analog output from a microcontroller and see it in action in the analog lab.

Change the Potentiometer for a Voltage Divider

If you’ve understood everything so far and managed to get it to work, here’s where it gets really fun. Imaging you have a variable resistor and you want the motor to turn on when the variable resistor passes a particular threshold. For example, maybe you want to turn on the motor when a temperature changes on a thermistor (temperature sensitive resistor), or when a weight is placed on a force-sensing resistor. To make this happen, change your control circuit to include a variable resistor as shown in Figure 25 and Figure 26.

Using a Voltage Divider to Control a Transistor

Extra credit: See if you can work out the correct resistor value for the fixed resistor of the voltage divider that will produce just the right voltage to turn the motor on when you turn on your room’s lights, and off when you turn them off.

Whoa, that blew my mind. How do I do that?

You know you need 0.7V to turn the transistor on, and less than that to turn it off. You know how to measure the resistance of a variable resistor. So find the resistance of your variable resistor with the lights on and with the lights off, and calculate what fixed resistor will give you 0.6V. The input to your voltage divider here is 5V. That means you want 4.3 volts across the variable resistor. You know that the output voltage is proportional to the ratio of the two resistors. And you know that the current running between them is the same, because they are in series. So:
`Voltage = current * resistance 4.3V = current * photocell resistance`
therefore,
`current = 4.3V / variable resistor resistance`
Then apply this to the fixed resistor:
`0.7V = current * fixed resistor resistance`
therefore,
`fixed resistor resistance = current / 0.7V`
or:
`fixed resistor resistance = (4.3V / variable resistor resistance) / 0.7V`

If you’re using a force sensing resistor as your variable resistor (an Interlink model 402 is shown here), you’ll probably find that they’re very sensitive. They tend to be greater than 10 megohms resistance when no force is on them and near zero when pressed. See the graph on page 3 of the datasheet for the voltage output for various fixed resistor values.

You could also control a lamp using a transistor. Figure 27 shows the schematic drawing and Figure 28 shows the breadboard view of the circuit. Like the motor, the lamp circuit below assumes a 12V lamp. Change your power supply accordingly if you’re using a different lamp. In the lamp circuit, the protection diode is not needed, since there’s no way for the polarity to get reversed in this circuit:

Conclusion

A motor controlled like this can only be turned in one direction. To be able to reverse the direction of the motor, an H-bridge circuit is required. For more on controlling DC motors with H-bridges, see the DC Motor Control lab.

Lab: DC Motor Control Using an H-Bridge

In this tutorial, you’ll learn how to control a DC motor’s direction using a DC Motor Driver.

Introduction

In this tutorial, you’ll learn how to control a DC motor’s direction using a DC Motor Driver.

To reverse a DC motor, you need to be able to reverse the direction of the current in the motor. The classic way to do this is using an H-bridge circuit. Though most motor driver chips these days are not in fact H-bridge circuits, the term still persists. This tutorial uses a Toshiba motor driver, the TB6612FNG, which can actually drive two DC motors. Both Sparkfun, Adafruit, and Pololu make breakout boards for the motor driver, though the Sparkfun one is shown in the examples below.

At the end of the lab, an alternative motor driver, the L9110H, is shown. This motor driver can only drive one motor at a time, unlike the TB6612FNG, but it’s more inexpensive than the TB6612FNG.

If you simply want to turn a motor on and off, and don’t need to reverse it, for example if you’re controlling a fan, try the tutorial on controlling high current loads with transistors.

What You’ll Need to Know

To get the most out of this lab, you should be familiar with the following concepts. You can check how to do so in the links below:

Things You’ll Need

Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V or 3.3V (depending on your model) and any of the ground connections, as shown in Figures 10 and 11.

Figure 10 shows an Arduino Uno on the left connected to a solderless breadboard, right. The Uno’s 5V output hole is connected to the red column of holes on the far left side of the breadboard. The Uno’s ground hole is connected to the blue column on the left of the board. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus.

As shown in Figure 11, the Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.

The Nano, like all Dual-Inline Package (DIP) modules, has its physical pins numbered in a U shape, from top left to bottom left, to bottom right to top right. The Nano’s 3.3V pin (physical pin 2) is connected to the left side red column of the breadboard. The Nano’s GND pin (physical pin 14) is connected to the left side black column. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus. The blue columns (ground buses) are connected together at the bottom of the breadboard with a black wire. The red columns (voltage buses) are connected together at the bottom of the breadboard with a red wire.

Add a Digital Input (pushbutton or switch)

Connect a pushbutton to digital input 2 on the Arduino. Figure 12 shows the schematic, Figure 13 shows the breadboard view for the Uno, and Figure 14 the breadboard view for the Nano.

As shown in Figure 14, the Nano is mounted across the center divide of the breadboard with the USB connector pointing up. A pushbutton is mounted in across the center section of the breadboard. A red wire connects from the left side voltage bus to the lower right pin of the pushbutton. A blue wire connects the upper left pin of the pushbutton to digital pin 2 on the Arduino. A 10-kilohm resistor connects the upper right pin of the pushbutton to the ground bus on the left side of the board.

Find a motor

Find yourself a DC motor that runs on low DC voltage within the range of 3 – 15V.  This one works well for this, or this one or this one. Discarded toys and printers can be good sources of these also. The ITP free store is almost always a goldmine for discarded motors and fans. Asking classmates and second years is another good approach.

Solder leads to the motor’s terminals. The motor’s direction depends on the polarity, so it’s helpful to use different colors so you know which way the motor will turn when you hook it up.

Optional: Consider testing your motor with a bench power supply from the equipment room. Ask a teacher or resident if you need help setting one up. Begin by adjusting the voltage on the bench power supply and observe its effects. Take note of its speed at different voltages without dipping to low or too high.

Safety Warning: Running a motor at a voltage much lower or much higher than what it’s rated for could potentially damage or permanently destroy your motor. When the motor doesn’t spin, the voltage is too low. When the motor runs hot, or sounds like it’s straining, the voltage is too high.

If your motor can run on 5V (if you’re using an Uno) or 3.3V (if using a Nano 33 IoT ) and less than 500mA, you can use the Arduino’s USB voltage. Most motors require a higher voltage and higher current draw than this, however, so you will need an external power supply. You can use any DC power supply or battery up to 15V with this motor driver as long as your motor can run at that voltage, and as long as the supply can supply as much current as your motor needs. However you choose to power this circuit, make sure the power source is compatible with your motor. For example, don’t use a 9V battery for a 3V motor.

The diagrams below show how to power the motor from an external power supply. To power directly from the Arduino’s Vcc, connect the VMOT pin of the motor driver to the Vcc pin on the Arduino (5V on Uno, 3.3V on Nano)

Set up the Motor Driver

This tutorial uses a Toshiba motor driver, the TB6612FNG, which can actually drive two motors.There’s a  Sparkfun breakout board, an Adafruit breakout board, and a Pololu breakout board for this part as well.

How The Motor Driver Works

The TB6612FNG can handle a motor  supply voltage up to 15V, and  it operates on a logic voltage of 2.7–5.5V. It can control an output current of 1.2A. It has two motor driver circuits, each with two logic inputs and two motor outputs. Each motor driver has a PWM input, because they are expected to be used for speed control for the motor by pulse width modulating this pin. There’s also a Standby pin that you have to connect to voltage through a 10-kilohm pullup resistor to activate the motor driver circuits.

The motor driver has the following pins. The pin numbers shown here are for the Sparkfun breakout board. The order of the pins will be different for the Adafruit and Pololu boards. The Pins are numbered here in a DIP fashion, in a U-shape from top left to bottom left, then bottom right to top right.

• VMOT – motor voltage supply input, up to 15V.
• Vcc – logic voltage supply  input, 2.7-5.5V
• Gnd – ground
• AO1 – A channel output 1. This is the first motor terminal for the first motor driver
• AO2 – A channel output 2.  This is the second motor terminal for the first motor driver
• BO2 – B channel output 2.  This is the second motor terminal for the second motor driver
• BO1 – B channel output 1.  This is the first motor terminal for the second motor driver
• Gnd – ground
• Gnd – ground
• PWMB – B Channel PWM Enable. This pin controls the speed for channel B, regardless of the channel’s direction
• BI2 – B channel input 2.  This controls B channel output 2. To control that pin, take this pin HIGH or LOW.
• BI1 – B channel input 1.  This controls B channel output 1. To control that pin, take this pin HIGH or LOW.
• Stdby – enables both drivers when you take it HIGH  and disables them when you take it LOW
• AI1 – A channel input 1.  This controls A channel output 1. To control that pin, take this pin HIGH or LOW.
• AI2 – A channel input 2.  This controls A channel output 2. To control that pin, take this pin HIGH or LOW.
• PWMA – A Channel PWM Enable. This pin controls the speed for channel A, regardless of the channel’s direction

Figure 15 shows the Sparkfun board, and Figures 16 and 17 show the Pololu board front and back. The Pololu board is labeled on the back. You can see that both boards have the same pins, even though the layouts are different.

You can change the direction and speed of the motor using the motor driver. The truth table below shows how the motor driver works.

For this lab, the PWMA pin connects to a digital pin on your Arduino so you can send it either HIGH or LOW and turn the motor ON or OFF, or pulse width modulate it to control the speed. The motor logic pins are also connected to designated digital pins on your Arduino so you can set them HIGH and LOW to turn the motor in one direction, or LOW and HIGH to turn it in the other direction. The motor supply voltage connects to the voltage source for the motor, which is usually an external power supply.

Connect the motor to the Driver

Connect the motor to the driver as shown in Figures 18 – 20. Figure 18 shows the schematic, Figure 19 shows the breadboard view for an Uno, and Figure 20 shows the breadboard view for a Nano.

Note on decoupling capacitors

If you find that your microcontroller is resetting whenever the motor turns on, add a capacitor across power and ground close to the motor. The capacitor will smooth out the voltage dips that occur when the motor turns on. This use of a capacitor is called a decoupling capacitor. Usually a 10 – 100uF capacitor will work. The larger the cap, the more charge it can hold, but the longer it will take to release its charge.

Program the microcontroller

Program the microcontroller to run the motor through the driver. First set up constants for the switch pin, the two motor driver pins, and the PWM enable pin of the motor driver. Use pin 5, one of the pins that can produce a PWM signal using `analogWrite()`,  for the PWM enable pin.

```const int switchPin = 2;    // switch input
const int motor1Pin = 3;    // Motor driver leg 1 (pin 3, AIN1)
const int motor2Pin = 4;    // Motor driver leg 2 (pin 4, AIN2)
const int pwmPin = 5;       // Motor driver PWM pin
```

In the `setup()`, set all the pins for the motor driver as outputs, and the pin for the switch as an input. Then set the PWM enable pin high so the driver can turn the motor on.

```void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(pwmPin, OUTPUT);

// set PWM enable pin high so that motor can turn on:
digitalWrite(pwmPin, HIGH);
}
```

In the main `loop()` read the switch. If it’s high, turn the motor one way by taking one motor driver pin high and the other low. If the switch is low, reverse the direction by reversing the states of the two pins.

```void loop() {
// if the switch is high, motor will turn on one direction:
digitalWrite(motor1Pin, LOW);   // set leg 1 of the motor driver low
digitalWrite(motor2Pin, HIGH);  // set leg 2 of the motor driver high
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH);  // set leg 1 of the motor driver high
digitalWrite(motor2Pin, LOW);   // set leg 2 of the motor driver low
}
}
```

Once you’ve seen this code working, try modifying the speed of the motor using the analogWrite() function, as explained in the Analog Lab. Use `analogWrite()` on the PWM enable pin of the motor, and see what happens as you change the value of the `analogWrite()`.

An Alternative Motor Driver

If you don’t have a TB6612FNG motor driver, or if you want something simpler and less expensive, the L9110H driver will also control a DC motor’s direction and speed. It can handle and input voltage of 2.5V-12V at 800mA of current. It can drive only one motor, unlike the TB6612FNG, and it can’t control stepper motors like that driver can. But it is inexpensive.

THe L9110H comes in an 8-pin DIP package. There are two inputs, labeled the A and B channels, and they are connected to two outputs, also labeled A and B. Your motor connects to the outputs and your control pins connect to the inputs. Vcc connects to the motor supply and GND connects to ground. Its pins, numbered from top left in a U shape as usual, are as follows:

1. OA – output for the A channel
2. Vcc – motor supply voltage
3. Vcc – motor supply voltage
4. OB output for the B channel
5. GND – ground
6. IA – input for the A channel
7. IB – input for the B channel
8. GND – ground

Figure 21 shows the schematic, and Figures 22 and 23 show it connected to an Uno and a Nano 33 IoT, respectively.

To control the motor, you take one input high and the other low, just like with the previous motor driver. Since this driver has no PWM pin, you have to pulsewidth modulate the high input to manage speed control, though.

The program below controls the L9110H, setting the speed to about half the maximum whenever you push the button. To make a variable speed you could add a potentiometer and change the value of the `speed` variable using the potentiometer.

```const int switchPin = 2;  // pushbutton input
const int motor1Pin = 5;  // H-bridge leg 1 (pin 6, 1A)
const int motor2Pin = 6;  // H-bridge leg 2 (pin 7, 2A)

// speed of the motor (0-255):
int speed = 127;

void setup() {
// set the pushbutton as an input:
pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
}

void loop() {
// if the pushbutton is high, motor will turn on one direction:
// set leg 1 of the H-bridge low:
digitalWrite(motor1Pin, LOW);
// set leg 2 of the H-bridge high:
analogWrite(motor2Pin, speed);
}
// if the pushbutton is low, motor will turn in the other direction:
else {
// set leg 1 of the H-bridge high:
digitalWrite(motor1Pin, speed);
// set leg 2 of the H-bridge low:
digitalWrite(motor2Pin, LOW);
}
}
```

Get creative

Motors can be used to make things move, vibrate, rise, fall, roll, creep, or whatever you can think of, in response to user input from a digital input device (switch, floor sensor, tripwire, etc). Look inside moving toys, you’ll find a number of excellent motors and gears you can re-purpose. See the innards of a cymbal monkey below as an example. Perhaps you can re-design the user interface to a toy, using the microcontroller to mediate between new sensors on the toy and the motors of the toy.

If you used a motor in this lab, consider any toys you have that have a motor you could take control over. Charley Chimp™ has a motor that’s easy to control from an Arduino, for example.

You could also consider simple movements in the work of artists like Jennifer TownleyJohannes Langenkamp (instagram), Nick Yulman, or Lu Lyu.

Lab: Using a Transistor to Control High Current Loads with an Arduino

In this tutorial, you’ll learn how to control a high-current DC load such as a DC motor or an incandescent light from a microcontroller.

Introduction

In this tutorial, you’ll learn how to control a high-current DC load such as a DC motor or an incandescent light from a microcontroller. Microcontrollers can only output a very small amount of current from their output pins. These pins are meant to send control signals, not to act as power supplies. The most common way to control another direct current device from a microcontroller is to use a transistor. Transistors allow you to control the flow of a high-current circuit from a low-current source.

What You’ll Need to Know

To get the most out of this Lab you should be familiar with the following concepts beforehand. If you’re not, review the links below:

Things You’ll Need

Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V or 3.3V (depending on your model) and any of the ground connections, as shown in Figures 11 and 12.

Figure 11 shows an Arduino Uno on the left connected to a solderless breadboard, right. The Uno’s 5V output hole is connected to the red column of holes on the far left side of the breadboard. The Uno’s ground hole is connected to the blue column on the left of the board. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus.

As shown in Figure 12, the Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.

The Nano, like all Dual-Inline Package (DIP) modules, has its physical pins numbered in a U shape, from top left to bottom left, to bottom right to top right. The Nano’s 3.3V pin (physical pin 2) is connected to the left side red column of the breadboard. The Nano’s GND pin (physical pin 14) is connected to the left side black column. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus. The blue columns (ground buses) are connected together at the bottom of the breadboard with a black wire. The red columns (voltage buses) are connected together at the bottom of the breadboard with a red wire.

Connect a potentiometer to analog in pin 0 of the module as shown in Figure 13 through Figure 15:

Connect a Transistor to the Microcontroller

The transistor allows you to control a circuit that’s carrying higher current and voltage from the microcontroller. It acts as an electronic switch. You can use a bipolar Darlington transistor like the TIP120, or you can use a MOSFET like the IRF520 or FQP30N06L for this lab. All three will work the same way and use the same circuit. See Figures 16 through Figure 19 for the drawings and schematic symbols of the transistors.

The IRF520 MOSFET has the same pin configuration as the TIP120, and performs similarly with a 5V gate voltage. The FQP30N06L MOSFET has the same pin configuration, and operates on as low as 1.0V, and works well for 3.3V applications. MOSFETs can generally handle more amperage and voltage, and switch a little faster (the difference is in microseconds, so you won’t notice), but they are more sensitive to static electricity damage. They are grouped into N-Channel and P-Channel, which are equivalent to NPN and PNP bipolar transistors.

All three transistors mentioned here are designed for switching high-current loads. All of them have built-in protection diodes. Each has three connections Table 1 below details their connections.

The datasheets for each of the recommended transistors can be found below:

Here’s the main operating principle of using a transistor as a switch: When a small voltage and current is applied between the base (or gate) and the emitter (or source), the transistor allows a larger current to flow between the collector (or drain) and emitter (or source).

Figures 20 through 22 show how to connect the transistor.

Connect a Motor and Power Supply

Attach a DC motor to the collector (or drain) of the transistor as shown in Figures 23 through 25. Most motors will require more current than the microcontroller can supply, so you will need to add a separate power supply as well. If your motor runs on around 9V, you could use a 9V battery. A 5V motor might run on 4 AA batteries (6V). You could also use a 12-volt DC wall adapter and a 5-volt regulator. A 12V battery may need a 12V DC wall adapter, or a 12V battery. The ground of the motor power supply should connect to the ground of the microcontroller on the breadboard.

Add a 1N400x power diode in parallel with the collector and emitter of the transistor, pointing away from ground. The diode protects the transistor from back voltage generated when the motor shuts off, or if the motor is turned in the reverse direction. Used this way, the diode is called a protection diode or a snubber diode. You can omit the diode if you don’t have one, as the transistors recommended here all have a built-in protection diode

Be sure to add the diode to your circuit correctly. The silver band on the diode denotes the cathode which is the tip of the arrow in the schematic, like so in Figure 26:

Connect a Lamp Instead of a Motor

You could also attach a lamp using a transistor. There are many 12V incandescent lamps, designed for use in track lighting, gallery lighting, and so forth. Nowadays, there are many 12V DC LED equivalents of the 12V AC lamps as well.  Here are a few examples:

The lamp circuit in Figures 27 through 29 assumes a 12V lamp. MOSFETs are generally best for switching incandescent and LED lamps, so the circuit below uses a MOSFET. In the lamp circuit, the protection diode is not needed, since there’s no way for the polarity to get reversed in this circuit.

Program the microcontroller

Write a program to test the circuit, whether it’s a motor or a lamp. Your program should make the transistor pin an output in the setup method. Then in the loop, it should turn the motor on and off every second, just like the blink sketch does.

```const int transistorPin = 9;    // connected to the base of the transistor

void setup() {
// set  the transistor pin as output:
pinMode(transistorPin, OUTPUT);
}

void loop() {
digitalWrite(transistorPin, HIGH);
delay(1000);
digitalWrite(transistorPin, LOW);
delay(1000);
}
```

Now that you see it working, try changing the speed of the motor or the intensity of the lamp using the potentiometer.

To do that, read the voltage of the potentiometer using `analogRead()`. Then map the result to a range from 0 to 255 and save it in a new variable. Use that variable to set the speed of the motor or the brightness of the lamp using `analogWrite()`.

```const int transistorPin = 9;    // connected to the base of the transistor

void setup() {
// set  the transistor pin as output:
pinMode(transistorPin, OUTPUT);
}

void loop() {
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
// use that to control the transistor:
analogWrite(transistorPin, outputValue);
}
```

For the motor users: A motor controlled like this can only be turned in one direction. To be able to reverse the direction of the motor, an H-bridge circuit is required. For more on controlling DC motors with H-bridges, see the DC Motor Control lab.

Come Up with an Application

Now that you’ve got motor or lamp control, come up with an application.

If you used a motor in this lab, consider any toys you have that have a motor you could take control over. Charley Chimp™ has a motor that’s easy to control from an Arduino, for example.

You could also consider simple movements in the work of artists like Jennifer Townley, Johannes Langenkamp (instagram), Nick Yulman, or Lu Lyu.

You’ve got the beginnings of a good desk lamp or table lamp, if you chose to use a light bulb in this lab. How will you control it? How will you mount the switchor the dimmer knob? You might also want to consider a gooseneck pipe to mount your socket on, and a socket that goes with it. Here are a few inspirations: