Sunday 5 July 2020

Using Transistors As a Switch with Arduino

 Recently, I have had reason to look into switches and how they can work with Arduino, and other popular MCU's.

This is part of a larger piece of investigation that is looking into buttons, sliders, potentiometers, switches etc as inputs whilst I think about how to make my own keyboard.

Of course, everyone has heard of a transistor. We all have some form of awareness as to how important they are with regards to the evolution of technology and computing. But what is a transistor really? Anyone with a background, or an interest in computing will probably tell you that they are used to calculate things on a CPU, and they are correct - but this isnt what transistors are limited to.

I am not going to go into the science of transistors here or how they work on a microelectronic level, there are plenty of other blogs and articles who have done a much better job than I could ever do. What I am aiming to do here is to give a quick guide on how to pretty much use a transistor as a switch, how to connect one up and how to control one in the code.

Transistors basically can do two things; they act as switches and they can help amplify signals. And they do this by controlling the current flowing through them. However today, I am only going to be talking about how they can be used as a switch in this article. You may be asking why would you want to use a transistor as a switch? There are no physical controls, so you would need to programmatically control it - and that's the beauty of using a transistor as a switch.

One really good example of how this can be used could be this - you have a light somewhere that you want to turn on when it gets dark. You build a circuit with a lamp, and a light sensor. When the state of the light sensor reaches a certain point, you then want the lamp to light up. So you can use a transistor to act as the switch to do this.

Naturally, all of this would require an MCU of some type as well as some code, but it really is a nice little example of a potential application.

One of the neat features of using a transistor as a switch is, depending on your transistor, that you may be able to switch on a circuit of a much higher voltage. This is because a lot of transistors are capable of handling a lot more voltage because of their amplification abilities.

Transistors typically come with three pins, which are cryptically called:

  • Collector: This is normally the current you are trying to control.
  • Base: This is normally the current you use for the switch. It controls the bias on the transistor.
  • Emitter: This normally goes to ground.
For the examples below, I will be using the following components:

  • 2x 1 kohm resistors
  • 1x red LED
  • 1x PN2222A transistor
  • 1x Pro-Micro/Arduino Nano MCU

Here is an example diagram to help visualise this:


On this breadboard we have a transistor acting as a switch to control the LED. When 5 volts is applied to the power rail, the LED will light up, if I remove the transistor from the circuit then apply power, the LED would not light up.

Why is this? Well, to explain this, lets look at the names of the pins in relation to the connections in the diagram. 

Starting from left to right, we have the collector first on pin 1. This has the LED connected to it, with a 1K resistor running to 5 volts. This part of the circuit represents the current we are trying to control.

The middle pin, pin 2 - we have the base. This is the switch itself, when enough current is passing through it, the switch is closed. When there is insufficient, or zero current flowing through it, the switch is open. This controls the flow of current through the collector - this means when the switch is closed, current flows through the collector and the LED lights.

On the right, we have the third pin, which is tied to ground.

If you construct this circuit, there are two ways to test it. Firstly, remove the transistor from the circuit and apply power - the LED should be off. If it is on, then you are passing power to the LED without the transistor controlling it.

Second, disconnect power from the base, this should turn off the LED. If it doesnt, then again - you are probably passing power to the LED outside of the control of the LED.

Now, the above shows a transistor controlling current to an LED using 5 volts, but remember when I said they can also be used to control the current in circuits that require a higher voltage? We can create an example of this as well, but this time using 3.3 volts to control the transistor whilst still using 5 volts to light the LED:

All that has changed in this example is that we are now applying 3.3 volts to the lower power rail as well as 5 volts to the upper power rail. When the power is connected, the LED will light up.

One point to note here is that using a transistor in this setting is rather pointless. All this circuit does is provide power to an LED. This can lead to some confusion  when building such a circuit for testing or educational purposes.

So how about we create the classic "blink", but this time using our transistor? To do this, we are going to need to use a micro controller, for this example I am going to use a Pro-Micro/Arduino Nano using 5 volts. This means we can keep our initial breadboard design the same, more or less.

All we need to do is alter the way the transistor is biased. Instead of simply being powered directly from the power rail, we are going to connect it to a GPIO pin:


This is the same circuit as the one I initially built. It is orientated differently to accommodate its one difference, and that difference is how we are controlling the base on the transistor.

Previously, we were simply providing it with current, which caused the LED to be illuminated for as long as power was available. However in this example, we have connected the base to pin 2 on the Nano. Unless you make it happen, pin 2 will not be high, which means if you create this circuit and power it, the LED will not light up.

So, on to the the classic "blink", to achieve this we need to write a couple of lines of code, the whole thing is this:




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(2, HIGH);
  delay(1000);
  digitalWrite(2, LOW);
  delay(1000);
}


The setup for this is simple, we just need to define the pin we are going to use to control the base. In the diagram we have it connected to pin 2, so that is what we define here. We need this to be used for output, as we will be sending a signal to the transistor.

The main body of the program should be familiar to anyone who has tried out the blink sketch that is used to test out various developer boards. We simply take pin 2 high for a couple of seconds before sending it low for the same amount of time.

This basically opens and closes the transistor as if it were a switch, if you try this out for yourselves, you will see the LED blinking on and off every couple of seconds.

Now, lets get in to some specific points that are worth mentioning:

Using resistors is essential. Not putting a resistor across a transistor in the way I have shown above can often cause your transistor to melt, it certainly did for me unless the ones I had were also defective. It is important to note that the resistors you use should be of the appropriate resistance for your load - it wont always be 1 kOhm.

The examples above use an Arduino Nano, but everything here is also compatible with the cheaper Pro-Micro developer board. The only difference between the two boards is that one provides a 3.3 volt rail and the other doesn't.

Using a GPIO pin to control the biasing of a transistor is the win here. The state of the transistor can be governed by the state of a separate sensor(s), the result of a programmatical state or pretty much anything that is capable of changing the state of that pin.

For me, it was fun but a little frustrating learning about how transistors work. Writing this blog entry has been part of my learning process and perhaps someone will find it useful in the future themselves :)

No comments:

Post a Comment