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.

google ads placeholder

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