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
- Breadboard
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! :)
17 Comments
5 years ago
I'm getting the segmentation fault error. how can i fix this?
Reply 5 years ago
The new version of the firmware nuked fast-gpio, there are a bunch of messages about it on the community hub.
https://community.onion.io/search/fast-gpio%20segmentation%20fault?in=titlesposts
Reply 5 years ago
Are you trying to run exactly the same code?
5 years ago
When I try the code...
Segmentation fault
Segmentation fault
Segmentation fault
Segmentation fault
Reply 5 years ago
The newest firmware nuked fast-gpio, it segfaults constantly.
Reply 5 years ago
I'm using omega2+, and following is working for me,
LED_PIN=1
gpioctl dirout-low $LED_PIN
while true
do
gpioctl dirout-high $LED_PIN
sleep 1
gpioctl dirout-low $LED_PIN
sleep 1
etc..
Reply 5 years ago
Maybe something has changed in the API for the GPIOs since then. Try just turning the light on however they do this nowadays and then it will be a piece of cake modifying the script. :)
5 years ago
Finally got all the equipment for this and was able to blink the LED :D
Thanks so much for the tutorial!
5 years ago
where can i buy the onion omega 2?
Reply 5 years ago
You can pledge for it on Kickstarter!
Reply 5 years ago
so its not avalible yet?
Reply 5 years ago
You better hurry > https://www.kickstarter.com/projects/onion/omega2-5-iot-computer-with-wi-fi-powered-by-linux
6 years ago
A nice tutorial thanks. Now that the console is up and running one can make the file at the prompt with something like:
touch blink.sh
chmod a+rwx blink.sh
Then from the console, go to editor, and look in "root" to find the file blink.sh where you can edit it from an easier GUI environment, or indeed even just paste in your code. The permissions could be tightened up later if desired.
6 years ago
Awesome! Thanks
6 years ago
Great simple tutorial, thank you!
How would you make the omega run the sketch as soon as it is powered on, and without having to plug it to the computer?
Thanks!
Reply 6 years ago
After you have successfully completed the above tutorial, edit the file: /etc/rc.local (for example if you have installed nano you can type nano /etc/rc.local)
And add the full path to your script (mine is /usr/blink.sh) BEFORE the "exit 0" which should be the last line in that file. For example my /etc/rc.local file looks like this now:
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
/usr/blink.sh || exit 1 #blinking script
exit 0
6 years ago
Thanks! Just got my Omega and was trying to figure out how to use it to blink the eyes on a giant spider I'm building for Halloween.