The Raspberry Pi is a great way to learn DIY technology and programming on a budget. They are also great cheap computers for kids, with lots of useful learning resources to help young minds grasp useful concepts for the future.

There are many great beginner projects out there that use the GPIO (general purpose input/output) pins of the Pi. It’s also great for writing code, as the Raspbian operating system comes with Python built-in. There is even a version of Minecraft for the Pi that can help you learn both beginner electronics and Python!

While this is great for people with some programming experience, what if you want to teach someone how to use the Pi’s GPIO pins without having to learn a programming language?

From scratch, you can .

Today, we’ll be using Scratch to turn on an LED attached to our GPIO pins while learning some basic animation and programming ideas — all without having to type in any code! This tutorial is perfect for getting kids involved with DIY electronics and programming thinking from an early age. Both the video and the article are perfect for home or classroom use.

What you need

start from scratch on raspberry pi

  • 1 x Raspberry Pi with Raspbian installed. Pi 3 is used today, but any Pi will do
  • 1 x LED
  • 1 x 220 ohm or higher resistor
  • 1 x breadboard
  • 2 x wires for connection

LED Setting

We want to set up our LED and resistor on the breadboard like this:

start from scratch on raspberry pi

Here is a diagram of the same setup. Note that in this diagram the LED is reversed, but the circuit remains exactly the same.

start from scratch on raspberry pi

We want to set it up so that the connecting wire from pin 5 GPIO connected to the leg of our resistor . The other leg of the resistor is connected to the positive side of our LED . If you’re wondering which side it’s on, look at the top of your LED. One side should be curved and the other should be flat. curved side positive a flat side negative . Use a piece of wire to connect the negative side of the LED to the pin GND .

Check if the circuit is correct and download the pi! If you’re wondering which pin is which, then our beginner’s guide to the Pi guide guide can help you.

Scratch Basics

To open notepad, click the Raspberry Pi start menu and navigate to » Programming»> «Scratch 2.0» When the scratch is opened, it will look something like this:

start from scratch on raspberry pi

There’s a lot going on here, but it’s pretty easy to get the hang of it. AT left part of the screen is an action. Everything we encode will play in this window.

start from scratch on raspberry pi

Just below this sprite window, where you can load images into your program or draw your own sprites if you’re feeling creative!

start from scratch on raspberry pi

On the middle panel you will find all the blocks that you can use to create your programs. You will also notice two tabs called » Suits and sounds», which you can use to customize your project, but we won’t be using them today.

On right you can drag these blocks to make the magic happen!

start from scratch on raspberry pi

The right side is currently empty. Let’s do something about it!

GPIO pins

Before we go any further, we need to add a few blocks to our toolkit to access our GPIO pins and turn on our LED. In the middle bar, click on More blocks .

start from scratch on raspberry pi

Now click Add Extension and select Pi GPIO . This will add blocks that we can use with our Raspberry Pi pins.

start from scratch on raspberry pi

Now that we have all the necessary tools, let’s create a program!

Light Emitting Cat

Since we already have the cat sprite loaded, let’s use it. We are going to create a program that will make the cat take a step each time the button is pressed, and make the LED light up for one second each time. Start by grabbing the block move 10 steps from the tab » Traffic ” and drag it to the empty space on the right. Now click on the » Additional blocks » and drag set GPIO output to the right and connect it to the bottom of the first block. It should look like this:

start from scratch on raspberry pi

You will notice that my GPIO box has number 5, click on the white circle and enter your GPIO pin number here. If you set up your LED the same way as shown above, it will also be number 5. Now if you click on the code block, it will glow for a moment. This means it is working, so you should see your cat move and the LED will light up. Progress!

Making it more difficult

Now that we have a basic start, let’s add some logic to our code. Right now our light is on and never goes out again. We want him to wait a bit before leaving again. We are going to use the block expectations for this.

On the tab » Control » take block wait 1s and stick it to the bottom of your stack. Now the program knows to wait a second when it gets there. To turn off the LED again, take another GPIO output for lock and drag it down.

This time we want it to turn off the LED by setting the GPIO output to low level . Click on the small dropdown arrow next to output high and change it to output minimum Don’t forget that this block also needs the same GPIO number as above it!

It should look like this:

start from scratch on raspberry pi

Now when you press the code block, the cat should move and the LED should turn on for one second before turning off. Right now this only works when we click on our code block. Let’s make a button to do that instead.

Button, button, I have a button!

We need to click something to tell our cat to move. The arrow should do its job! In the bottom left window Sprites» click the button next to » New sprite» . This will allow us to choose from the sprite library that comes with Scratch.

start from scratch on raspberry pi

We use sprite arrow1, which seems appropriate for our program, but you can use any sprite you like. You can even draw your own sprites in Scratch, or upload images you’ve made elsewhere to use. Once you’ve added your arrow, it should appear in the same panel as your cat on the left. Drag the cat to the left side of the screen and your up arrow:

start from scratch on raspberry pi

We need to give our arrow its own set of blocks. Double click on the sprite’s arrow, you should see that the panel on the right is now empty. We want our cat to run its block of code every time the arrow is clicked.

To do this, take a block, when this sprite is clicked tab Developments . This means that whenever you click on an arrow, its block starts running. Now we need to send a message to our cat when this happens. Luckily, Scratch will let us do just that.

Welcome, loud and clear!

We will send a message to our cat with a block broadcasts . Take it in the tab » Developments ” and place under the block, when this sprite is clicked . This block will send a message to every other sprite in our program. Right now message1 is written, but let’s add our own message by clicking the dropdown arrow next to message1 and selecting A new message . Enter go in the window that opens and click OK.

start from scratch on raspberry pi

Now double click on the cat again. We need to tell the cat to listen to this broadcast message. Drag block » When I get» to the very top of the stack we’ve already made, and make sure the dropdown menu is shown as well. Now, every time you press the arrow on the left bar it passes, the cat gets a turn and moves, and the LED should light up.

start from scratch on raspberry pi

Well done! It looks good! There is only one last thing we can do to make it even better.

Never ending cat story

If you’ve pressed the arrow enough times, you’ll probably notice that your cat has moved away from the right side of the screen. We could just take it and drag it back every time, but good programmers are lazy and they make the code do the work for them. Let’s be good programmers and use blocks to make our cat move on its own.

Drag the cat back to the left side of the screen and make sure it doesn’t touch the edge. Place your mouse pointer in the middle of the cat sprite and look in the bottom corner of the left panel. Will be there x and y, followed by two numbers. Write them down, we’ll need them in a minute.

start from scratch on raspberry pi

Every time our cat moves we want to say this: if you touch the right side of the screen, go back to the beginning . We can use blocks to say this. Start by grabbing the block if then from tab control and drag it under your code blocks. This one looks a little different, it has a diamond gap at the top and a gap in the middle. We use these spaces to tell us what to do.

start from scratch on raspberry pi

Now go to the tab sensing and select mouse pointer touch? block. You will notice that this is a diamond shape that fits perfectly into the diamond gap in the block. if then . If you have trouble installing it, please drag it to the right side first if and then swipe left until you see a diamond-shaped glow. You will also notice that it says mouse Pointer, and that’s not what we need. Use the dropdown menu to select edge .

start from scratch on raspberry pi

While this part of the block says that if cat touches the edges do nothing. Let’s change this.

Back to top

Our if then the block has a gap that needs to be filled. Go to the » Traffic » and select transition to bloc x: y: and drag it to the gap in the block if .

The scratch is pretty smart and will put the x and y numbers where your cat sprite is already placed, but make sure those numbers match the ones you wrote down earlier. If they don’t, change them by clicking on the white squares next to x: and y:.

The complete block of code for your cat should look like this.

start from scratch on raspberry pi

It is now a fully functional program! When you press the arrow enough times for your cat to touch the other side of the window, it will return to the beginning again.

start from scratch on raspberry pi

That’s it, we’re done. Well done!

You can now use Scratch on your Raspberry Pi

Today you created a program that included animation (when the cat moved), homemade electronics (creating and controlling an LED circuit), and some programmer logic to make your life a little easier.

All without having to write a single line of code.

If you are a parent or teacher, there are many great ways to introduce children to and many fantastic starter projects: suitable for young minds. Anything that makes DIY electronics and computing accessible to kids can expand their thinking and bring them into line with the core principles to learn something bigger and better.

Похожие записи