Putting the GPIO pin behind transistor to get more current

In this example we are going to let the GPIO pin control a transistor so that we can draw more power when controlling a LED than what the GPIO pin can supply.

DISCLAIMER

We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry Pi.

We will be using a simple LED in this example where we aim to draw 16 mA power (I could easily go higher using this method if I had bigger LED that can draw more power since the current will mostly come from the 5V and not from the GPIO pin it self), load on the GPIO pin will only be around 0,00018 A.

At far bottom of this article we will also deal with the initial undetermined state of the GPIO pin at boot up time before your application runs.

A transistor is basically a switch that is triggered by tiny electric signal.  (Click here if wanting to understand transistor specifications sheets)

Transistor is suitable to use when switching within the same circuit.  While if the power your switching is in completely different circuit then you would want to use a optocoupler which is another kind of electric switch where only light signal is sent between the two circuits keeping them isolated.   (See our guide about Optocouplers)

There are NPN and PNP transistors. The NPN ones switch on the low side (-) while PNP transistors switch on the high side (+). This is important difference and for example this difference would mean that the NPN transistor that we will be using in this example would not have been suitable for the RGB LED example since there we would need to switch on the high side (+) as the RGB LED had 3 anodes (+) and only one cathode (-).

In the RGB LED example the LED was not shining very bright at all since we could not provide a lot of power from the GPIO pin, a transistor solves such issues.

Pieces we use:

If you are not familiar with working with a breadboard or on how to place a cobbler on the breadboard then click here bellow

Breadboard

If you don’t know resistor color coding's to find correct resistors then you can click the link bellow

Resistor color coding

Drawing up the circuit:

First we just draw up the circuit without knowing the values of the resistors.

Here is what we know:

Now we calculate the rest:

Now that we have the voltage and current then R2 = 2,6 V / 0,00018 A = 14,4 kΩ  (We will use 15 kΩ resistor for R2)

We we update our circuit with the calculated values of R1 and R2:


Now we are ready to put this onto the breadboard:

We will use GPIO 5 for the GPIO pin.

For the led then you can see in the picture bellow then that you have two ways to identify the + and – or anode vs cathode. Its the length of the pins where the longer wire is the anode and the shorter is the cathode. If wires have been trimmed then you can usually also see it on the LED it self the flat side on the edge is the cathode or – as is shown on the picture to right here.

If you need more information about orientation of other kinds of LED then click this button.

LED oritentation


When looking up the spec for your transistor then the spec will usually show how the pin layout is so you can know which was it should turn.

For mine it was like this:


When putting the parts on the breadboard then it is good idea to go by the circuit drawing instead of blindly following the breadboard drawing since its easier to make mistakes if not understanding the wiring.

Raspberry Pi pin layout

If you need more information about the GPIO headers on Raspberry Pi or other boards then click bellow.

Raspberry Pi Pin guideOrange Pi One Pin guideOrange Pi Pin guide for most H3 modelsOrange Pi Zero 3 Pin  guideOrange Pi 5 Plus Pin guideRock 64 Pin guide

The Xojo code for this one really simple as this was more of electric guide than coding guide:

We just had one checkbox on a window with the caption “LED on”.

In the window I added the following property:

Private mLineRequest As Gpiod.LineRequest

And then Opening event to the Window:

Sub Opening()
  try
    using Gpiod

    var chip as Chip = Chip.Open("/dev/gpiochip0")

    // We call helper function to set up pin number 5 in output mode.
    mLineRequest = RequestOutputLine(chip, 5, "led control")

  catch e as GpiodException
    MessageBox(e.Message)
  end try
End Sub
Function RequestOutputLine(chipHandle as Gpiod.Chip, lineOffset as UInt32, consumer as String) As Gpiod.LineRequest
  // Helper function to set up output pin.

  using Gpiod

  // Exceptions will be caught by the caller method

  var reqCfg as RequestConfig = nil

  var setting as new LineSetting()
  setting.Direction = Gpiod.Direction.OUTPUT
  setting.OutputValue = Gpiod.LineValue.INACTIVE

  var lineCfg as new LineConfig()
  lineCfg.AddLineSetting(array(lineOffset),setting)

  if consumer <> "" then
    reqCfg = new RequestConfig()
    reqCfg.Consumer = consumer
  end if

  return chipHandle.RequestLines(reqCfg, lineCfg)
End Function

And finally ValueChanged event for the checkbox:

Sub ValueChanged()
  mLineRequest.Value(5) = if(me.Value, Gpiod.LineValue.ACTIVE, Gpiod.LineValue.INACTIVE)
End Sub

Getting the code

You can find this example code at Example code for GPIO guides.


Un determined state on the GPIO pin at startup

Now I was not 100% happy when running it, the LED was on when I booted the computer which is caused by the GPIO pin having undetermined state.
Everything else worked, the code was able to turn it on and off, and the LED was shining bright as it was not having to be conservative on the current it takes. There was easy fix to that, adding 1kΩ resistor between the GPIO pin and the ground to act as a hardware pull down resistor.

I did not want to enable pull down resistor in the software GPIO layer since then let might still come on while Raspberry Pi was booting.

This change made the circuit end like this:


After adding the 1kΩ resistor then the LED was shut when booting the computer and it only got turned on and off when sending the signal from Xojo the program with the checkbox button.