Last time, we got to making the LED shine, so now let’s try to make it flash!
There are various ways to do this, but since I’m sick of the black LX Terminal screen…
I want play with this kitty!
It’s the character of the pre-installed software named Scratch, who appeared on the Raspbian install screen. This time we’re going to use Scratch to make the LED flash!
We briefly touched on Scratch in the previous post, “Raspberry Pi DIY Case & Overview the Pre-installed Softwares”, but let’s go over it one more time!
Scratch (Programming language) – Wikipedia
Scratch is a visual programming language. It can be accessed as a free desktop and online multimedia authoring tool that can be used by students, scholars, teachers, and parents to easily create games and provide a stepping stone to the more advanced world of computer programming. It can also be used for a range of educational and entertainment constructionist purposes from math and science projects, including simulations and visualizations of experiments, recording lectures with animated presentations, to social sciences animated stories, and interactive art and music.In addition, a new version, “Scratch 2.0”, was released in May 2013. This new version was a web application, eliminating the need to download any software.
If you click on “Create” in the top menu of the website, the Scratch web application will be displayed. You can try it out straight away without even registering. Instructions for using it are displayed on the right side of the screen. They are next to the animation and are very easy to understand. So, it’s probably a good idea to go through them before starting working with the Raspberry Pi.
Also, if you click on “Explore” you can see many works created using Scratch. They respond to clicking and typing, so you don’t just watch them. You can actually try moving them around in the browser. I’m so happy that you can instantly enjoy games and such with Scratch!
Raspberry Pi Learning Resources
This is a tutorial from the Raspberry Pi website on how to use Scratch and GPIO!
STEP1 is lighting up the LED, so I’ll ignore that and start from STEP2!
And then, I immediately ran into trouble.
I thought to myself, “Scratch is a pre-installed software, I can use it right away!” But it turned out that there are different versions of Scratch, and to use GPIO you need a separate package called “ScratchGPIO”. After looking carefully, I noticed that it was actually written in the “What you will need” section of this tutorial.
Well then, I’ll install “ScratchGPIO”!
Scratch GPIO by cymplecy
I installed ScratchGPIO by following the instructions in the documentation and referring to the above page. I installed it easily by just executing two commands.
1 2 |
wget http://goo.gl/Pthh62 -O isgh5.sh --no-check-certificate bash isgh5.sh |
The installation only requires these two lines!
A shortcut should appear on the desktop… Well, there should be, but it was nowhere to be found, so I searched and it was in the pi user desktop folder! I tried double clicking on it, but it wouldn’t start, and I couldn’t see it in the start menu. It turns out that to start it up you need to log in as a Pi user again.
/home/pi/Desktop
There are two shortcut icons “ScratchGPIO5” and “ScratchGPIO5plus”. I couldn’t find much information about “ScratchGPIO5plus”, but it seems to be used with add-on board (expansion card). The standard version “ScratchGPIO5” works just fine on my Raspberry Pi.
After logging in again as pi user, click on the “SCRATCHGPIO5” shortcut icon.
Now, you can use GPIO!
I’m starting with the wiring. The necessary parts are the same as those I wrote about in the last post. This time, I wired it like the below figure!
I wired the GPIO15 Pin 10 and the GND pin 20.
Now, let’s write the script in Scratch!
The script for GPIO is in the Control tab. It will be a “broadcast signal” command, so drag the block with “broadcast” written on it in the middle of the script page.
Next, designate what kind of signal you will send.
If you click on the list box in the block, a menu will appear and you can enter a new command, or choose one you have previously entered. This was my first time, so I clicked “new” and an input box appeared. Then, I entered my command in it.
In the tutorial, pins are specified by their number, so we’ll define them in the same way. This time, I have wired GPIO15, so it’s pin number is “10”. The command is to “Turn on pin 10 (GPIO15)”, so I enter ” “pin10on”.
You can also double click blocks to choose a command and execute it! If it’s executed correctly the LED should light up! (We haven’t executed the command to turn off the light yet, so it just stays lit up but…)
It’s lit up now. Next step is to make it flicker. Let’s try to make it twinkle!
You can understand how to do it without a detailed explanation. I tried to set it up on my own using the ScratchGPIO5 command guide as a reference.
As the blocks get close together, they connect and create the processing flow. I connected the repeat process blocks to enclose the contents. The blocks corresponding to the start and the end of the script only “indent” to one side, making it easy to understand, even for people with no programming knowledge.
To make the flashes easy to see, I added sleep, so it would repeat ten times.
Since I was using the Scratch program, I made Scratch the cat, which is set as the default sprite, the launch condition.
Now, when I click on the cat the Scratch LED twinkling program starts!
Making the LED twinkle was a success!!
(The orange breadboard was given to me by a superior. It’s so cute. I really like it!)
We’ve made the LED blink, but I’m not used to specifying pin numbers yet so it’s annoying. So, I thought “I want to use the GPIO names instead!” Then I looked it up, and found a way to do it!
The below script makes the LED light twinkle ten times just like before. But this time the script I’m using is “Variables”! You can control the GPIO pins through setting the variable names as GPIO names.
Like command lines, the set values are specified by “0” and “1”. I think it’s much easier to understand!
You can share what you’ve made with Scratch online. Select “share your project on the network” in the “share” tab in the menu, and a box in which you can enter necessary information for sharing your project will appear.
To log into the Scratch website, “Your Scratch website login name” and “Password” will be needed. It’s convenient because I already registered beforehand. Let’s add the “Project name” and the “Project notes”.
The upload will start after clicking on the “OK” button. When the upload is finished, you will be able to access to your project on the website.
After logging in open the “My stuff” page, you can see your projects!
If you click on the “See inside” button, you can view the script.
You can also directly edit the script in your browser.
This is the project page. If you click on the green flag icon, you can confirm the changes to the values of the variable “gpio15” displayed on the stage. I adjusted the script so that when the cat is clicked on, the processing details are displayed in a speech bubble.
You too can download the script, so please try it out with your own Raspberry Pi!
*Some information was brought to my attention, so allow me to correct myself.
You can download the script, but since the web application version of Scratch uses version 2.0, it is not compatible with the current Raspberry Pi (Version 1.4). To make it work properly with your Raspberry Pi, you need to create the script like in figure 12!
*Editing note: Scratch projects are displayed using Flash. So, I’m afraid that those of you viewing this on iOS (iPhone, iPad, etc.) will have to use a PC, etc., that uses Flash Player to view projects.
My project was successful as I could make an LED blink! And I had fun playing with Scratch!
Scratch is visually easy to understand. I was able to use it without any detailed instruction manual. Because you can complete whole programs through just assembling blocks, not only it’s a good learning environment for those new to programming, it’s also convenient for checking the operation of the wiring! Now, that I’ve tested my electronic kit a little, I think I can use it again in the future!
Well then, what should I do next time? I don’t have time to think about it. Anyway, like always I’ll to follow my boss’s orders.
I know, I’ll create a “print server”!
What’s a print server? (I get the feeling I’m making one without even really knowing what it is). Anyway, continuing on from the NAS, I want to make another tool with Raspberry Pi to make things easier in the office!