RPi-08: LED Hello World

In this tutorial, we’re going to be use Processing running in Raspberry Pi to control an simple LED. The idea being that you could use this tutorial as a starting point in order to get a basic understanding of physical computing through Raspberry Pi, Processing and electronic circuits. This tutorial will explain the code behind the Processing program line-by-line. After this tutorial, you should have a grasp on how basic programs operate and be able to modify our code.

First you’ll need to install Processing.

  1. Installing Processing on Raspberry Pi:
    curl https://processing.org/download/install-arm.sh | sudo sh
    (you’ll need your RPi to be connected to wifi to download Processing from the internet)
  2. Wire up an LED and a 330 ohm resistor to GPIO pin 17, as shown below. If you need more guidance, follow this guide.

    led-gpio17

  3. In your RPi, run Processing (in the main Programming menu) and create a new sketch with the following code:
    import processing.io.*; // use the GPIO library
    
    // store the desired state of the LED in a variable
    boolean ledOn = false; 
    
    void setup() {
      // set pin 17 as an output:
      GPIO.pinMode(17, GPIO.OUTPUT);  
    }
    
    void draw() {
      if (ledOn == true) { // If the desired state is on, then:
    
        // turn the LED on:
        GPIO.digitalWrite(17, GPIO.HIGH);
    
        // and set the background red:
        background(255, 0, 0);
      }
    
      else { // otherwise:
    
        // turn the LED off:
        GPIO.digitalWrite(17, GPIO.LOW);
    
        // and set the background black:
        background(0, 0, 0);
      }
    }
    
    void mouseClicked() {
      // When the mouse is clicked, store the opposite of 
      // ledOn into ledOn, which toggles ledOn:
      ledOn = !ledOn;
    }
  4. Save and Run your Processing sketch. When you click in the window, the background will turn red and the LED will turn on. When you click again, they will both toggle back to their original state.
  5. Now Try wiring up more LEDs and see if you can control them independently by clicking on different parts of your sketch window.

Content for this tutorial from Raspberry Pi website:

https://www.raspberrypi.org/learning/introduction-to-processing/

Leave a Reply

Your email address will not be published. Required fields are marked *