RasPiCommPlus Logo:Controlling a LED-Array with a potentiometer

In this sample project we will write a small application that allows us to use a slider to control a LED-Array. It is designed for use with the RasPiComm+, now available for preorder on Indiegogo!


Required Parts

If you want to follow along you'll need the following parts:

PartRS Stock Number
Raspberry Pi8111284
RasPiComm+ MainboardTBA
RasPiComm+ 8-Port Output ModuleTBA
RasPiComm+ 8-Port Analog Input ModuleTBA
LED-Array2465689
Standard Type Slide Potentiometer2499402
Breadboard / Resistors

Setup the RasPiComm+

  1. Plugin the Analog Input Module in first slot
  2. Plugin the Output Module in the second slot
  3. Execute the Setup Script to configure your RasPiComm+ as follows:
    sudo ./rpc+setup.sh

In this example, we assume that you plugin the Analog Input Module in the slot 1 and the Output Module in slot 2 - you may choose different slots, but you will have to adjust the code to take the different module slots into account.

You may want to view the getting started video for the RasPiComm+

Setup your Bread board

Setup your breadboard according to the following schematics:

LEDArray project Schematics

Configure the Outputs

Before we can use the outputs to change the LED-Array, we need to initialize them in linux first.

Linux represents all GPIOs as consecutive numbers, starting from 0 and going to as many as your hardware supports. So if we want to tell Linux to set the output pin of an module, we need to use the correct number. The raspberry pi itself reserves the first 54 GPIOs for itself, which means that the GPIOs of the RasPiComm+ start at 55 and going up.

The following table shows the Linux GPIO to Output Pin mapping:

Output PinLinux GPIO
Output 055
Output 156
Output 257
Output 358
Output 459
Output 560
Output 661
Output 762

Now armed with this knowledge we can configure the GPIOs as outputs in linux. This is a simple two-step process for each output. First, we'll export the gpio, that is: tell Linux that we want to access the GPIO from the commandline and second configure it as an output, that is tell Linux that the GPIO in question is an output (and not an input) so that we are able to set it's value.

For example, to configure GPIO 55 we need to:
  1. export the GPIO:
    echo 55 > /sys/class/gpio/export
  2. configure the GPIO as an output:
    echo out > /sys/class/gpio/gpio55/direction

After executing those two commands, the GPIO 55 - which corresponds to the first Pin of the Output Module - is set to 0 (low).

Screenshot of setting up the GPIO

Test your setup

Before we write the application that reads the current slider value and sets the LED-Array, we will verify our setup to see if it's working as expected.

To do this, we execute a few commandline commands to see if the slider and the LED-Array is working.

First, we'll verify the Slider:

  1. Move the slider to one of the outer positions
  2. Then execute the following script:
    cat /proc/rpc+/module1/analoginput0

    We expect to read back a value near the minimum (0) or the maximum (2^12 = 4096)

  3. Now move the slider to the other opposite position and execute the script again:
    cat /proc/rpc+/module1/analoginput0

    Confirm that the value returned is near the opposite side of the scale

Screenshot of the Analog Input test

Second, we'll verifiy the LED-Array

  1. Set the GPIO 55 to 1
    echo 1 > /sys/class/gpio/gpio55/value
  2. Now we check that the first LED is on.
  3. We can disable the LED again by executing
    echo 0 > /sys/class/gpio/gpio55/value

Write an Application

Now we're finally setup to write a simple application that can take advantage of our hardware setup!

Pseudo Code

The following pseudo code will represent the application:

      

      main() {

        /* initialize the gpios */
        ...

        /* loop forever! */
        while (true)
        {
          /* read input from slider */
          ...
        
          /* using the slider input, compute the outputs that we want to set */
          ...

          /* set the outputs */
          ...

        };
      }
      
    

An Implementation using Bash script

We now create a bash script, that you can download here, named led_array.sh with the following content:

      
  #!/bin/bash
  # RasPiComm+ Example Project 
  # prints general info
  function print_info() {
    echo "RasPiComm+ Example Project 1"
    echo "Controlling an LED-Array with an potentiometer"
    echo ""
  }
  # exports the supplied gpio and configues it as an output
  function init_gpio() {
    if [[ ! -d /sys/class/gpio/gpio$1 ]]; then
      echo $1 > /sys/class/gpio/export
      sleep 0.5 # insert a small delay, as the gpio is not instantly available and the direction change may fail
    fi
    echo out > /sys/class/gpio/gpio$1/direction
  }
  # sets the specified output high
  function set_output() {
    echo 1 > /sys/class/gpio/gpio$1/value
  }
  # sets the specified output low
  function clear_output() {
    echo 0 > /sys/class/gpio/gpio$1/value
  }
  # initializes the gpios
  function init_gpios() {
    echo "initializing gpios"
    init_gpio 55;
    init_gpio 56;
    init_gpio 57;
    init_gpio 58;
    init_gpio 59;
    init_gpio 60;
    init_gpio 61;
    init_gpio 62;
  }
  # sets the leds according to the supplied value
  function set_led() {
    local value=$1
    local led_count="$(( ($value / 500) ))"
    # debugging statement 
    #echo "-> led_count: ${led_count}"
    local index=0
    while [[ $index -lt 8 ]]
    do
      if [[ $index -lt $led_count ]]; then
        set_output $(($index+55))
      else
        clear_output $(($index+55))
      fi
      index=$(($index+1))
    done
  }
  # read the value of the analog input into the variable VALUE
  function get_analog_value() {
    # we read the value of the of port 0 of the analog input in module 1
    VALUE=`cat /proc/rpc+/module1/inputanalog0`
  }
  function main() {
    # print the startup info
    print_info;
    # initialize the gpios
    init_gpios;
    echo "reading analog input and setting outputs..."
    # keep looping forever
    while [[ 1 ]]
    do
      get_analog_value;
      local analog_value=$VALUE
      # when debugging, uncomment the following line to view the analog value
      #echo $analog_value;
      set_led $analog_value;
      # when debugging, uncomment the following line!
      # sleep 1;
    done
  }
  # main entry point
  main;

      
    

Using the Application

Then we start the script

./led_array.sh

and see the following output:

Screenshot when starting the script

Now we can use the slider to control the LED-Array!

Additional Resources

If you have questions or feedback, please check out the following Resources:

ResourceUrl
IndieGoGo CampaignIndieGoGo Campaign
RasPiComm+ Support ForumOfficial RasPiComm+ Support Forum
Twitter Github Tumblr PInterest Youtube