Blinking LED with Linux or Hello World with iMX233-OLINUXINO


Image

Today I did my first Linux shell script with iMX233-OLinuXino. I used to use Windows for many years, then recently moved to Ubuntu but never had to deal with shell scripting till now.

We should make GPIO testing bench to do functional test of the iMX233-OLinuXino GPIO ports, so I had to learn some new stuff.

Raivis installed so called SYSFS package in the OLinuXino Linux kernel which allow you to access the GPIOs from the linux shell so I had to read some more about Linux shell scripting to may make the board GPIO tester.

After reading about SYSFS it seems pretty easy to manipulate the GPIOs from the shell.

To make GPIO1 output and set in “1” for instance this should be executed:

echo out > /sys/class/gpio/gpio1/direction
echo 1 > /sys/class/gpio/gpio1/value

and to make GPIO2 as input and read it could be done with this script:

echo in > /sys/class/gpio/gpio2/direction
echo cat /sys/class/gpio/gpio2/value

echo will return 0 or 1 depend on GPIO2 state

All gpios can be seen with

$ ls /sys/class/gpio

So my first thing was to attach LED to PIN9 (CONNECTOR.PIN19) which correspondents to gpio0 in Linux (yes I know it’s confusing, there is one cross table and Linux kernel assign the numbers not exactly as per our schematic :-o )

Then run VI and created my first linux script:

echo out > /sys/class/gpio/gpio0/direction
while [ 1 -eq 1 ]
do
echo 1 > /sys/class/gpio/gpio0/value
sleep 1
echo 0 > /sys/class/gpio/gpio0/value
sleep 1
done

saved it as “gpio” and made it executable with

$chmod +x gpio

then executed the script with:

$./gpio

as expected the LED start blinking with 1Hz which you can see on the video: http://youtu.be/4FvNwmmj_X8

20 Comments (+add yours?)

  1. Morgaine
    Apr 23, 2012 @ 17:15:25

    Very cool, good progress!

    By the way, you don’t need “[ 1 -eq 1]” as the conditional expression for infinite loops, just use “true”. :-)

    Reply

  2. Kiril Zyapkov
    Apr 23, 2012 @ 18:03:33

    Nice!

    Although the LED is blinking at 0.5 Hz :)

    Reply

  3. Greg
    Apr 23, 2012 @ 22:19:47

    Are you sure it should read
    echo cat /sys/class/gpio/gpio2/value

    I would think the correct line would be
    cat /sys/class/gpio/gpio2/value

    Reply

  4. Greg
    Apr 23, 2012 @ 22:22:05

    And I /really/ have to get me one of these little beauties :)

    Reply

  5. sagt3k
    Apr 24, 2012 @ 09:03:09

    I am very interested in communication via I2C, you could show me some examples? What distribution of Linux you are using?

    Reply

    • OLIMEX Ltd
      Apr 24, 2012 @ 09:39:31

      please go to Freescale web and download the L2.6.31_10.05.02_ER_docs.tar.gz documents there is described LTIB and the Linux support package, all iMX233 peripherials have implemented drivers: SPI, I2C, UARTs, Video effects, etc etc.

      Reply

  6. Jason
    Apr 27, 2012 @ 07:08:14

    FYI there is a unix command “true” that is more typically used for infinite loops in shell scripts. It’s just a program (or shell intrinsic) that always returns 0. So a more idiomatic infinite loop would start “while true”

    Reply

  7. Per Cramer
    Aug 02, 2012 @ 14:11:31

    Nice little sample. I installed the arch linux distribution and it is running nicely. But any hints on how to work with the i2c?

    Reply

  8. Trackback: iMX233-OLinuXino working with GPIOs « olimex
  9. flavigny
    Oct 08, 2012 @ 17:44:42

    I am interrested in connecting some Leds to the gpio pins. I read your post, you do’nt wrote where is the other leg of te led ? watching your photo, it appears to be 5V or Ground. For dummies; may be you will update to indicate pin 40.
    I Also note that you do’nt talk about an in line resistor (as for LED1 integarted) ?
    Regards
    PO.

    Reply

  10. Lukas
    Jan 30, 2013 @ 23:36:15

    Thanks, very helpful post. I am having this problem, though:
    I attached an LED to pin18 on the board, which corresponds to gpio17 in Linux. It’s value is currently 1, so the LED is on.
    When I try to set it to 0, with:

    echo 0 > /sys/class/gpio/gpio17/value

    the board immediately crashes (shuts down). Do you have any idea what could be the problem? Thanks!

    Reply

  11. Carlos
    Feb 08, 2013 @ 20:22:26

    Hi everyone,
    I am interested in connecting more LED in another GPIOs and I am having a hard time to figure the LINUX GPIO numbers.

    I know that:

    CONNECTOR PIN9 = LINUX GPIO0 = iMX233 PIN26
    CONNECTOR PIN18 = LINUX GPIO17 = iMX233 PIN14

    What about the others GPIOs, does anyone knows what is the correspondence between LINUX GPIO number and iMX233 pin number?

    Thanks

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 514 other followers

%d bloggers like this: