The transistor can be used to control a component when the Arduino board doesn’t have the necessary power.
The transistor amplifies a pwm signal from your arduino board. With it, you can operate a 12V motor using an external power supply and a transistor, for example.
There are two types of transistor: Mosfet and bipolar.
The Mosfet transistor allows you to control the voltage of your component. In other words, you can vary the voltage but not the current. Here’s a Mosfet transistor reference for arduino: IRF520.
The bipolar transistor allows you to control the current of your component. In other words, you can vary the current but not the voltage. Here’s a bipolar transistor reference for arduino: BC547B.
If you don’t know whether your component should be controlled by voltage or current, you can look at its data sheet, where you’ll probably find graphs showing whether it’s voltage that varies the value of your component, or current.
The transistor is a semiconductor made up of a PN junction. This junction is also used to make diodes. We advise you to use a protection diode with the transistor to prevent it from burning out.
The transistor has three pins:
Emitter: Corresponds to the Vcc of your external power supply
Collector: Corresponds to the GND of your external power supply
Base: This is the signal pin coming from the arduino board.
As we said earlier, the transistor acts as a relay between the Arduino board and your component if the Arduino is not powerful enough to operate it.
The Arduino board can power components up to 5V and 0.4 amps. If you want to control a 12V motor, you will need an external power supply to power the motor and a relay or transistor to control it.
The two components allow you to control a component that requires more power than the arduino board, using an external power supply.
However, the relay only enables the component to be switched on and off without being able to vary the voltage. It is therefore very useful for switching on a lamp, for example.
We will now see how to control a motor with an external power supply of 9 :
Pin 3 of the arduino board must be programmed to send a signal to the transistor. Here’s the code that turns the motor on and off alternately:
int motor =3; // Pin used for motor
void setup(){
pinMode(motor, OUTPUT); // We declare the motor as output
}
void loop(){
digitalWrite(motor, HIGH); // Turn on the motor
delay(1000); // Wait one second
digitalWrite(motor, LOW);
delay(1000); // Wait one second
}
We’re now going to look at how to vary the power of an LED using a transistor. As we said earlier, the difference between a transistor and a relay is that a transistor allows the voltage at its terminals to be varied. This is what we’re going to see with an LED:
Here is the code to vary the voltage across the LED:
int led = 6;// led pin
void setup() {
pinMode(led, OUTPUT); // Initialise the output led
}
void loop() {
for(int i=0; i<255; i++){ // turn the led on progressively with a for loop
analogWrite(led, i); // assign this to the led
delay(5);// Pause for 5 milliseconds
}
for(int i=255; i>0; i--){ // Turn the led off gradually using the for loop
analogWrite(led, i);// Assign this to the led
delay(5); // Pause for 5 milliseconds
}
}