Introduction: Blinking a LED With Onion Omega
Yesterday, I received the Onion Omega, which I backed on Kickstarter a couple of months ago. Onion Omega is a single board Linux computer, running Open WRT and can be easily connected to the internet (it actually has two WiFi antennas). I believe I paid something around 25$, which I consider a fair price, especially after they have deployed their web interface, called Onion Console. This, will enable the Omega to interact easily via the cloud, making it a IoT development platform, full of potential.
Unfortunately, the guys over at Onion, are currently busy with trying to set up the web Console, so not many tutorials can be found, apart of their FAQ page.
Luckily, they have provided us with an easy interface to control the Omega's GPIO pins, so using a very simple bash script, I was able to create beginner friendly "Hello World" example, by blinking a LED lamp.
The materials you will need are:
- Onion Omega
- Onion Omega's Dock
- LED lamp
- Small resistance for the LED
- Jumper cables
Step 1: Prepare the Circuit
Connect one of Onion Omega's IO pins to a small resistance (~300Ω) and that to a LED lamp's anode (the "long" leg). Connect the LED's cathode ("short" leg) to the Onion Omega's Ground.
Use the pictures above for reference. Note that the first sketch is not pin-accurate, but merely for illustration purposes.
Step 2: Setup the Environment
- Follow the official Get Started guide to connect the Onion Omega to your local WiFi and the internet. Linux users should not need to download any drivers.
- Connect to the Omega via shell (instructions on how to do that are included in the Get Started link above).
- Make sure you are running the latest firmware by running in the shell the following command: oupgrade -force
- (Optional) Update the package manager opkg, by typing: opkg update
- (Optional) Install a text editor of your choice, I used nano: opkg install nano
Then you are good to go!
Step 3: Blink the LED
Unfortunately, at the moment the Onion Console is not up and running yet, nor it is clear how to program the GPIOs with a common programming language, such as C or Python, even though, it is promised that this will be possible in the future.
Instead, we will use a common bash script and the fast-gpio interface, that enables the users to easily read and write to the IO pins (read more about it here). Our script shall do the following simple tasks, indefinitely, until we stop its execution:
- Declare a pin as an output.
- Set that specific pin to HIGH
- Wait for one second
- Set the pin to LOW
- Wait for one second
- Go to step 2
First, we need to create a file and start writing into it. We will name it: Blink.sh
If you downloaded nano previously, you can achieve this by typing: nano Blink.sh
Paste the following code into it:
#!/bin/ash ## Simple script to blink a LED on the Onion Omega using fast-gpio ## the variable that will hold the pin number, where the LED lamp is connected LED_PIN=6 ## declare the LED_PIN as output fast-gpio set-output $LED_PIN while true do ## set LED_PIN to HIGH (turn LED on) fast-gpio set $LED_PIN 1 ## sleep for 1 second (equivalent to Arduino's delay(1000) sleep 1 ## set LED_PIN to LOW (turn LED off) fast-gpio set $LED_PIN 0 ## sleep for 1 second (equivalent to Arduino's delay(1000) sleep 1 done
The code should be self explanatory, but let me know if you have questions. It will blink a LED connected to pin number 6.
After you have written the code, press control+o to save the file and control+x to exit nano.
Make the script executable by running the command: chmod +x Blink.sh
Finally, execute the script by typing: ./Blink.sh
If everything went as it should, you should see your LED blinking, every one second! You can stop the script by pressing control+c. That's all! :)