dark mode light mode Search Menu
Search

Control an Arduino using Scratch

Stephen Chin on Flickr

FOR THIS PROJECT YOU WILL NEED

  • A computer running Windows
  • An Arduino Uno or compatible
  • The Arduino IDE installed
  • A breadboard
  • 1 x Push button
  • 1 x 10k Ohm resistor (BROWN-BLACK-ORANGE-GOLD)
  • 1 x 220 Ohm resistor (RED-RED-BROWN-GOLD)
  • 1 x LED
  • 4 x Male to male jumper wires

The Arduino range of boards have been available for over a decade and they offer a consistent and dependable platform for projects great and small. Children can learn electronics using Arduino, factories can use them as part of industrial processes, the Arduino is just that flexible!

But what if you have never programmed an Arduino before? Is it easy? To a new user the Arduino editor and language is rather tricky at first, just like any programming language. But what if we could use something instantly familiar to new learners and children? Something like Scratch?

Scratch for Arduino, (S4A) is a version of Scratch with special blocks that we can use to write code to directly control an Arduino Uno (or compatible) connected to our computer via USB. You will be instantly at home using the Scratch interface, and will soon be able to write code to control electronic components.

In this project we will learn

  • How to create a toggle button to control an LED.
  • How to create a variable, store data inside of it, and update that data.
  • How to create an electronic circuit to control an LED with a button.
  • How to read the status of a button (input) and use that to update a variable.
  • How to create tests that will check if the user has pressed a button, and if the LED is already on.

Getting Started

These hardware steps need to be repeated when setting up this project. Firmware and Scratch for Arduino software only need to be set up once, as described below.
Configure the Arduino Board

As shown in the diagram above, the hardware build for this project involves connecting the long leg of an LED (Light Emitting Diode) to pin 13 of the Arduino using male to male jumper cables, and connect the short leg to GND on the Arduino via a 220 Ohm resistor (RED-RED-BROWN-GOLD) to limit the amount of current that the LED can consume.

We then connect a push button to the breadboard so that it sits over the centerline on the board. The bottom left pin of the button is connected to 5V on the Arduino, the bottom right pin is connected to GND via a 10K Ohm resistor (BROWN-BLACK-ORANGE-GOLD). The top right pin is connected to pin 2 of the Arduino and has the default state of false, this means it is turned off. This is due to using the 10K Ohm resistor connected to GND to act as a pull down resistor, ensuring that the pin is always false until we press the button and connect the bottom left pin to the top right, sending 5v to pin 2 on the Arduino and changing the state to true.
Download Scratch and Firmware for Arduino

On our computer and the first step is to install Scratch for Arduino (S4A). Click here and click on Downloads. Click on the download link for your operating system. While we cover Windows in this tutorial, there are options for Mac and Linux.

Once downloaded, double left click on the downloaded file and then double left click on the S4A16 file to start the installation. Also from the Downloads section, download the firmware for the Arduino as we will need this later.

The next step before we can write any code, is to install and run the Arduino editor, full details of which can be found here.

Install the Arduino Firmware

Once installed, open the Arduino application and connect your Arduino which will trigger the application to detect your board.

Click on File >> Open and navigate to where the firmware file was downloaded to. Select this file to open and the Arduino application will ask you if it can create a folder to store the code, select yes / ok.

To install this firmware on the Arduino we need to go to Tools >> Board and ensure the correct Arduino model has been selected, in this case an Arduino / Genuino Uno. Then go to Tools >> Port and ensure the correct port has been selected. If in doubt, you can use the Windows Device Manager to identify the correct COM port. However, the Arduino application is usually very good at automatically identifying the port.

Once everything is set, click on Sketch >> Upload to install the firmware. Once complete our Arduino is ready to communicate to S4A and we can close the Arduino editor.

Please note that these steps do not have to be repeated as the Arduino remembers the firmware even when powered off.

The Scratch for Arduino Interface

Open the S4A application and you will be presented with a screen similar to Scratch 1.4. But in the top right of the screen, called “The Stage” we see an Arduino board, and a series of numbers rapidly changing. The Arduino represents your Arduino, and the numbers are the values of the pins that we can control.

The interface is identical to Scratch 1.4 and we can see that the blocks used to code are on the left side, the coding area is in the center, and the sprites and stage on the right. The main difference between Scratch and S4A is that, in the Motion section of blocks, there are added blocks for working with the Arduino.

Coding with Scratch for Arduino

We start the code for this project by dragging the When Green Flag Clicked block from Control into the coding area. This is the trigger to start our code. Then we go to Variables and click on Make a variable, call this variable status and then drag the set status to 0 block over to the coding area so that it connects to the previous block. These two blocks will act as a trigger to start the project, and reset the variable to 0 as that variable is used to set the status of the LED with 0 being off and 1, on.

To continually run our code, we need to use a Forever block, found in the Control section. Drag this block and connect to the previous. Inside of the Forever block we will add If, also found in the Control section. This is used to check If a certain action has been performed, in this case has the button been pressed? The check is constructed of a _ = _ block found in Operators which is placed in the empty block space of If. The two blanks are where we first place a value of sensor Digital 2 block from Motion into the first blank, then in the second we type true. So when the button is pressed, the value of the pin it is connected to changes from false to true, and this is the trigger to do something.

Inside of the If we need to add another check, and this time we use If..Else from the Control section. This is placed inside the previous if. In programming, this is called nesting. The new check needs to be told what to look for. In this case, we are asking it to check the value stored in the status variable. If this value is 0 then it will activate some code. Using the _ = _ block from Operators placed in the same shaped space that is part of the If..Else block. Then from Variables we drag the status block and place it into the first blank space. In the second space we type 0 (zero)

If the status variable contains the number 0, then that means the button has never been pressed and that the user wishes to turn on the LED. So from Motion we drag the digital 13 on block and place it inside the If section. With the LED on we need to tell the code that we want to keep it on, and so we change the value stored in the status variable to 1 using set status to 1 block from Variables. To ensure that the user has enough time to press the button before the loop repeats we add wait 1 secs to the code. Change the value to give you enough time to press and release the button, 0.1 is pretty quick, so change it to 0.3 if you wish.

But what if the LED is already on? Well that means the user wishes to turn off the LED so we need to use digital 13 off from Motion then set the value of status variable to 0 using set status to 0 from Variables, then we repeat the wait 0.1 secs to delay the loop long enough for the user to press the button. Using one button to turn on and off the LED is called toggling and it is very common in the real world.

So what have we learnt?

We have learnt that we can use one button to toggle an LED on and off using two logic checks. The first check being “Has the user pressed the button?” and the second “Is the LED on or off already?” We also learnt to save the status of the LED (on/off 1/0) to a variable and update that status each time the button was pressed.

    • How would you control two LEDs at the same time?
    • How could you have one LED on, and the other off?
    • What other forms of input can we use to create a toggle?

Learn More

Introduction to Arduino with Scratch

https://www.instructables.com/id/A-Gentle-Introduction-to-Arduino-for-Scratch-Users/

Scratch/Arduino extension

http://khanning.github.io/scratch-arduino-extension/

Get kids started with Arduino

https://www.stemmayhem.com/get-kids-started-with-arduino/

Scratch for Arduino

https://www.instructables.com/id/Physical-Computing-Scratch-for-Arduino/

Scratch for Arduino using mblock

https://www.mblock.cc/en-us/blog/mblock/try-scratch-arduino-using-mblock/