0%
Loading ...

Scratch GPIO – Raspberry – First Steps

Maxresdefault

At this post here, a demo of how to blink a Led with scratch on Raspberry will be explained. Although this post is for beginners, it is suggested to have a previous experience with programming in general and programming by python and Scratch in specific.

Learn Scratch

Learn Python – Code Academy

  • Step 01 – Boot you Raspberry Pi

    • Login to your raspberry board using the following defaults:
      • Username: pi
      • Password: Raspberry
    • At the command prompt write startx. This action will start the XWindows on the Raspbian.
  • Step 02 – Install GPIO

    • Open a terminal and write there: sudo apt-get install python-dev python-rpi.gpio.
  • Step 03 – Write a demo program with python to test the GPIO functionality

    • At terminal write: sudo nano /home/pi/Desktop/RPiLED.py, this will open a text editor and will create a file RPiLED.py.
    • At Nano editor write the following python code:

       import RPi.GPIO as GPIO  import time
       def blink(pin):
        GPIO.output(pin,GPIO.HIGH)
        time.sleep(1)
       GPIO.output(pin,GPIO.LOW)
       time.sleep(1)
       return
       GPIO.setmode(GPIO.BOARD)
       GPIO.setup(11, GPIO.OUT)
       for i in range(0,50):
       blink(11)
       GPIO.cleanup()

Exit the nano editor by pressing CTRL+X, Y, and Enter. Right now, running RPiLED.py will cause the LED to blink 50 times.

  • Step 04 – Install Scratch GPIO

    • Open a terminal and write there the following in order to download scratch GPIO library:
      sudo wget http://goo.gl/Pthh62 –O install_scratchgpio5.sh
    • Then write the following to install Scrach GPIO:

sudo bash install_scratchgpio5.sh

  • Step 05 – Load Scratch and write a demo program that Blinks a Led

    • Create the following program at Scratch and run it, notice the broadcast command:

Completed Script

Available GPIO Commands and Pins

GPIO Pins controlled by Scratch GPIO:

The current scratch_gpio_handler.py has the GPIO pins fixed to the following inputs and outputs.  The pin numbers given, are the pins as counted on the P1 GPIO header itself.

Outputs (21,18,16,15,13,12,11)

Inputs (26,24,22,19,10,7)

Broadcast Commands:
Command Alt Command Result
pinXon pinXhigh Turns pin X ON
pinXoff pinXlow Turns pin X OFF
allon allhigh Turns all pins ON
alloff allow Turns all pins OFF
pinpattern1010111 Sets each pin ON or OFF depending on 1 or 0 [21,18,16,15,13,12,11]
Other commands:

You will need to see SimpleSi’s blog post for more information.

Command Result
motorX Runs motor X (A = pin11, B = pin12)
sonarX Trigger input on pin23, X = echo output on an input pin

To use the input pins, see the blog pages for more information.

External Links:
https://pihw.wordpress.com/lessons/rgb-led-lessons/rgb-led-lesson-0-the-absolute-basics-gpio/

Robotics and the Raspberry Pi