XXXXXX
![]() |
| The Raspberry Pi 400, a computer in a keyboard for education and interfacing (Fair Use) |
INTRODUCTION: The Raspberry Pi is a family of low-cost educational computers also suited for embedded (AKA Internet of Things (IoT) applications). The Raspberry Pi 400-- circa 2021/2 -- comes in as a "new" member of the family, with a motherboard housed in a keyboard with a 40-pin GPIO bus as a port on the back of the keyboard. It may be bought with power brick and mouse for US$100 or so. It comes pre-loaded with several computing languages, including Python (the new Basic), Scratch (Lego-style click the bricks together coding, for 7 years old and up), Java, Python and more. Libre Office is present, as an Office Productivity Suite. We even have, by arrangement with Wolfram, a Mathematica installation. Accordingly, this platform is a highly affordable gateway computer for learning to program. That makes it reason-able (note the suffix) to develop a gateway, bridging unit based on this lightweight computer family. And, this is also a gateway to the Maker Movement/Culture, more broadly, which is critical to building up regional technical capacity from the ground up, e.g. consider the Grid Beam as a standard for building structures and 3-D printing as a way to manufacture things for ourselves..
Again, circa 2021/2, we may contemplate:
As an educational note, this gateway unit is meant to support workshops using the Accordion principle, ranging from a sub 1 hour overview, to more detailed explorations:
![]() |
| This unit is like an accordion, that can be compressed or expanded to fit circumstances |
We will start with setting it up (the Raspbian Operating System aka Raspberry Pi Operating System or OS, BTW is a form of Linux) and go on to look at SCRATCH and also Python at Hello World Level. In some ways, this unit will also be a proof of concept demonstration and invitation to the region to standardise basic, gateway access computing education on this platform.
1: Setting up the Raspberry Pi 400
Raspberry Pi Vietnam, we can see the basic layout of the RPi 400. This is built to a computer motherboard in a keyboard pattern. Note, from the box, the SD card will already be installed. Plug in the Power brick, also the USB Mouse, and the Display and you are good to go.
Not quite, notice, it is never advisable to plug in items while power is running (USB etc being exceptions), and that as soon as it is connected to power, the Computer will try to come up. So, get a power strip with a glowing power on light that allows you to have a hard external switch with visual confirmation.
That will be especially important if one is going to interface with external components. A good idea is to use a surge suppression power strip. Preferably, one with protection of all three lines, live, earth and neutral. Of course, be careful to respect 110 - 120 vs 220 or 240 V. 50 or 60 Hz should make little or no difference.
WARNING: If you do not understand these terms, find an adult who does to set up the computer.
Let's expand that:
And, for best results, plug in the monitor separately. See to it that it is on.
Okay, time for another announcement:
![]() |
| Teacher Browne says . . . |
This first section is about setting things up right.
What about displays?
These days, most TV's are effective as computer displays (also called, monitors). If you are using an older monitor, the Raspberry Pi 400's mini to full size HDMI connector can be adapted to old fashioned SVG connectors and DVI connectors. You will need to have an appropriate adapter, which can be bought in some shops that sell computer connectors or online.
Here, are some typical display ports:
![]() |
| Common Display Ports. The Raspberry Pi 400 outputs to a mini-HDMI port, and the provided cord goes to a standard size HDMI, which can be adapted to two common older monitor ports, DVI and VGA. (HT: Xenarc, fair educational use. [Note, a YT video on various computer ports here. Dell discusses video interface technologies and issues here.]) |
From experience, you may need to do a restart of the RPi 400 to get it to display, and the first time, it will ask you to set up for displays etc. This means there is need to learn how to use the F10/Power key. Which, is also important to preserve the SD card from corruption.
Pi Hub explains:
The Raspberry Pi 400 can be safely turned off (and back on again) with the F10 key.
Power Off
Power off by holding down the Fn+F10 keys for two seconds. This carefully shuts down your operating system, so you don’t cause any corruption to your memory card.
Power On
Powering back on is achieved by pressing F10 (or Fn+F10).
This also means it is ill-advised to simply pull the plug, except in an emergency, the Fn + F10 keys should be used to turn it off first.
And yes, that is also a problem for standard PC's, you can damage the hard drive by doing an unplug.
When powering up is successful, as a first indicator, a logo will flash then a little later, it will go to the desktop. The desktop works in the familiar mouse, cursor, point and click framework that has been standard for desktop computers for many years now.
Of course, in the heart of the Raspberry Pi is a Microprocessor, nowadays a quad core, 64 bit ARM derived processor similar to what is in cell phones. That looks like:
![]() |
| A Microprocessor being installed on a Motherboard. See discussion here |
If you want gory details, at first level:
This is the physical layer of the Computer, which hosts and makes use of software etc. We can use a layer-cake model that shows where "apps" come in:
Obviously, the computer interacts with the outside world, including the user. To do so, it has to interface through ports. Display, Keyboard, Mouse, USB, etc are all accessed in this way. Some computers are meant to be embedded in machinery or appliances and others may host target systems such as a robot; as we just saw. The Raspberry Pi is one of these.
That is why, the Raspberry Pi 400 has a 40-pin GPIO Bus, useful for interfacing with external hardware.
GPIO means, general purpose input output bus.
Important note:
The GPIO Bus for the Raspberry Pi 400 is electrically and functionally the same as the GPIO bus for the Model 4:
The bus layout for the Raspberry Pi 400 is electrically and functionally the same, but there is an alternative labelling, Wiring Pi:
Where, the standard GPIO bus pinout is arranged as follows:
That is:
For interfacing work in projects (which we hope to see eventually), it may be helpful to compare the two 40-pin GPIO bus layout labelling schemes and make a few notes on the pins and what they do:
(See a printable, one page version, here.)
We may compare, too, the older 26 pin GPIO bus:
Such an interface bus is a bit of a mixed blessing, however.
For instance, on power up for the system,
" . . by default, all the GPIO pins will be in input mode with a High-Z state or floating state, and this is true with almost all microcontrollers. Keeping the pin in a floating state can lead to leakage current, which may result in high power consumption because a floating pin is highly susceptible to picking up the noise and may result in leakage current."
That can complicate matters in several ways, for instance if it would have been better to have the GPIO pins you intend to use starting in a 0V OUTPUT state. Leakage and noise are invitations to glitches, also. But before we go to more sophisticated solutions, a simple fix is to put a pulldown resistor, 10 k Ohms, from the GPIO pin to ground.
Let's do a preview:
In a circuit with GPIO 5 intended to put 3.3 V across a 220 Ohm resistor and Red LED to ground, that stopped the LED from being weakly ON on power up, with about 20 - 30 mV across the resistor. Less than 3 micro amps.
That will work for our simple purposes, but for more sophisticated interfacing, we need a better solution than this bush league "kludge."
![]() |
| In the tristate condition, the pins act like open circuit switches. Depending on enabled direction signals flow L to R or R to L, allowing two directions but not at the same time. |
So, while it is wonderful to have individually programmable input/output lines, we must be aware that these can easily be damaged if they are over loaded or over driven.
We must be cautious, and where possible I would recommend bus buffering.
For example, the 74LS245 eight line bidirectional bus transceiver can be configured as eight lines one way, eight lines the other way, or as in effect an open circuit, using tristate gates. Now, too, the GPIO lines for the Raspberry Pi use 3.3 Volts as power supply level, and are not compatible with 5 Volt supply digital logic, such as bipolar transistor based TTL.
One compromise solution is to use a diode clamping circuit that will stop a High input to the GPIO line from going much above 3.3 V:
However, there are families of CMOS 74xx00 chips, and for example a 74AC245 is usable with power supply voltage VCC from 2 V to 6 V. This would be useful for designs for systems to work with the GPIO bus, which is of for when you go on to do projects.
For example we may choose to buffer the GPIO lines, perhaps as below, when we go on to do a fairly serious project such as running a drip irrigation controller or a robot or the like:
It might also be convenient to use a bus extender cable that plugs into the GPIO port then allows plugging into a breadboard. Such as:
Beyond this, there are "hats" and other extender boards for the Raspberry Pi, e.g. the Gertboard (manual here, discussion here):
Always, always, double check, to avoid damaging the computer.
On the other end of the interface, there will often be a breadboard with electronic components, but as that can be a little mysterious, let us see:
More details are here in the main source. Where, "breadboard" came from how hobbyists would use a bread cutting board, screw several screws into it and hook up wires and components to test them. There's no need to raid mommy's kitchen, an old termite free bit of plywood would do. You can even mount a protoboard on it and add a power supply. That gives you a poor man's version of today's pro kit, see here or here. We might ponder for example the classic Global Specialties' PB503A:
As you go on to do projects, the following may be helpful resources. Pardon a focus just below on RS Components as a particular case study, that's because they are a personally tested longstanding comprehensive supplier of known consistent high quality:
- Raspberry Pi dot org, a go to for this educational computer.
- Practical Electronics for Inventors, a reference book (there are many others too, this online multivolume textbook can be downloaded as a series of PDFs)
- Jameco as a longstanding, personally tested, reliable supplier of electronics, test equipment etc
- RS Components, ditto
- Maplin, ditto
- (sadly, Henry's Audio Electronics -- once a great source for instrumentation hard to find elsewhere -- is no longer with us as of c 2016)
- (As frustrating, Elenco no longer offers assemble it yourself electronics instrumentation kits like this classic XP-720 power supply, just children's educational kits.)
- B&H for Cameras, Video etc
- Sweetwater is a very useful source for audio, music and related things (including some video etc).
- Radio Shack may be helpful, here.
- Old fashioned telephone book sized catalogues from suppliers are now hard to find, even as PDFs, try here for a first guide to components for electronics, etc and try here for the RS Pro Catalogue as a flip book, with R,C,L etc passive components here, semiconductors (discretes and IC's or "chips") here, digital logic ICs here, memory chips here, processors and microcontrollers here, amplifiers and comparators here, clock and timer IC's here, op amps here, sensors here.
- RS on test and measurement is here.
- The author of this unit recently purchased a Hantek 2000 series scopemeter for about US$ 120, and is initially impressed; this is an unusually inexpensive, 40 MHz bandwidth two input channel instrument. Consider here, here for a broader range and note a manual here. He suggests, buy a separate digital multimeter and a separate signal generator, even though this scopemeter has meter and signal generator features. (NB: The classic, analogue Avometer based on the galvanometer instrument, is now regarded as obsolete, some digital meters give a pseudo analogue scale.)
- RS on Raspberry Pi etc is here.
- A gateway to products and catalogues is here.
- Here is a general guide to components for design.
- A PDF format, recent general selection guide to semiconductor components is here and Fairchild has some advice here.
- Wikipedia surveys the huge 74xx00 digital logic IC family here and the 4000 family here.
- Here is a survey of the TTL, 74xx00 family of IC's that raises points of concern for designs that have to deal with detailed technical limitations and issues.
- Unfortunately, there is no one standard for codes for semiconductor electronics parts [vacuum tubes/valves do have a standard code], the NTE cross reference is a useful source, and note they bought ECG some years ago. This old ECG replacement Guide at Web Archive may be helpful, but note, the 700 p. PDF is just short of 1 GByte..
- Wikipedia -- yes this can be helpful on technical topics -- as a gateway to Mechatronics
- The ARRL Radio Amateur's Handbook can be helpful too (even an old free for download edition such as this, the first one the author of this note bought as a student)
- Gridbeams as a construction system
- The Maker Movement may be a useful source for ideas
- Hello World Magazine is an excellent source for Raspberry Pi for education and making, notice its books page, especially The Big Book of Computing Content and The Big Book of Computing Pedagogy, as well as its Issues on topics, all free for download and keyed to the UK ages 5 - 19 computing initiatives.
- MagPi Magazine is a similar rich source on all things Raspberry Pi, likewise with free for download issues. The Raspberry Pi Handbook is a good start. Of course, the various issues are well worth browsing, e.g. Issue 120 on Robotics, Issue 121 on learning Electronics with the Raspberry Pi Pico W, Issue 117 on smart gardening, Issue 114 on practical programming, etc.
- There is of course The Raspberry Pi Beginner's Guide, a MagPi book.
- Do not overlook Ebay and Amazon, etc
- Note, for suppliers and information sources alike, a longstanding reputation for quality, reliability and value for money counts (especially when resources are tight and one cannot afford to get some things wrong)
This gives us enough that we can turn to:
2: Scratch language, programming and the Hello World first case
![]() |
| Teacher Browne talks about Scratch as a first programming language . . . |
![]() |
| The Scratch Language's Logo |
The Raspberry Pi 400 comes with the original version, version 2 and version 3.
We will use version 3.
To do so, let us look at a YouTube video for a basic program.
Traditionally, we begin to code by making a "Hello World" program. As Scratch is a game-oriented highly visual language, that will be a little more involved than would be so for more traditional languages. Like, Python or even Java.
However, that's fine, as the example lets us catch the flavour of Scratch. Here is a Hello World exercise:
FIRST EXERCISE: Let's code a "simpler" Hello World than we just saw, using the Scratch Cat.
a: Once the Raspberry Pi is hooked up and turned on, click on the Raspberry Icon to show a drop down menu.
b: Go down the menu to programs, and click on Scratch 3
c: When it opens up, notice the Scratch cat in the stage area, a coding section in the middle and to the left coloured buttons that will display menus of code blocks.
NB: The 100+ Scratch Code Blocks are described here.
d: Below the stage on the right is a window pane with a blue button; as the pointer goes over it, it says choose a backdrop. Choose and click on Rio Beach.Now, the cat should be walking on the sand at Rio!
e: The centre column will show in its top right, a "thumb-nail View" of the beach and on the stage, the beach will be highlighted in blue. Point to the cat on the beach, and click on it.
f: The thumb-nail cat icon appears top right of the coding pane and the cat icon in the sprites pane is highlighted in blue. We are now ready to code for actions of the cat. Point to the Events -- paler orange -- button at left and click on it. Some choices will appear.
g: Point to When [flag] clicked, and click-drag it to the middle of the coding pane. Release it there.
h: Click the control button, bright orange. Click-drag Repeat [10] and put it just under the When [flag] clicked block. release it there.
i: In the "mouth" of the Repeat [10] times block, there is a "mouth" where other blocks can fit, these will say what is to be done [10] times. Click-drag the wait [1] seconds block into the mouth until it is ready to click in place. Release it there.
j: Click on the Looks, dark Purple, button. Notice it has a say [Hello!] for [2] seconds. block. Click drag this to the mouth just above the wait block, it will stretch. release it there.
k: Now, change some options: click on repeat [10], delete the 10, and type in 5. Change the Hello time to for 1 seconds similarly. Make the wait 0.2 seconds.
l: You are now ready to run your Hello world code. The words, EVENTS. LOOKS etc after the dashes are the sets of buttons used, not part of the code. The actual code is in Courier text, as is usual in textbooks, coding cookbooks etc. Also, the code is highlighted. So, the code (without the button set comments) should look like:
when [flag] clicked -- EVENTS
repeat [5] times -- CONTROL
say [Hello!] for [1] seconds -- LOOKS
wait 0.2 seconds -- CONTROL
(end loop)
m: Put the pointer on the green flag just above the stage pane, and click on it. Watch the stage.
n: Change Hello! to Hello World! Click the green flag again, the message is now Hello World!
o: Change 5 to 3 for repeats, and run again. Count the repeats, it should now be three.
p: Change the wait to 2 seconds and run again. Does the wait between Hello messages become longer? It should.
q: Now, change the wait to 0 seconds, run again. Does the message blink now then go out after three flashes? It should.
r: Lastly, click on the wait icon, and delete it, then run again. Does the Hello Message seem to be on constantly for 3 seconds? it should.
s: DISCUSS: What are you learning about block code instructions and programs?
t: CLEAN-UP: Delete the blocks one by one, and click the background tab then click on the trash can icon to delete it. (We are not ready to save programs yet.)
SECOND EXERCISE: Let's code the example from the video. Well, let's modify it, to see how animation of a character works
a: Open up Scratch, making sure the code pane is clear, then go to the sprites pane and delete the cat.
b: Go to the stage pane at bottom right, and click the choose a sprite button. Pick, Champ99, as in the video. Use the Size menu item in the sprites pane to make him be at 50%.
c: the funny sounding word in the video -- English is not our presenter's first language -- is "Costumes," a tab above the blocks. Click on it, you will see a long string of poses for our Champ99. Delete all but the first two, showing him with arms down and up. Notice the labels, that will be important. Click drag to make sure the arms down champ image is on top of the other one.
d: Click on Backdrops and put up the stars backdrop.
e: Look for an Earth sprite and add it to the pane. Adjust so Champ stands on the earth.
f: Now, select champ with arms down. Click the events, light orange button and click-drag into the code pane, when [flag] clicked.
g: Go to the looks button and click it, it is blueish purple. Click-drag switch costumes to [the arms up champ] Add this under the flag block. Use the labels Scratch provides, you can change the name as was done in the example video.
h: Click the bright orange Control button, and click drag repeat [10] times. Remember, this has a mouth, so we can make a loop. Place it under the switch costumes block.
i: Go back to the looks button and click it again. Click-drag Say [Hello!] for [2] seconds. Click it into the mouth of the repeat block.
j: Go back to the Control button, and click it. Drag into the code area, wait [1] seconds. Put it in the mouth or the repeat block, just under say hello.
k: Go back to the Looks button and click it. Click drag switch costume to [champ with arms down] and put it under the repeat block. This means it will be executed after the loop is completed. Make sure it will switch back to the arms down Champ image. [this will allow you to see animation in action.]
l: Now, set repeat to 3 times, there is no need to do 10 or even 5. Set say hello to 0.5 seconds, and wait to 0.1 seconds. The stacked code should look like:
when [flag] clicked -- EVENTS
switch costumes to [the arms up champ] -- LOOKS
repeat [3] times -- CONTROL
Say [Hello!] for [0.5] seconds -- LOOKS
wait [0.1] seconds -- CONTROL
(Loop closes)
switch costume to [champ with arms down]-- LOOKS
m: Now, go to the green flag and click, we should see the champ put his arms up, say hello three times then put his arms back down again.
n: this is a mini game with animation, congratulations. We also see how code attaches to a sprite and that the sprite can change its look.
Let us observe:
1 -- Scratch comes up with a full framework for us to work with, including
- a coding pane and
- a software window, looking unto
- a software stage which can host actors or objects, called "sprites."
- If you want the gory details, here is the Scratch interface (see discussion here):
. . . and, here is the underlying approach:
Where, of course, windows often have panes:
![]() |
| Here's "Scratchie"! |
3 -- We see that we can delete and change our sprites to what we may prefer.
4 -- In the code pane, we see that . . . just like we do with Lego type bricks . . . we can click together a stack of successive action steps for our program, selected from a menu. As in:
![]() |
| A stack of Lego-type blocks (from Lego and Duplo) showing various sizes. (HT: Fredley et al, at Stack Exchange, fair educational use.) |
5 -- These allow us to take steps in sequence and to branch and loop.
6 -- That means, Scratch allows us to carry out the three main programming structures, sequences, branches and loops.
7 -- We also see how the green starting flag can be used to start up a program, by clicking on it.
8 -- A lot of things must be going on in the background to let those things work, as the gory details show.
9 -- Don't forget, saving the project.
Some folks may find a chart of the basic programming structures helpful:
(The fourth structure, CASE, is actually a chain of branches.)
Yes, we just figured out together what is happening in the Hello World programs and with the Scratch Language.
But on the Raspberry Pi, we can do a lot more than code little games.
For, as there is the GPIO bus, we are able to put real world things on our software stage and interact with them.
This is interfacing.
3: Scratch Language and Interfacing through the GPIO Bus (using a LED case study), part 1:
Computers interact with users, programmers, networks and also various real world things such as robots.
In fact, some computers are "embedded" in various machines we see, including modern cars and trucks. Also, the display, mouse, keyboard and printer are external devices the computer interacts with. To do so, it uses ports and special arrangements for interfacing, such as ports and their protocols for communication. A protocol, here, is a standard arrangement for communication. For example, TCP/IP is transmission control protocol/internet[work] protocol. And yes, the famous Internet is a global network of networks. Modems, Bridges, Routers etc are devices that allow us to connect to it, through Internet Service Providers.
In short, interfacing is a big deal.
Let's dip a toe in with our second Scratch-based coding example.
So, we now turn to interfacing, with a Light Emitting Diode [LED] as a first case study.
Oh, one preliminary, language:
Now, let's watch:
(Yes, this is more or less the same technology in LED lights and LED flashlights. You may choose to only watch and discuss the video for now [or do this as a class demonstration], until you are confident that you can safely do actual hardware interfacing.)
So, let us note:
1 -- The external circuit is meant to drive a Light Emitting Diode [LED], and there is a current limiting resistor, calculated to be within the safe load range of the GPIO lines.
(We are told, each GPIO pin should not be the source of or sink more than 16 mA, and the total for the GPIO bus's pins should be no more than 54 mA. But we are using just one GPIO pin -- GPIO 5, to work as a source of current for the LED when it goes high to 3.3 V.)
As we can see:
We need one more step, to solve the "GPIO pins are set to input, floating high on power up" problem.
Here, a 10 k Ohm pulldown resistor will do the trick:
That should be enough for now.
2 -- The LED, as suggested, has two pins, A -- Anode (+) -- and K -- Cathode (-) -- and generally drops about 1.8 - 4 V depending on its colour. A red LED drops about 2 V, the GPIO output is about 3.3 V when high, and 1.3 V across 220 Ohms, by Ohm's Law, V = I R, gives a current of about 5.9 mA, a reasonable value. The current limiting resistor, of course, protects the LED from drawing too much current. With a 10 k Ohm pulldown resistor across the same output as just seen [it is shown in red], adds about 0.3 mA to the load. Quite acceptable.
(BTW, Resistors come in a range of standard values -- see table -- and use a standard colour code.)
If you are still concerned, a 470 Ohm resistor would limit current to about 2.8 mA which should still be bright enough.
If you want gory details:
| A Chart of LED colours and voltage drops (HT: CircuitBread dot com, fair use education) |
3 -- More details: the two LED terminals are marked, often with a FLAT or a notch on the plastic housing next to the K terminal, and in a new LED usually the A terminal is longer than the K terminal. (Of course, terminals can be clipped off with pliers so that is not reliable.) Another way in some cases is to notice which terminal is like a 7, with the LED chip mounted on it, that will be the K terminal.
![]() |
| Light-Emitting Diode, showing structures (HT: Wikipedia, fair use Edu) |
4 -- On the Breadboard, clipped from the above video and labelled:
5 -- Of course, circuit connections are to be made with the power OFF, including here, the onward connections to the GPIO Bus for the Raspberry Pi. (See the discussion above.) As a reminder:
6 -- We then add the power lead terminals,
a: + power in line with the leftmost lead of the Resistor . . . here, green . . . and
b: GND (-) to the the rightmost, K lead of the LED . . . here, the blue covered lead.
7: The other ends of the leads go
a: + power to GPIO 5, pin 29 [label with a flag using a bit of making tape],
b: GND (-) to GPIO GND, pin 30 [label, similarly].
c: a 10 k Ohm pulldown resistor perhaps should have gone from power + to GND also.
This is because he is using a Model 4, for a Model 400, pin numbers are also counted zig zag style. The electrical functions of the pins are the same but there are two different ways to label, see the chart above. This can cause some confusion, so let is clip the video and mark it"
8: Wire up a LED driver circuit, with a pulldown resistor. Double check, and have a supervisor cross check to make sure that things are correctly wired up. (Note, there are some differences with number of GPIO bus pins and arrangement of pins for different members of the Raspberry Pi family, adjust what you do accordingly.)
9: Now, we go to how we will drive the circuit, under control of a Scratch Program.
4: Part 2, Scratch coding to drive the LED circuit:
At this point, our Raspberry Pi has been successfully and safely connected to an external object, using a PORT, the General Purpose Input-Output Bus, GPIO. Scratch Language on the Raspberry Pi has special statements for working with that bus and thus with things tied to it. As we continue to work with the video, we will see it in action. And, we will be able to further explore programming the computer.
In continued steps, once the Raspberry Pi has been turned on and the Desktop opening screen is in place:
10: Go to the Raspberry Pi Screen, which has a Raspberry Pi Menu - the round button with a raspberry on it) with various programs including several Computer Languages. Click on the Menu button to open up and drop down the selection of options. Notice, how many possibilities there are, you may wish to explore that LATER.
11: In the Video Scratch 2 is then selected, we may have a computer with Scratch 3 which has a similar interface. The Scratch 2 interface is like this, complete with our friend, Scratch the cat. It also has a menu of Scratch coding blocks that we will select from to build our Scratch Program:
12: Go to More Blocks and add the GPIO extension to the core Scratch 2 language. This allows us to set output pins such as GPIO 5 to high or low, which will drive the external LED circuit. This is a vital preliminary step:
When you open up Scratch the language, enable GPIO as an extension to the base language, Scratch.
13: The main job here is to have a "button" on the screen, that then causes the LED to light up or turn back back off, using the Scratch language.
14: To get the button sprite, move Scratchie to the side, then go to the lower pane and drop down the sprites menu. In the video, the narrator selects then clicks okay for an arrow key sprite.
15: It appears next to Scratchie and it is selected and dragged off to the top of the panel.
16: The cat icon is chosen and from the code block menu, he picks one that makes the cat move forward ten steps for when the arrow key is pressed.
17: Similarly, from more blocks, he picks and drags the block that will turn on the GPIO pin 5 high and sticks it under the move cat block. (This means the actions will be done in sequence but it is so fast it looks like the same time.) Also, it is set to make the output go high. High voltage is about 3.3 V for the Raspberry Pi. This is enough to drive the LED circuit.
18: To make the LED stay on a second and go off again, from the control menu, a wait one second block is picked and stacked. Coding has now reached three lines of code.
19: Drag another copy of the output pin but choose it to do output low. Fourth block.
20: Next, to enable the arrow button, he selects it and picks an EVENT block. he puts it on screen and adds another, broadcast just below. It is set up to send the message go to everything in the display window.
21: Going back to the cat, he selects another event block that makes the cat move when it receives go. This he stacks on top of the four blocks for the cat. Here is the code, notice, two blocks of code, one for each sprite and Go tells Scratchie and the LED to act:
Notice, how we access GPIO code blocks:
Notice, too, the available blocks and how they are colour coded:
![]() |
| Scratch coding blocks. For more details on the language, try here and here for more details (but for Scratch 2 not 3). Here, is a Wiki for Scratch. .(HT: Wikipedia) |
22: At this point, if you click on the arrow button, the LED will light up for one second and turn off, also Scratchie will move forward ten steps:
23: However, Scratchie will march his way off the pane once there are enough clicks made so more needs to be added. What is added next will cause Scratchie to loop to the left side of the screen and start walking right again.
24: He selects an IF-THEN block, puts it after the turn on turn off sequence of steps and adds in its "diamond" [for making decisions] another block, changing it from mouse to touching edge of screen.
25: He puts in another motion block to fit between the arms of the If-THEN block. It is a go to the left edge. On a test, it works.
26: At this point, he has set up a little "game" in which Scratchie moves and the LED lights when he clicks the arrow button. This could be done over and over as long as he likes.
The final code [with selections in square brackets] is:
PRELIMINARY STEPS:
Make sure the computer is turned off . . .
then, set up the LED circuit with pulldown resistor and interface it to the GPIO bus,
where GPIO5 (pin 29) is + and GND (pin 30) is - electrically, 0 V:
(When GPIO 5 goes high, it applies +3.3V, when low, 0 V or ground;
yes, this actually is earth potential from the Mains power supply)
Turn on the computer
When Scratch 3 is opened from the Raspberry Pi Menu,
go to the leftmost pane, with the coloured buttons
go to the bottom where a square blue button will allow you to get extensions
click on this button
You will see a menu of extensions
click on Raspberry Pi GPIO (the Icon looks like a Raspberry Pi model 4)
This will enable interfacing and you will see some Green Icons and a Raspberry Pi GPIO button (This extends the base language to use the GPIO Bus)
Add a background if you want one
Also, add an arrow sprite
CODING STEPS:
FOR THE ARROW SPRITE BUTTON (add this sprite)
when this sprite clicked -- EVENTS
set gpio [5] to
output [low] -- Raspberry Pi GPIO
FOR SCRATCHIE (this sprite is automatically there when Scratch opens up)
when I receive [go] -- CONTROL
move [10] steps -- MOTION
set gpio [5] to
output [high] -- Raspberry Pi GPIO
wait [1] secs -- CONTROL
if {touching [edge]} then -- CONTROL, touching from SENSING
EXECUTING STEPS:
Place the cursor on the arrow button and
click the mouse.
repeat, until Scratchie marches off the screen
What happens as you press the arrow button once then over and over, why -- discuss.
This pair of short programs will be triggered each time the mouse arrow pointer is clicked on the arrow button. In this final form, as the video shows, Scratchie will walk ten steps right and the LED will come on for one second then turn off. This will repeat each time the button is clicked, until the cat reaches to the edge. When this happens, Scratchie will then pop back up at the left edge so we can click over and over; getting a repeat of the walking and LED flashing.
So, we actually have a miniature computer game that has been coded in Scratch! One, that interfaces to a real world target system, a driver circuit for a LED light. (Later on we will be able to drive a robot or a drip irrigation controller, etc.)
We can take in the bigger picture of how the computer system works as it interfaces with you the user and a target system it hosts . . . for the moment, a humble little LED circuit:
We can do this for ourselves, and work through carefully to see that it works.
If you have trouble and cannot figure it out, call a supervisor.
However, pin 29 on the Model 400 is GPIO 21, adjust code for that. That is, make your code refer to GPIO 21 going high and low instead of GPIO 5. That way, you can still use pins 29 and 30.
Now, we could go on from here with a whole study of Scratch programming if we wished to.
This brings out our approach: this is a gateway tutorial so you can know what to do to get started.
![]() |
| Training Wheels |
To go on from here, download the Official Raspberry Pi Beginner's Guide -- here -- and use it to guide you in doing more with Scratch as well as other activities. Soon, you will be ready to do many other things on your own, this is like training wheels on a bicycle. (If you got a kit, the Guide may be in it as a paperback book, and the PDF is actually accessible from the Raspberry Pi on screen Menu.)
For the 4th Edition, the Scratch 3 Tutorial is on p. 54. It is Chapter 4.
EXERCISE: Ask yourself, if we can code LEDs to go high or low and can make them stay on for a fixed time, could we use the GPIO pins to control drip irrigation for a mini garden or even a field? How could that be done, and how would you figure out when, how often, what time of day, how much water to drip for a given crop? Could this be done using Scratch language? Discuss. How would you go about planning and carrying out such a project? Compare:
![]() |
| A simple drip irrigation controller (HT: Aqualin) |
However, we also want to move on to a more typical kind of language, Python.
5: First steps to Python programming for the Raspberry Pi
For this, let's use the Beginner's Guide. Here, we go to p. 92, Chapter 5. You can actually click on the hot link in the Table of Contents. Here's how it looks:
Once we click on the Raspberry Pi icon, top left of the screen, with a Raspberry on it, a menu opens. Under programs, we will find an "Integrated Development Environment" (IDE), Thonny. Thonny allows us to compose and run Python 3 programs, and opens up in a "simple" educational mode that looks like:
Here, we see a Toolbar, a Script Area and a Shell. The Shell allows interactive execution, here of a Python 3 Hello World.
NB: Python is so important for the Raspberry Pi that it is part of its name. Raspberry is a fruit name, as is common in computing. Pi is short for Python. The simplest way to say it is that Python is the new BASIC . . . Beginners' All-Purpose, Symbolic Instruction Code, which was the original educational computer language. And yes, there is an actual raspberry pie, too. (For fun, the story of Basic is here.)
26: Type it in, in the Shell:
print("Hello World!")
Then, press the ENTER key.
You should see Hello World! in the next line, unless you made a mistake in typing in.
You have written and executed your first Python 3 program on the Raspberry Pi.
27: Now, use the mouse to move the pointer to the script area, and click so you see the I-beam cursor for text entry, then type print("Hello World!") in the Script area. Press ENTER again. Ask yourself, why doesn't it execute the one line program? Why does it go to the next line as though it expects you to key in another line of code?
(ANS: It does expect just that. And, unlike the interactive Shell (which accesses the Python Interpreter that "reads" and executes), the Script area expects you to save the script you are writing as a program, then call for a Run. Let's go there next.)
28: Notice, there is a RUN button/icon in Thonny's Toolbar. It is the right-facing arrow-head in a Green circle. Green, for "Go." Click it. As the Guide we are following says, Thonny will ask you to save the program, try "Hello World1" or something like it. We suggest a number code like that that tracks versions. Notice, it saves and runs the program, as we can see:
You have now run Python programs in Interactive and Script modes. That's a lot you have seen how to do, already.
29: Second Program, Indentation. Here, we implement a loop and use indentation as part of the way Python controls execution. To start, click on the Green-Plus-Sign, "NEW" button, and notice how a new tab appears in the Script area. Type as the first two lines . . . this is a Sequence coding structure:
print("Loop starting!")
for i in range (10):
Yes, after you typed the print instruction, press ENTER to go to the next line. Type it in
The first line instructs, print the message in the ( . . .) jaws of the print operation. Then, as step two, the program sets up the framework for a loop to follow, which will be indented. The colon says, details follow.
Note, range (10) is a Python Instruction that means, start with counter variable, i set it to i = 0 and count up by ones, to one short of 10, that is 9. This give us ten values of i.
30: Now for the loop. When you click ENTER now, Thonny indents four spaces for you. Instructions in the loop are "nested." This loop is short, just one instruction:
print("Loop number", i)
This will print several lines as the loop number counts up 0, 1, 2 . . . 9. Ten times.
31: After typing in the third line, likely you pressed ENTER again, if you haven't, do so now. Notice, Thonny has politely indented again. However, that's not needed, backspace just once. To do so, use the keyboard's BACKSPACE key. This tells the Interpreter, the loop ended.
32: Now, key in the fourth and final line:
print("Loop finished.")
33: Press RUN in the toolbar and save the program, say as Indentation1. It will save, notify in Shell and run. There will be several lines of output, again in the Shell. Loop starting appears once, then Loop number 0, Loop number 1 . . . Loop number 9. Finally, Loop finished. The next line will have the triple arrowhead >>> saying, Python is ready for a new input.
Note, you saw a sequence of instructions run, with decisions and branching to form the loop.
The loop implicitly tests for i less than 10 and runs the loop if that is true. it adds 1 to i behind the scenes and goes back to test the value of i. When i has been incremented to 10, the branch fails, the interpreter goes to the next line.
That final line tells the interpreter to print the final message.
Of course, the print instruction has got a real workout. There are many others, those you can pick up as you go.
And, hiding details behind the scenes makes Python simpler to learn, but we also need to pick up on what is going on too. (That's why Java is somewhat better for learning the Computer Science aspects. Later, that's the 3-credit course this is an entry and basic bridging unit for! You might want to go through the Python Unit after this one. Of course, for practical programming, there is a world of Python programming and useful aids.)
34: As a third program, following the Guide, let's look at decision-making and branching a bit more. Where, if you happen to be named Clark Kent, please use a nickname or John/Jane Doe or something. To start, press the NEW button, and type in:
userName = input("What is your name? ")
Note, userName is a variable, and it uses "Camel Case." Yes, coding does strange things with rules of Grammar. input is a new operation that will set the value of userName. The single equal sign means give the value on the right hand side to the variable on the left hand side.
Variables are, basically, tagged memory locations in the computer.
35: Save this program as Name Test1, by clicking the RUN button again. Notice, in the Shell area you are asked, What is your name. Type it in and press ENTER as usual. Nothing visibly happens, but the variable is set up and the name you typed in is assigned as its value.
36: Now, we add the "secret sauce." Type in the following lines that set up a decision and alternative actions depending on the branch taken:
if userName == "Clark Kent":
print("You are Superman!")
else:
print("You are not Superman!")
Now you see why Clark Kent is so unusual. Also, did you notice that weird double equal sign?
Note, the double equal sign means, test the value of the variable to see if it matches, exactly, Clark Kent. If so, it goes to the nested line. If not, it goes to the else and does what the nested print instruction there says. We here see explicit branching and decision making. There are other Comparison operators, less than, greater than, greater than or equal, less than or equal and most unusual for a newbie not equal !=. Part of the Python vocabulary. As angle brackets or "arrow head" keys can do odd things with HTML, let's clip an image from the Guide's text:
37: Let's do two runs. First, click RUN, and type your name. It will print that you are not Superman. Click RUN again, and now type in the magic value, Clark Kent. Bingo, the output now says you are Superman. And yes, all that stuff about Jor El and Kal El is Hebrew, Superman is a bit of a takeoff on the Christmas story. You can save using the save icon.
38: Let's skip the adjustment to != in the Guide, the alternative code will keep on asking for your name until you type Clark Kent or click the red stop button on the toolbar. On your own, you can do that as Name Test2.
Yep, that's part of why we use a version number. And yes, this step by step tutorial approach makes it easier to begin to learn but eventually you will have to take off training wheels on the bike.
39: For the third Python coding case study, let us do a Turtle bot exercise, which will require importing from the provided Python Library, using the import instruction. Create a new tab using the Thonny toolbar, then key in as first line in the script area:
import turtle
40: To avoid the annoyance of having to do a full library reference to library dot library instruction, we can assign a variable, here a name for our turtle, pat:
pat = turtle.Turtle()
41: Next, insert a temporary, test instruction line:
pat.forward(100)
Note, this will make the turtle move 100 pixels, drawing a line. Sorry, there is no turtle sprite. Originally, turtles were turtle-like robots with real pens that could do pen up, pen down, move, thus drawing on say a sheet of paper. Now, Python is allowing us to do a software version with the turtle being a one pixel dot.
42: Click RUN, and save as Turtle Snowflakes1. A new window, titled, "turtle graphics" should appear, and a line should be drawn of length 100 pixels:
43: Minimise the turtle graphics window . . .
(just as you would on a normal PC -- except, that UP is a V-symbol and DOWN is an inverted V, an arrow head pointing down)
. . . and when the main Thonny Window appears, click STOP to stop the turtle graphics window. You are now ready to edit the program. First, delete temporary line 3 and replace it with:
for i in range(2):
pat.forward(100)
pat.right(60)
pat.forward(100)
pat.right(120)
Note, this should draw a parallelogram, using a loop similar to the loop count exercise above. This is what is sometimes called a for next loop. Of course forward(100) draws a line. right(60) turns the direction of pat to her right by 60 degrees, that is, clockwise. The second forward(100) draws a line at 60 degrees to the first, giving is half of the parallelogram. Turning right 120 degrees now reverses pat's direction and on the second run of the loop, she draws the second half of the parallelogram. Where, Geometry tells us, this will be a rhombus.
44: Save, and click RUN. Another turtle graphic window will pop up and we now see a parallelogram. In the next step, we will use this to draw a ring of ten parallelograms, creating a flower-like snowflake shape. (You can do the other bits and pieces on your own following the Guide. For the last bit of this tutorial, we want to go on to a LED exercise.)
45: To do the full snowflake, insert a new line just after the line that defines pat. This line sets up an outer loop, to draw the ten parallelograms in a ring. Of course, we will have to do some indenting to get the two nested loops to work right. To insert the line, click on the end of the second line, press ENTER on your keyboard and type:
for i in range(10):
46: To close the loop, scroll down to the end of the program and add another line, indented four spaces -- press the space bar key four times first:
pat.right(36)
Note, 360 degrees divided by 10 is 36 degrees, this is what will give ten petals to the snowflake. (Real snowflakes, of course, tend to have six, tied to the structure of the water molecule.)
| An actual snowflake (HT: Kljatov. Fair use, education.) |
47: To set up correct nesting, click on the start of each line between these two instructions, i.e. the 4th to the 6th lines, and press the space bar four times. The resulting code should look like:
import turtle
pat = turtle.Turtle()
for i in range(10):
for i in range(2):
pat.forward(100)
pat.right(60)
pat.forward(100)
pat.right(120)
pat.right(36)
48: Save, and RUN. If you got this fairly complex program right, you should see a bit of an amazing result in the turtle graphics window:
49: Now, for the fourth coding exercise, let's first set up our LED circuit on the breadboard just as was done for the Scratch case, but don't try to plug it into the Raspberry Pi 400 GPIO bus. Remember, double check and have the tutor cross check. Then, watch the tutorial video here:
The Python 3 code, as shown uses GPIO 17, but we wish to use GPIO 5, to keep the same circuit we used for the Scratch program:
LED_PIN = 5
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
try:
while True:
NB: To trigger a Keyboard Interrupt, press at the same time, CONTROL-C keys.
50: This exercise was set up for the single board computer version of Raspberry Pi (useful for onward projects!) but needs to be adjusted for the 400. Notice, how careful the demonstrator is about not causing damage by making live connections. Remember, from earlier how to turn off the Raspberry Pi 400 safely. I would be more concerned about repeatedly unplugging and re-plugging the SD card than the demonstrator is, and for the 400 it is nicely protected in the back of the keyboard. Again, you may choose to only watch and discuss the video for now [or do this as a class demonstration], until you are confident that you can safely do actual hardware interfacing. A reminder, again:
51: To take this a bit further, showing how LED lighting can go on to more complex interfacing, watch this video, and this video. Take note, they use an older version of Python, 2.7.x, which is not fully compatible with Python 3. How do you think it could be modified for Python 3 running on the 400? Why?
WHERE TO GO FROM HERE
This unit is a gateway to programming unit, and as it starts with SCRATCH, it can be used by 7 - 11 year olds "of all ages." Scratch is more than good enough to learn the first steps of computer programming, as we saw above. Indeed, the visual approach is a "prettified" version of the Nassi-Schneiderman chart approach to structured programming, and in recent years, there has been work done to use it for "serious" programming, its ability to work with the GPIO bus speaks for itself.
MIT has created an online home for Scratch, where you can register and do much more. The introduction to programming and the Raspberry Pi book also has further information.
The Raspberry Pi also introduces Python, which allows you to build on the unit section above. This course has another gateway unit which focusses on Python, too; based on the PC (probably the most common computing platform in the Caribbean and wider world, not counting smart phones and the like). That Unit then points to where you can go in the Python world. If you are going to do more or less personal coding, Python is a perfectly good and heavily supported language.
Then of course, the Raspberry Pi also introduces Java, which is introduced in the Raspberry Pi book. A discussion of Java on Raspberry Pi is here. It is worth the pause to read an excerpt on why Java may be preferable to Python (and to C) from the just linked article by , as you are not likely to run across such sobering thoughts and insights elsewhere:
. . . I'll limit the discussion to "professional grade" programming languages that target standalone programs, support multi-tasking, allow hardware and network access, etc. That means Java, Python, or C/C++ . [--> comment: that already says a lot] The most recent releases of Raspbian include development and runtime support for all these languages.
Python is in effect "promoted" for use on the Pi, at least in part for its ease-of-learning for new programmers.[--> comment: hence, "Python is the new Basic"] I am an experienced programmer and am not fond of Python due to an admittedly subjective dislike of a number of its characteristics, for example, dynamic typing. There is one characteristic of Python for which my dislike is objective -- performance. This source [--> see wayback machine, here] shows that Java is always faster than Python 3, and in fact can run hundreds of times faster, depending on the task.
C in my opinion, is a "high level machine language", i.e., something that is very close to the system hardware, or at least the operating system. You can do pretty much anything, and you can do it about as fast as possible; in fact, this source suggests C can run up to 7X as fast as Java. I am also not fond of C since I (subjectively) dislike some of its characteristics that I find arcane, archaic, or both; for example, explicit pointers. Objectively, the fact that you can do anything means that you are only an obscure bug (e.g., bad pointer arithmetic or an errant memcpy) away from overwriting memory and potentially crashing the program or even the entire system. Java prevents such hazards, so Java is safer. I feel the safety also improves programmer productivity. I consider C++ an object-oriented "wrapper" around C that does nothing to eliminate the hazards of C.
The bottom line: Java is faster than Python. Java is safer than C.
Food, for thought.
This course goes on to the main, Java-based part, starting with Unit 1. That main part of the course, is Java based as Java is not only very popular but helps us learn the frameworks of Computer Science by making the scaffolding fairly explicit, starting with the famous magic words for a typical Java Hello World, "public static void main(String args[])". And, as GregF10 warns, "Java is faster than Python. Java is safer than C."
Those magic words "public static void . . ." draw out many key issues in programming, languages and computer science, especially for object oriented languages.
Now, too, the Raspberry Pi is a very useful device for embedded systems and the like. The following will be helpful for onward work:
- Explaining Computers' Chris Barnatt did a very useful first, simple robots exercise using Python on the Raspberry Pi, using the Pololu Zumo Robot tracked vehicle chassis. Part 1 (GPIO bus introduction, driving LEDs -- similar to the above excercises), part 2 (setting up and running a robot), part 3 (wireless keyboard control), part 4 (control over a network using SSH), part 5 (line following).
- A similar Zumo Robot Chassis is on offer at Amazon, here. Metal motor-gear sets are here.
- More recently, Chris has done a survey of ten useful, real world applications for the Raspberry Pi that are well worth pondering.
- There are bare bones, low cost robot vehicle chassis kits (usually with plastic geared motors etc) available on Amazon and via eBay; of course, buyer beware, choose a reputable supplier and watch where they ship from. (See a case here. This gives details on motor driving.)
- What about robot arms? The Pi Arm was a six degree of freedom robot arm set up for the Raspberry Pi. There is a Yahboom arm here that might be suitable for a class demonstration unit. (Look at an industrial arm, here and here.)

Mini Mars Rover - Nikodem Bartnik has developed a "mini Mars Rover" robot with an arm, here. This includes his

An Omnidirectional Wheel,
with wheels within wheels
(cf. vid on Mechanum Wheels;
also see here, here)
design adventures to get a viable arm. Notice, he uses omni[directional] wheels, literal "wheels within wheels" that can move sideways. (Think about what it would -- and did -- take to scale that sort of vehicle up to real Mars Rover size.) - He has also developed a small plot garden watering system, using a Raspberry Pi Pico. It has sensors and data logging. NB: backyard and front-yard drip irrigation system installation, here. Animation for a more complex small farming system, here. Handbook, here. Notice the Open Sprinkler Pi expansion board here, also here, see vid, a more detailed page here, the Open Sprinkler here, getting started guide here. A study on mulching here.
- He spent a year developing his Indy Mill, a CNC milling system that he for example used to mill printed circuit boards (which is a lot less messy and hazardous than the usual chemical etching system, cf. here). See another case here, and a 3d printer case here. (This opens the door to industrial production.)
- The Raspberry Pi can be made into the heart of a media centre, try here and here for initial ideas. MagPi 102 on media centres, is a third ideas sparker.
- The Grid Beam (cf. here, book here, vid), is a useful construction system for prototypes and one off items of larger scale, using wood or metal with a 1.5 inch base dimension unit. Doorway to the Maker Movement.
- There is also a Lego Technic (cf. tutorial) compatible, open source 3d printable [cf. here, vid] miniature bitbeam version, cf. here, here, here, here, here, here. Notice, some initial explorations, especially on dimensioning, 8 mm was chosen as basic unit. Introductory talk points. NB: bitbeam is currently niche but is aptly scaled to initial exploratory and educational exercises and small kit, e.g. a tablet computer stand. 3d printing is a transformative technology that makes it practical, anywhere.)
- On 3-d printers, a review is here. Wiki overview, here.
- For a glimpse at the high end -- aircraft -- try here (see here on actuators), and for robots try here.
- And, more.
So, the gate being open, let us go through it.
cccc
xxxxx











































