Diodes, Transistors and Optocouplers: what they are, some theory and simple usages examples with Arduino

Last updated on Sun, 2012-10-07 11:08. Originally submitted by fabio on 2010-07-22 10:39.

In this post I'll experience with three kind of components available in the Arduino Base Workshop KIT: diodes (model 1n4007), transistors (models BC547 Transistor and MOS Irf540) and optocouplers (model 4N35). I'll briefly describe them and we'll see some simple examples of circuits built upon an Arduino Duemilanove board which make use of them.

Diodes: driving current only in one way

Well, the title says it all. A diode is a two terminals electronic component that conducts electric current only in one direction.

The diode 1n4007 available in the Arduino Base Workshop KIT

The most common function of a diode is to allow an electric current to pass in one direction (called the diode's forward direction) while blocking current in the opposite direction (the reverse direction). Thus, the diode can be thought of as an electronic version of a check valve. This unidirectional behavior is called rectification, and is used to convert alternating current to direct current, and to extract modulation from radio signals in radio receivers.

A simple Arduino based circuit using a diode

We can build this simple circuit to show how diodes works:

Scheme of a simple Arduino base circuit using a diode

Once assembled on the Arduino board, the circuit will looks like:

Picture of a simple Arduino base circuit using a diode

What does this circuit do? Well, nothing complex or useful actually. But we can understand how diodes works with it. If we plug the diode as seen on the pictures above when we'll push the button the LED will turn on. Cool, uh?

Instead, if we revert the diode so that current now flows in the opposite direction, we'll se that the LED won't turn on when we press the button. This confirms the behaviour of a diode: it lets the current flows if it's passing in its forward direction while it blocks when it pass in the opposite direction. You can see a demo in the video below.


A transistor is a semiconductor device which can be used to amplify or switch electronic signals. In the simple Arduino based circuits transistors are usually used as switch for electronic signals.

Transistors usually have 3 connectors called collector, base and emitter. In normal state the collector and emitter are disconnected but, when a current is applied to the base connector, the transistor change its state and the collector and emitter get connected thus current can flows between them.

This behaviour make the transistor the perfect component for interface two different circuits operating at different voltages. For example one circuit could be the one powered by Arduino: small current and low voltages. The other circuit could be the one operating a DC motor which needs a lot of voltage and current. Connecting the two circuits could be painful but with the transistor we could drive the second circuit by changing the state of the transistor trough the first circuit connected to the Arduino. Cool stuff.

Transistors BC547 (right) and MOS Irf540 (left)

The Arduino Base Workshop KIT comes with two types of transistors displayed in the picture above: a MOS Irf540 (left) and a BC547 (right). They differs from the building technique which results in different specifics. For all the details have a look at the detailed specifics linked above but they mostly work the same way: they only differ in the amount of current they are capable of deliver. For bigger currents (eg powering motors) the MOS Irf540 will be perfect. The BC547 is not capable of delivering lot of current so use it with care.

A simple Arduino based circuit using transistors

We will use a transistor controlled by the Arduino board to act as a switch on an external circuit. The transitor base will be connected to an Arduino output pin. This is the circuit:

Scheme of a simple transistor circuit on a Arduino board

Once plugged into the Arduino board it will looks like:

Scheme of a simple transistor circuit on a Arduino board

Note that the circuit coming from Vin (9V) is actually completely separated from the one coming from the Arduino board. They are two indipendent circuit. Only the transistor let them interact.

We will now use the Hello World program with a simple modification: we will use pin 2 as output (in the helloworld program we used pin 13). To get the code refer to the Hello World blog post.

What does the circuit we created do? Well, let's see in the video below:

As you can see each time the output on pin 2 is HIGH our transistor will get a voltage on its base connector resulting in the collector and the emittor getting connected. Current coming from the +9V source can the flow down throgh the resistor and the three series LEDs lighing them on.

A similar result could have been achieved using the MOS Irf540 transistor.

UPDATE 2012-10-07: Improvement

The circuit above can be improved by using this circuit:


This is preferable against the one explained before because it won't float when the microcontroller is shut off or when it is not actively drive the pins. Basically, the circuit explained above only works reliably when the microcontroller actively drives the pin high or low. When it is not doing so, the transistor may float randomly and close/open the circuit. The fixed circuit in this section, by using a pull-down resistor doesn't show this effect and works reliably.


An optocoupler, also called opto-isolator, optical isolator, optical coupling device, photocoupler, or photoMOS, is an electronic device that usually contains both an infrared light-emitting diode (LED) and a photodetector and use them to transfer an electronic signal between element of circuits maintaining them electrically isolated.

Internals of an optocoupler

When a voltage is applied to the LED, the LED lights and illuminate the photodetector which produces an output current on the photodetector: basically this means that now the photodetector circuit is now connected and current can flow in it.

4N35 Optocoupler DIL-6 package

The Arduino Base Workshop kit comes with two 4N35 Optocouplers packaged as a DIL-6 package.

4N35 Optocoupler from the Arduino Base Workshop KIT

This little component has 6 legs each of them having a different usage. It can be easealy understood while looking at the following picture from the 4N35 datasheet which shows us the inside schematics of the 4N35:

Schematics of the inside of an 4N35 Optocoupler

So, we have leg 1 and 2 near the printed dot on the chip (that's visible on it if we look carefully) that acts respectly as anode and cathode. Leg 3 isn't connected to anything: it's just useless. We then have leg 4, 5, 6 respecly emitter, collector and base.

We already know these terms from the transistor introduction above. They do exactly the same of the legs of a transistor. The difference here is that we can leave the base unconnected and just use the LED (legs 1 and 2) to connect the collector and the base.

An Arduino based circuit using an 4N35 Optocoupler

Now that we know the theory behind optocouplers and that we know how to connect our 4N35 Optocoupler it's now time to create a simple circuit with it. Here it is:

Scheme of a simple Arduino based circuit using an 4N35 Optocoupler

The circuit above once created using the Arduino board will looks like:

Picture of a simple Arduino based circuit using an 4N35 Optocoupler

We can use the same program used for the transistor example above. This is the result:

So, it basically do the same of the transistor example above but this time we are using an optocoupler. Not bad, uh?


Now, I know how to use three new electronic components: diodes, transistors and optocouplers. The simple circuits I built will be used as base for more complex stuff. I'm pretty sure these components will be pretty useful when I'll build more complex stuff. Looking forward to it!


Diode on Wikipedia
Transistor on Wikipedia
BC547 Nodatasheet by Daniel Soltis
BC547 Official Datasheet
Arduino Hello World on this blog
Opto-isolator on Wikipedia
Electus Distribution Reference Data Sheet: OPTOCOUP.PDF
4N35, 4N36, 4N37 OPTOCOUPLERS Data sheet

transistors_1.jpg574.38 KB


Submitted by Hubschrauber (not verified) on Sun, 2015-05-10 07:39.

The section about the 4n35 says: "The difference here is that we can leave the base unconnected and just use the LED (legs 1 and 2) to connect the collector and the base." Doesn't the internal LED actually cause the collector and the emitter to connect?

Hi I have read that in

Submitted by j (not verified) on Wed, 2014-06-04 00:09.


I have read that in nuclear industries, optical isolators should be used for acheiving independence (i.e. isolation so that if one part gets damaged other part is not affected ) instead of resistors. Could you please explain to me why?

Re: Hi I have read that in

Submitted by Anonymous (not verified) on Tue, 2015-06-09 17:36.

Hi J,

So the difference is that a resistor isn't directional and will therefore allow current to flow from either side. An optical isolator on the other hand works similar to a diode in that it only lets unidirectional transmission. This is good when you don't want a component further along in the system to affect previous components.

I suppose in this case, an optical isolator would prevent surges and other damaging events from passing to the parts that work well, while a resistor would let it pass right on through.

Thanks a lot

Submitted by N. Blondel (not verified) on Mon, 2013-10-14 23:33.

Thanks for these precisions, it helped me to understand a lot too!
I studied electronics before going to IT embedded systems and I wanted to get back to electronic as a hobby. So I bought this base workshop kit with an Arduino Uno; but my memory of these components is a bit old and I was even wondering what was some of the components of the kit...

Whatever great article, thanks for sharing!

Good article

Submitted by AN (not verified) on Sun, 2012-10-07 02:37.

Thank you for the good article!
Reading, I have noticed that looks like the terminals on the picture of transistors (specifically at left of the 5-cent coin) which is TO-220 packaging one are named incorrect: CBE should be read as BCE according to the specs

That is right. Thanks for

Submitted by fabio on Sun, 2012-10-07 11:09.

That is right. Thanks for noticing and posting a comment. I have fixed the image and also added an improved circuit which should be better.

Which to choose ?

Submitted by Pierre-Alain Dorange (not verified) on Tue, 2012-07-31 21:19.

Thanks for this simple explanation, very useful for a beginner like me.
But i'm a bit perplex when i'll have to choose between the last 2 techniques (transistor, opto and rely could be added)... I do not understand what is the difference and when it's better to choose a transistor, an opto or a relay.
I saw schematics using both relay and transistor... Probably related to power necessary.

thank you very much for time

Submitted by anthony treacy (not verified) on Sat, 2011-03-26 14:09.

thank you very much for time to submit this web page. very helpfull. it helped me alot.

thank you again ;-)

You are welcome. Thanks for

Submitted by fabio on Sat, 2011-03-26 14:19.

You are welcome. Thanks for stopping by and for saying thanks. ;-)