Blinking LED

Within this tutorial we are going to use Scratch and a Raspberry Pi to make an LED blink on and off.

Equipment You Will Need

  • 1 x red LED
  • 2 x male to female jumper wires
  • 1 x breadboard
  • 1 x 330 ohm resistor
  • Raspberry Pi
  • keyboard
  • Mouse
  • SD Card with Raspbian installed
  • HDMI cable
  • Compatible Screen
  • Power Supply

The Circuit

Lets build the circuit. Make sure you have your LED the right way round. The positive leg is the longer leg on the LED, this is represented by the bent leg in the diagram below:

LED Circuit

Code

The first thing we need to do is load scratch. To do this go to menu -> programming -> scratch.

  1. Once Scratch has opened click on the Control menu

  2. Drag a When green flag clicked block to the coding area.

  3. Next we need a Broadcast block. Connect it to the When green flag clicked block.

  4. Now click on the little black arrow and click the next/edit button. You should get a text box like this Text Box. In the textbox type gpioserveron and click OK.

  5. Drag another Broadcast block into the coding area and attach it to the GPIO Server On block. Click on the black arrow and click on new/edit. This time type config18out and click OK.

  6. Next we need a forever block. Attach this under the config 18 out block.

  7. We now need another Broadcast block this time attach it within the forever block. Again click on the black arrow and edit/new. This time type gpio18on.

  8. We now need a Wait 1 second block. Attach this under the gpio 18 on block.

  9. Next we need another Broadcast block. Attach this under the Wait 1 second block. Click on the black arrow and edit/new within the text box type gpio18off.

  10. we now need another Wait 1 second block and attach it under the gpio 18 off block. Click within the white space and change the 1 to a 2.

Your code should look like this Code

What The Code Does

  • When Green flag clicked This tells Scratch to run the code when the green flag is clicked.

  • GPIO server on This tells Scratch to interact with the GPIO pins on the pi.

  • Configure pin 18 This configures pin 18 on the raspberry pi as an output.

  • forever. This makes the code loop forever.

  • GPIO 18 on This tells the raspberry pi to turn GPIO pin 18 on, which is where your LED is attached.

  • Wait 1 This makes the code pause for 1 second.

  • GPIO 18 off This tells the raspberry pi to turn GPIO pin 18 off, which is where your LED is attached.

  • wait 2 This makes the code pause for 2 seconds.

Running The Code

Now that we have finished the code to run it click the green flag. You should now see the LED blink on and off, if you don’t go back and see where you went wrong.

To stop the code click on the red circle

Well done you have just made an LED blink on and off.

Keep Having FUN while LEARNING!