Getting started with the Arduino Yun – the Arduino with WiFi

arduino-yun

One of the most common complaints of Arduinos is their lack of connectivity. You build these cool hardware hacks, but then you can’t make them apart of the Internet of Things (IoT) unless you plug in a bulky Arduino WiFi shield.

The Arduino Yun solves that problem. The Yun is an Arduino with WiFi built in. Additionally, the Yun has a second microprocessor that runs a lightweight version of Linux and comes with Python preinstalled. This means that for $75, you can have sensors and buttons trigger Python scripts, and Python scripts trigger LEDs, motors and other actuators. And Python’s just the language that comes with it — you can install Ruby, Node or PHP if that’s your jam.

Before you start hacking though, you need to make some preparations. In this tutorial you’ll learn how to:

  • format an SD card to work in the Arduino Yun
  • upgrade OpenWRT, the version of Linux that runs on the Yun
  • connect your Arduino Yun to the wifi network
  • SSH into your Yun
  • install the Arudino IDE on your computer
  • run Blink, the “Hello World” of Arduino programs

My guess is that it’ll take you 30-45 minutes to complete this tutorial. To follow along, you’ll need an Arduino Yun (~$75 at Spark Fun or Ada Fruit), a microUSB cable (~$5) and a micro SD card (~$8).

Once you’ve finished this tutorial, you may want to put your newly acquired skills to use to:

Get a primer on the Twilio SMS and MMS docs here. Check out our IoT Tutorials for Sync for IoT as well.

Overview of the Arduino Yun

This is what the Arduino Yun looks like:

Arduino Yun Hardware

The Yun’s primary components are:

  • Atmel ATmega32U4 – this is the “Arduino chip” that controls the pins and lets you do all the hardware hacking that you associate with Arduinos. When you send a sketch (Arduino code) to the Yun via the Arduino IDE, it’s running on this chip.
  • Atheros AR9331 – This is the “Linux and WiFi chip.” It runs OpenWRT, “a Linux distribution for embedded devices.”
  • Three ports: a standard USB, a micro USB, and an ethernet port
  • Three buttons: WiFi reset, “Arduino chip” reset, and “Linux chip” reset. You’ll use the WiFi reset most often.
  • A microSD slot for external storage

A few tidbits worth noting:

Unlike the standard Arduino, the Yun does not have a dedicated port for power. Instead, it gets its juice from the micro USB port. You’ll spend a lot of development cycles with the Yun plugged into your computer, but actually sending data to it over WiFi. The Arduino Yun I bought from Adafruit did not come with a microUSB cable.

If you hold down the Arduino’s WiFi reset button for more than 30 seconds, your Yun will reset to factory conditions! (Though, if you’ve upgraded OpenWRT, the new version will remain.) If you hold the WiFi button for more than 5 seconds but less than 30, it will reset the WiFi networking settings.

For instance, let’s hypothetically say that you’re writing a “Getting Started with the Yun” guide at your office in Chicago, then it snows 19 inches, so you decide to work from home the next day. Since your Yun is set up for the office WiFi, you’ll need to hold down the WiFi reset button for somewhere between 5 and 29 seconds, then go through the steps below to connect to your home WiFi network.

The Yun also has all the pins that you’d expect to see on an Arduino (we’ll cover those in a future tutorial):

arduino-yun-pins-1

arduino-yun-pins-2

Prepare the Arduino SD card

You’re going to need a micro SD card with your Yun. The official getting started guide says, “You’re discouraged from using the Yún’s built-in non-volatile memory, because it has a limited number of writes.” I don’t know what that means, but it sounds bad, so I’m going to take it at face value.

So before we do anything with the Yun itself, let’s prepare the microSD card. If you’re using a brand new SD card, it will probably work out of the box. If you’re pulling your SD card out of a camera like I did, you’ll need to format it to a FAT file system (Not FAT 32!).

To format an SD card on OSX, plug it into your machine (you’ll need one of those adapters to make it look like a big SD card), open up the Disk Utility and do this:

format-sd-card-on-osx

Don’t unplug the SD card from your computer just yet! We need to put some software on it before we move it over to the Yun.

Upgrade OpenWRT-Yun

Your Yun ships with a lightweight version of Linux called OpenWRT-Yun. In the time that has passed since your Yun shipped off the factory line, a newer and better version of OpenWRT-Yun has been released. (As of writing, the image released on Nov 14, 2014 is the most recent).

I know… It’s tempting to just jump right into playing around with the Yun, but you’re going to save yourself a lot of headache if you upgrade to the latest version of OpenWRT-Yun now. (If you want to know all the details of why, check out the OpenWRT release notes). This process will take less than ten minutes.

  • Visit the Arduino Software page
  • Scroll halfway down the page
  • Click the link for the OpenWRT-Yun upgrade image

upgrade-openwrt

  • Unzip the file and copy the .bin into the root of the SD card
  • Eject the SD card, remove it from your computer, and plug it into the Yun

Now we’ve got an OpenWRT image lying in wait on our SD card. Let’s fire up the Yun.

Connect to the Ardunio Yun over Wifi

If you have not previously connected your Yun to WiFi, it will create its own WiFi hotspot. You’ll connect your computer directly to the Arudino’s WiFi, open up the control panel in a browser, configure the Yun to connect to the wireless network of your choosing, then meet it back on that network.

  • Plug the small end of the micro USB cable into your Yun and the other end into your computer
  • Wait about 60 seconds for the Yun to boot up. When the Yun’s ready to go, you’ll see the white USB LED turn on.
  • On your laptop, check the available WiFi Networks for a new one called something like: ARDUINO-YUN90XXXXXX. Tangentially, here’s a pic from a thread on /r/arduino titled This happens during Arduino Yun Hackathon:

arduino-yun-hackathon

Once you’ve connected to your Arduino’s WiFi, open a browser and visit arduino.local. If that doesn’t work, visit 192.168.240.1 (I’m not sure why, but I found the former to be flakey). When asked for a password, enter “arduino”

You’ll see a lot of information that we’re going to come back to. For now, ignore it and scroll all the way to the bottom where you’ll see an option to upgrade OpenWRT. Click RESET.

reset-yun

While you’re waiting the few minutes for the thing to reinstall and reboot, pat yourself on the back. Seriously… I know how much fun it is to dive in and start playing around. I’m proud of you for delaying gratification.

Now let’s have some fun.

Connect your Arduino WiFi

Once your Arduino Yun reboots:

  • ensure you’re still connected to the Arduino YUN-09XXXX WiFi network
  • revisit arduino.local or 192.168.240.1
  • click the Configure Button in the top right

Here you can:

  • Change the name of your Arduino Yun
  • Change the password. You should do this… especially if you’re at one of the aforementioned Yun hackathons
  • Connect your Arduino to a WiFi network.

Select your WiFi network from the dropdown and carefully enter your password. If you get the password wrong you will not get an error message. If your Arduino reboots but you still see its Arduino Yun-902XXXX as an available option, there’s a good chance you typed in the wrong WiFi password.

configure-wifi

When all the settings are to your liking, click Configure and Restart. While you’re waiting, switch your laptop over to the WiFi network you just punched in to your Arduino Yun and wait for it to join you there.

Once it’s back up and running, you have arrived at the moment of truth.

  • Open up a terminal
  • Type ssh root@arduino.local (or, if you changed the name, root@yourarduinoname.local)
  • Type yes to add your Yun as a known host
  • Punch in your password
  • Rejoice!

You have just SSHed into a $75 hardware hacking computer that’s smaller than a deck of playing cards.

ssh-arduino

Install the Arduino IDE

The last bit of setup is to install the Arduino IDE on our computer. This will let us write Arduino programs that will interact with the pins on the board so that we can do the kind of things that you associate with a traditional Arduino.

Make sure you have either Java 6 or Java 7 installed on your machine. If you don’t, download and install Java 7. (Java is currently at version 8, though the Arduino IDE seems to only support 6 or 7.)

Visit the Arduino software page and download Arduino 1.5 or greater! This is not the most obvious option on the page! The Yun requires the latest and greatest Arduino IDE, not that old boring stuff all the other Arduinos are running. Make sure you download the correct package for your OS and Java version.

arduino-ide-1.5.8
This file is about 150 MB. After it downloads, install it, then open the Arduino IDE. Then we need to do two things to set up the IDE to work with your Yun:

First, click Tools -> Board and select Arduino Yun.

board-arduino-yun
Second, click Tools -> Port and select the option with the IP address (it’s probably the last one).

tools-port-ip

<Blink>

Great! Now you’re connected to your Yun over WiFi and can start writing Arduino programs, called “sketches.” Let’s walk through the Arduino equivalent of “Hello World,” a simple program called “Blink.”

Click on File -> Examples -> Basics and select Blink.

arduino-blink

The first thing you may notice is the code structure. Most Arduino programs are composed of two chunks: the setup() which runs once after the program is loaded onto the Arduino, and the loop() and which runs continuously after that.

In the case of Blink, the setup() initiates the Yun’s built in LED as an output. The loop() turns it on and off every second. The comments in the Blink code are pretty self explanatory, so I won’t waste words replicating them here.

Our last step is to get our code onto the Yun. In the top left of the Arduino IDE, click the “Upload” icon. Oddly, that’s not the arrow pointing up, but the one pointing to the right.  After a few seconds, your program should load and a red light on your Arduino will start flashing.

upload-file

Onward!

Alright! So you’ve now successfully got your Arduino Yun running the most recent version of OpenWRT-Yun, you’ve connected it to a WiFi network, you’ve installed the Arduino IDE, and you’ve successfully uploaded and run a sketch. You’re ready to start hacking away on your WiFi enabled Arduino.

Three things that we’ve done with ours are:

I’d love to see what you do with yours. If you’ve got any awesome Arduino hacks, drop them in comments below, hit me up on twitter at @greggyb, or drop me an email at gb@twilio.com.

If you’re looking to learn more, the following resources were incredibly helpful in piecing together this guide:

Happy Hacking!

  • bob

    Soo many steps, it appears overly complex

    • http://greg.baugues.com Greg Baugues

      Totally feel you on that. I felt the same way when I was getting started — that it was a pretty high “effort to magic” ratio. But I’ve found it to be worth the payoff. I think you’d find that, if you started working through it, this tutorial would take you about 30 minutes, and then things start moving a lot faster.

      • Henrique Potter

        Sorry for kicking old dogs, but i completely disagree. You did a great tutorial here, compressed well even what was on the official Arduino getting started page, and still work even 7 months later.

        • http://greg.baugues.com Greg Baugues

          Very kind of you. Thank you.

    • https://franciskim.co Francis Kim

      Looks straightforward to me *shrug*

    • Multiplataformgamerz

      late response i know
      but if you find this complex, you should rethink if you are fit to work with hardware hacks or hardware in general, since this is just software stuff, not even the hard part here.
      A better fitting hobby would do the job.

  • Pellumb

    great tutorial. thank you. one more thing; How to upload folders to arduino YUN via wifi ??? how to format sd card so its ready for folder uploads – its to do with smart alarm clock project from MAKE…thanks in advance

  • Jade R

    Hey nice tutorial to get started on the Yun. A couple of things I’d like to point out 1. I still had to unzip file and transfer binary to SD card 2. To start using the SD card memory, you have to create an arduino folder in sd card root. Otherwise +1

  • sscirrus

    This was a beautifully written post. My setup process was smooth and everything went as planned. Thank you so much!

    • http://greg.baugues.com Greg Baugues

      That’s very kind of you to say. Thank you.

  • Leo Kauntz Moderini

    Hi, if I go to 192.168.240.1 I can’t find the option Upgrade OpenWRT, it only shows me that WIFI is connected and Ethernet not, but there is a button where stands “configure”, if I click, there are only the options “board configuration”, “wireless parameters” and “advanced configuration panel (luci)”, if I click that one, I get into a menu with lots of configuration options and other data, but nowhere stands upgrade OpenWRT, how can I upgrade OpenWRT? Sorry for my English, Thank you in advance for answers!

    • http://greg.baugues.com Greg Baugues

      So sorry you’re having problems with that. Try unzipping the .zip and putting the .bin in the root directory. (And if that works, can you let me know?)

      • Leo Kauntz Moderini

        Yes, I will, thank you by now, I am going to try tomorrow.

      • Leo Kauntz Moderini

        Now I was able to upgrade OpenWRT, I had to extract the binary file and put it in an “arduino” folder in the root of the sd card and format it in FAT file system, FAT32 didn’t work. Then, on 192.168.240.1 the option to upgrade OpenWRT appeared. Thank you for help, this nice tutorial and all the useful comments! :)

        • http://greg.baugues.com Greg Baugues

          Updated with your feedback. Thank you. (Though I was able to get it to work with the .bin in the root, so I left that as is).

  • Rachel

    Could I take a camera out of a phone and use that instead of a webcam?

    • Rachel

      Sorry wrong discussion

  • iben

    I have been trying to unzip the folder in so many different ways now but i can’t get it unzipped, please help me! I’m using OS X. thanks.

  • iben stoylen

    Im really stuck on the last step… When i click upload than I need to insert a password, and I have managed to forget my password. How do I change the password or reset it?

  • Haziq Sheikh

    hi i need help with connecting Bluetooth with arduino yun so that i could control two or more locks using bluetooth and wifi ( one communication medium at a time ).

  • mintedsky

    Thank you so much, this tutorial got me through set up~~!!!!!!!

  • zekrom fast

    Very helpful.. thanks.

  • http://www.deadlywind.com/ Paintball Barrel

    make them “a part”, not “apart”

  • Felix

    This was a very useful resource! I had some problems so I thought I would share them and the solutions. I am using a Mac running El Capitan.

    My first problem was that after configuring the board to be recognised on my wi-fi network, it wasn’t recognised i.e. the button ‘find me here’ didn’t take me to the board. Also I could not upload sketches over wi-fi. I was able to ssh to the board though…

    The solution turned out to be to disable the McAfee firewall.

    The second problem remained: being able to send sketches over wi-fi to the board. The board was visible in the list of ports as a wi-fi location but the IDE kept asking for my password again and again. This was only solved once the micro SD card arrived and I did the openWRT-yun upgrade that I should have done in the first place!

    So thank you very much and I hope my experiences help others if they get stuck.

  • Marty Tripp

    Hi there, I purchsed the Yun from Digikey, and picked it up today…only to find that I couldn’t even get it to show the Arduinoxxxx SSID…Because the Linux version was too old! I finally had to connect via ethernet and then was able to use the Linino login and update the OpenWRT. I then found that it will not connect to my home wifi using a ‘ö’ character or with WPA2. Here’s a great resource for anyone new: http://playground.arduino.cc/Hardware/Yun#restoring_a_yun
    Hopefully it helps those that have a headache like I did!

  • Marty Tripp

    THANK YOU for this posting, btw…it did help immensely! You may want to put that webpage link I included above just so anyone else with issues can rock through them!

  • hydrogen.uranium

    Hi,
    First, I Configured my Arduino Yun using Wizard Setup in arduino.local (192.168.240.1) I put all the details required I when it finished click save and restart to start the process but unfortunately I got an Error :
    Arduino Settings Error while communication with device: AJAX/XHR Error:0
    Warning! you are offline!
    Warning! you are online!

    Here is my specification for my laptop Asus Model GL552JX Processor: Intel(R) Core (TM) i7-4720HQ CPU 2.60GHz RAM: 8GB System 64-bit Operating system x64-based processor Windows 10 Home Single

    How Can I resolved this matter and I already tried so many example in Internet but seems not working for this problem. After all my board does not have any problem besides mentioned above.
    Thank you

  • ز. عقبة

    Can I configure a password for “Arduino Yun-XXXXXXXXXX” wifi ? I need to keep it secure, not open.

  • Lorys

    How do I safely stop the program from running? Great tutorial by the way.

  • Tajuddin

    Please help with the dragino Yun shield problem
    https://forum.arduino.cc/index.php?topic=513329.0