Hello,
I will show you in this video how to connect a breadboard to the Raspberry Pi 400
and how to control an LED using a simple Python program.
There are plenty of YouTube videos which introduce the Pi 400 computer,
but most of them are geared towards using it as a desktop PC.
What this video will show instead, is how to use Pi 400 to control electronic circuits
through the General Purpose Input-Output (GPIO) Ports.
The Pi’s GPIO headers are exposed in the back of the device as a large rectangular connector with 40 pins.
You can connect various electronic components such as LEDs, sensors and motors to these headers
and control them by programming the Pi.
While you could use individual wires with this connector, I strongly advise against it.
Individual wires are not practical
and greatly increase a chance of accidental damage to the Pi, due to incorrect wiring.
If you want to build circuits and control them using any Raspberry Pi, including the Pi 400 model,
I suggest using a T-cobbler connector with a 40-wire ribbon cable.
They are usually are included in Raspberry Pi stater kits,
but you can also purchase them from retailers such as Amazon for about the price of 2 fancy coffees.
The T-cobbler is typically installed at one of the ends of a breadboard,
roughly centred over the breadboard's centre line.
I suggest leaving the cobbler permanently in the breadboard -
connect and disconnect the ribbon cable as needed.
The ribbon cable is keyed – it has small protrusions on both connector ends,
which match the corresponding notches on the Pi and the t-cobbler connector.
The ribbon is reversible with this model of Pi.
You can connect it this way... or that way...
I prefer the latter approach as it results in the ribbon being flat on the table.
Make sure to push the connectors in fully at at both ends when you make the connections.
However you arrange the ribbon, you cannot connect the cable incorrectly with Pi 400, which is great!
This is in contrast to the regular Pi, where it is very easy to make the connection wrong,
either by reversing one side of the cable or missing an entire row of headers altogether.
The breadboard should be disconnected from the Pi while you build or change your circuit
and only connected while operating it.
I suggest disconnecting the ribbon on one side only..
Although my Pi connection side is fairly lose and is easy to disconnect,
I prefer to disconnect the cobbler side.
The cobbler is cheap to replace in case I break a connector pin.
The cobbler side typically requires some force so I tend to pull by the ribbon,
gently rocking it from side to side to disconnect.
Let’s build a simple circuit to see how the Pi can operate it.
The simplest possible project is a single LED which can be turned on and off.
Here is what you need:
A breadboard with the t-cobbler installed, disconnected from the Pi for now.
One standard LED, any colour.
One resistor, anything between 220 and 1000 Ohms
And one jumper wire
The cobbler has GPIO pin numbers printed on it, which makes circuit building easy.
You can use any plain port, typically indicated by a number.
Other pins such as SDA or SCL serve special purpose and they would not be normally used
for controlling an LED.
I will use GPIO number 26 in this example.
The circuit is started by grounding a power rail – the long blue connector strip
that runs along the long breadboard edge.
I use a short jumper wire to connect that rail with any cobbler pin that is labelled ‘GND’.
I will come back to the ground rail in a moment.
The LED circuit starts with the GPIO 26 pin and a resistor connected to it.
Resistors do not have polarity and you can connect them either way.
I insert the other leg of the resistor to an available terminal strip away from the cobbler.
The role of the resistor is to protect the GPIO port and the LED from damage
by limiting the electric current that flows through the circuit.
Most LEDs have legs of different length.
The longer leg is called anode, which is the ‘plus’ side,
while the shorted leg is called ‘cathode’, which is the 'minus' side.
This is important, as, unlike resistors, LEDs are not reversible,
and they will not light up if connected backwards.
Normally the ‘plus’ side is oriented towards the GPIO pin,
while the ‘minus’ side is connected to electrical ground.
The longer leg of the LED needs to be connected inline with the resistor.
The electrical connection is made when the resistor and LED anode share a terminal strip.
Pay close attention to it, as it is easy to be one row off here.
The shorter LED leg needs to be connected to ground.
I always try to avoid unnecessary jumper wires.
In this case I will connect the shorted LED leg directly into the ground rail.
As I have connected the blue power rail already to a GND pin of the Pi,
the circuit is now closed and complete.
Double-check your circuit.
Does it look right?
If it does, carefully connect the breadboard to the Pi using the ribbon cable.
Nothing should happen yet, as I write a bit of code to control the circuit.
We can program the Raspberry Pi to control basic circuits in many programming languages,
but by far Python offers the best features while keeping the programs simple and short.
Raspberry Pi OS has a number of editors available out of the box,
which we can use to create and edit Python programs.
I suggest Geany editor, which has a lot of nice features yet it is easy to use.
Python is one of many programming languages we can use to program the Raspberry Pi.
Because of that, we need to start each script with a special line,
which will tell the computer the programming language the script is written in.
This special line starts with ‘#!’, it must be the first line in the program
and it cannot be preceded by anything else, even by a space, in order to be recognized.
A nice thing about Python is that we have access to thousands of software libraries,
which allows us to accomplish many tasks with relatively few lines of code,
instead of writing a lot of code from scratch.
The library that I recommend for programming the GPIO ports on Pi is called gpiozero.
Libraries such as gpiozero are brought into our programs using the import statement.
Here I will use it to import the LED functionality from gpiozero.
Just like that, now my program knows how to operate LEDs on the breadboard.
Each LED used in a project needs to be declared as an object in the program
and initialized using the Pin number it is connected to.
My LED is connected to GPIO26, so the initialization statement reflects that.
Now the LED is ready to be controlled, for example I can make it light up by using the on() function.
Keep in mind that the LED will not come on at this very moment.
For that to happen, I need to actually run the program.
Having said that, the program is not quite ready yet.
Running it now will turn the LED on, but as there nothing in the program after the on() function,
the program will terminate immediately, returning the LED to it’s normal 'off' state.
I would not see the LED come on at all, as all that would happen very quickly.
For the LED to stay on long enough to be seen, I need to delay the program exit.
For example by using a sleep period for a given number of seconds.
The sleep() function is contained in the time library, which needs to be imported at the top of the program.
Now the program is ready!
I can run it by using a menu option,
pressing F5 key or by using the convenient run button.
Here is the completed program in action...
The LED turns on for 2 seconds...
There is a possibility that this program will produce an error on your system,
if your Geany is pre-configured for Python version 2, which is now obsolete.
Unless you are maintaining legacy programs, you need to configure your editor for Python3
to take advantage of the latest Python features available, which gpiozero relies on.
You will find this setting in Geany under the Build menu, Set Build Commands...
If you see only ‘python’ and not ‘python3’ anywhere on this screen,
make sure to change it to python3.
Your program should run correctly after this change.
Of course this is just the beginning!
This is the simplest circuit with the shortest possible Python program controlling it.
This little project should give you the idea what programming GPIO ports with Raspberry Pi looks like.
If you are interested in more complex projects than this one,
my YouTube channel has many videos on the topic,
showing a variety of projects and programming techniques.
Thanks for watching!