Tuesday, August 11, 2015

Blink - Introductory S4A (Scratch for Arduino) Project

Blink is the first project that we shall try on the Arduino board. This is the S4A (Scratch for Arduino) version of the original Blink tutorial on arduino.cc. The aim is to familiarize the students with the board and using the S4A to interact with it. (Detailed setup instructions for S4A can be found on s4a.cat.)

In S4A (Scratch for Arduino), the Arduino board is treated as a Sprite, and appears on the screen as such. You write code for the Arduino, just as you would for any normal Sprite. If you are using multiple boards, you can open up multiple Arduino Sprites.

While working with a new hardware board, 90% of the time, the first step is trying to light up an LED on the board. Not only does it involve the simplest of wirings, but it also provides a visual indication that the connections are fine and that you can communicate with the board. And keeping with the tradition, that's where we shall start too: light an LED connected to the Arduino.


Aim:  

Connect an LED to the Arduino and try to blink it.

 

Materials required:


  • Arduino board
  • 1 LED
  • 1 Resistor, anything between 220 ohm to 1 K ohm
  • USB cable connecting the computer to the Arduino

Wiring:


The wiring involved is really simple, and is the same as the Blink example on arduino.cc. As shown below, connect the positive leg (the long leg) of the LED to Digital Pin 13 via the resistor, and the negative leg (the short leg) to the GND (Ground) to complete the circuit. Now connect the Arduino to your computer via the USB cable.




The LED does not require much power to light up, and the Arduino board itself can power it. You do not really need a separate power source such as a battery, for this project. The resistor is used to limit the electric current flowing in to the LED and to ensure that the LED does not burn out due to the direct power from the Arduino. The electricity will flow in to the LED from Digital Pin 13 through the resistor and flow out to the GND (Ground), thus completing the circuit.

The Program on S4A:


The Blink code sets the LED on and off. Here is the code snippet on S4A.



We use an event - "Green Flag Click" - to start the program, just as in the regular version of Scratch. Next, in a "forever loop", we include the instructions to turn the LED on and off, by sending a signal to Digital Pin 13, and then switching it off.

The "digital <NUMBER> on/off" instructions can be found within the "Motion" section of the S4A Instruction Set. The "forever loop", "green flag click" and "wait" instructions can be found in the "Control" section of S4A.

As you can see in the code above, we let the electricity pass to the LED and then switch it off, with a delay of 1 second in between. Why the "wait 1 second" between the instructions? Well, if there is no delay in between, the human eye cannot detect the blinking of the light.

Test:


Now, try clicking the green flag on the screen and watch the LED blink.



You have successfully written your first S4A code, and tested out your Arduino!

No comments:

Post a Comment