Writing Your Odroid Project in Bash

I’ve purchased my Odroid-N2+ and installed the Ubuntu MATE operating system on an SD card. It works great!

One part of the SBC (Single Board Computer) is the GPIO (General Purpose Input & Output) pins, that can be used for sending and receiving signals to the devices the user develop.

An easy project for begginers can be found in Odroid Wiki wehn you select from the tree on the left side of your browser’s screen:

odroid-## -> application note -> gpio -> wiringpi

(replace ‘##’ by your Odroid model)

Or click here to go to the page.

Start by installing the ‘odroid-wiringpi’ as described at the beggining of the page.

Once you got to the example relevant to your device, you’ll see an image that will help you to locate the relevant pins on your device.

Note: the physical pin number differs from the WiringPi number.

The program you will find for turning the LED on and on is in C. With some changes I’ve made it looks as follows:

    #include <stdio.h>
    #include <stdlib.h>
    #include <wiringPi.h>
    #include <signal.h>
    #include <unistd.h>
    
    void sig_handler(int signo)
    {
        if (signo==SIGINT)
        {
            printf("\n\nExit\n");
            digitalWrite(0,LOW);
            exit(0);
        }
    }
    int main(void)
    {
        /* Connect to signal-handler, so the LED will not stay on
         * when the user presses <Ctrl>+C */
         
        if(signal(SIGINT, sig_handler)==SIG_ERR)
            printf("Cannot catch SIGINT\n");
            
        wiringPiSetup();
        pinMode(0, OUTPUT);
     
        for (;;)
        {
            digitalWrite(0, HIGH); 
            delay(1000);
            digitalWrite(0, LOW); 
            delay(1000);
        }
        return 0;
    }
 
    

You can use the same pin handling functions for other devices such as Raspberry Pi and Arduino.

Now, the hint for translating your program into Bash (GNU Bourne Again SHell) is the command found on the Odroid Wiki page:

gpio readall

The output of that bash commands looks as follows:

In the output you can see the mapping of each physical pin to the corresponding WiringPi(wPi) number to be used in a program, and its mode (for example Input or output). The table can help you decide what WiringPi pin number to choose if you translate a program written for another device into yours. For example, Raspberry PI uses BCM numbering, that does not appear in the table.

While trying to find how to access the pins from a Bash script, I found many examples with files under the ‘/sys’ folder. We don’t need them here. If the command ‘gpio’ takes parameters, then check if you can use others to set the pin mode adn to write to it using the command:

gpio -h

From the output, you can learn that you can set the pin mode, read from the pin or write to the pin using the ‘gpio’ command.

Following is the full script to blink the LED, and turn it off when the user presses

<ctrl>+C:

#!/bin/bash

function brexit () {
  gpio write 0 0
  exit 0
}

echo "Hello, world"
trap brexit SIGINT

gpio mode 0 out
while 
  true
do 
  gpio write 0 1
  sleep 1
  gpio write 0 0
  sleep 1
done

The ‘brexit’ function turns the LED off, and the ‘trap’ command associates it with the signal SIGINT (user break).

Command ‘gpio write <pin-number> <value>‘ sets the pin value to

  • 1 – HIGH to turn the LED on.
  • 0 – LOW to turn the LED 0

Advertisement