The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

81

Transcript of The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Page 1: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf
Page 2: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Copyright

The Hands-on Arduino Yún Manual Lab

Agus Kurniawan

1st Edition, 2015

Copyright © 2015 Agus Kurniawan

Page 3: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Table of ContentsCopyright

Preface

1. Preparing Development Environment

1.1 Arduino Yún

1.2 Electronic Components

1.2.1 Arduino Starter Kit

1.2.2 Fritzing

1.2.3 Cooking-Hacks: Arduino Starter Kit

1.2.4 Arduino Sidekick Basic kit

1.3 Arduino Software

1.4 Unboxing

2. Basic Operations

2.1 Getting Started

2.2 Connecting to Arduino Yún

2.3 Updating Arduino Yún Firmware

2.3.1 Preparation

2.3.2 Updating The Board Firmware

2.4 Connecting to Existing Wifi

2.5 Reseting Wifi Settings

2.6 Connecting Arduino IDE Sketch to Arduino Yún

2.6.1 Serial Port

2.6.2 Remote via Wifi

2.7 Hello World: Blinking LED

2.7.1 Wiring

2.7.2 Writing Blinking LED Program

2.7.3 Deploying and Testing via Serial Port

2.7.4 Deploying and Testing via Remote Wifi

2.7.5 Deploying Program via Web Panel

2.8 SSH

2.9 Restart and Shutdown

3. Arduino Yún Sketch Programming

3.1 Getting Started

3.2 Digital I/O

3.2.1 Wiring

3.2.2 Writing Program

3.2.3 Testing

3.3 Serial Communication

3.4 Analog I/O

3.4.1 Potentiometer

3.4.1.1 Wiring

3.4.1.2 Writing Program

Page 4: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

3.4.1.3 Testing

3.4.2 RGB LED

3.4.2.1 Wiring

3.4.2.2 Writing Program

3.4.2.3 Testing

3.5 SPI

3.6 I2C

4. Arduino Yún Linux Programming

4.1 Getting Started

4.2 YÚN Package Manager

4.3 SFTP

4.4 Python

4.5 Bridge Library and Python

4.6 Bridge

5. Servo Motor

5.1 Servo Motor

5.2 Hardware Implementation

5.3 Arduino Sketch Implementation

5.4 Testing

6. Using REST with Arduino Yún

6.1 Getting Started

6.2 Deploy Bridge Library

6.3 Testing Digital I/O

6.4 Testing Analog I/O

7. Logic Debugging

7.1 Logic Debugging

7.2 Analyzing Digital Output

7.3 Analyzing Analog Output

Source Code

Contact

Page 5: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Preface

This book was written to help anyone want to get started with Arduino Yún. It describes the basic elements of the Arduino Yún operations anddevelopment.

Agus Kurniawan

Depok, March 2015

Page 6: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

1. Preparing Development Environment

1.1 Arduino Yún

The Arduino Yún is a microcontroller board based on the ATmega32u4 (datasheet) and the Atheros AR9331. The Atheros processor supports aLinux distribution based on OpenWrt named OpenWrt-Yun. Further information about Arduino products, you can visit on website,http://arduino.cc/en/Main/ArduinoBoardYun .

The following is a form of Arduino Yún.

(source: http://arduino.cc/en/Main/ArduinoBoardYun)

The diagram of Arduino Yún can be seen in Figure below.

Page 7: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

(source: http://arduino.cc/en/Main/ArduinoBoardYun)

You can buy this product on your local electronic store. You also can order it by online. Find it on http://arduino.cc/en/Main/Buy. The following is thelist of Arduino store you can buy

Arduino store, http://store.arduino.cc/Amazon, http://www.amazon.comCooking-hacks, http://www.cooking-hacks.com/index.php/shop/arduino.htmlRS Components, http://www.rs-components.comElement 14, http://www.element14.comEXP-Tech, http://www.exp-tech.de

1.2 Electronic ComponentsWe need electronic components to build our testing, for instance, Resistor, LED, sensor devices and etc. I recommend you can buy electroniccomponent kit.

1.2.1 Arduino Starter Kit

Store website: http://arduino.cc/en/Main/ArduinoStarterKit

Page 8: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

1.2.2 Fritzing

Store website: http://shop.fritzing.org/ .

You can buy Fritzing Starter Kit with Arduino UNO or Fritzing Starter Kit with Arduino Mega.

Page 9: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

1.2.3 Cooking-Hacks: Arduino Starter Kit

Store website: http://www.cooking-hacks.com/index.php/shop/arduino/starter-kits/arduino-starter-kit.html

1.2.4 Arduino Sidekick Basic kit

Store website: http://www.seeedstudio.com/depot/arduino-sidekick-basic-kit-p-775.html

Alternative online store

http://www.amazon.com/Arduino-Sidekick-Basic-Kit-Version/dp/B007B14HM8/

http://www.exp-tech.de/Zubehoer/Arduino-Sidekick-Basic-Kit.html

Page 10: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

1.3 Arduino SoftwareTo develop application based on Arduino board, we need Arduino software. You can obtain it on http://arduino.cc/en/Main/Software . We must installArduino Software at least 1.5.0 version to build app for Arduino Yún. Please download and install based on your platform.

Page 11: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

1.4 UnboxingI bought Arduino Yún from online electronics store in Germany. I got this board as follows.

After opened, I got some stuffs, shown in Figure below.

This case we still the following items:

Power adapter with 5Vmicro USB cable for power if you don't have power adapterUSB cable

For electronics component, I user Arduino Sidekick Basic kit and Linker kit, http://store.linksprite.com/linker-kit/, for electronic components

Page 12: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf
Page 13: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

2. Basic Operations

This chapter explains how to get started with Arduino Yún.

2.1 Getting Started

In this chapter we explore how to connect to Arduino Yún through Serial Port and Wifi. Then, we try to build a simple app, blinking LED, and deploy tothe board. As we know, Arduino Yún consists of Linux OS, OpenWrt, so we try to connect Arduino Yún Linux via SSH.

Let's start.

2.2 Connecting to Arduino YúnIn this section, firstly, we try to connect to Arduino Yún via Wifi. This board create AP with SSID Arduino Yun-XXXXXX. XXXXXX is random characters.Plug in power adapter to micro USB Arduino Yún. Note: Please use 5V DC for Arduino Yún power.

From your computer, open your Wifi manager, you can see the SSID Arduino Yun-XXXXXX, for instance, you can see it in Figure below.

If you see this SSID, try to connect to it.

Page 14: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

After that, open a browser and navigate tp http://arduino.local . If success, you see this page.

By default, the board password is arduino. Click LOG IN button to authenticate.

If success, you will get this page.

2.3 Updating Arduino Yún FirmwareIn this section, we try to update Arduino Yún firmware. When we buy Arduino Yún, the board firmware may old version so we need to update. Thereare two methods to update the firmware:

Web PanelTerminal via SSH

In this section, we use Web Panel to update Arduino Yún firmware.

2.3.1 Preparation

Page 15: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Firstly, we need microSD card. Format it by FAT/FAT32.

After that, you can download the latest Arduino Yún firmware on http://arduino.cc/en/Main/Software .

Page 16: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Extract this file and you will get Arduino Yún firmware file in *.bin format.

Copy Arduino Yún firmware file (*.bin) into your microSD card.

Plug in this microSD into Arduino Yún board.

Page 17: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

2.3.2 Updating The Board Firmware

After you put microSD which consists of Arduino Yún firmware file, we can upgrade Arduino Yún firmware. Open a browser and navigate tohttp://arduino.local .

Entry password. By default (if you don't change the password), the password is arduino.

If success, you can see the page, shown in Figure below.

Page 18: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Click RESET button.

You will get a confirmation. Click OK button to upgrade Arduino Yún firmware.

The board will update Arduino Yún firmware from microSD card. You see WLAN LED (blue color LED) is blinking. Wait this process until WLAN LEDdoesn't blink again.

Page 19: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

After that, try to restart.

Now you get Arduino Yún with the latest firmware.

2.4 Connecting to Existing WifiIf you want to connect Arduino Yún to existing Wifi, we do it by configuring on Arduino Yún web panel.

Firstly, connect your computer to Arduino Yún AP which SSID name Arduino Yun-XXXX.

Page 20: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

After joined, open a browser and navigate to http://arduino.local.

Entry password and then click LOG IN button.

If success, you get web panel dashboard, shown in Figure below.

Page 21: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Click CONFIGURE button.

You will get the page, shown in Figure below.

Page 22: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Checked on CONFIGURE A WIRELESS NETWORK. Select target SSID and entry its password (if any).

If done, click CONFIGURE & RESTART button.

Please wait it until finished.

If success, your computer must join Wifi which Arduino has already joined.

After that, open a browser and navigate to http://arduino.local . If you get the page, your Wifi configuration is correct.

2.5 Reseting Wifi SettingsWe can reset our existing Wifi settings on Arduino Yún. It's easy. You just press and hold WLAN button on the board. See this button in Figure below.

Page 23: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

The board will blink WLAN LED. Press and hold until blinking WLAN LED is stopped.

If success, Arduino Yún will back to original Wifi settings. You also see Arduino Yún AP with SSID Arduino Yun-XXXX .

2.6 Connecting Arduino IDE Sketch to Arduino YúnIn this section, we try to connect Arduino Yún via Serial Port and Wifi. Plug in Arduino Yún with power 5V.

2.6.1 Serial Port

You can plug in Arduino Yún board to computer directly through micro USB.

In Windows platform, you will see Arduino Yún board on Device Manager. In Linux, you can see it as /dev/ttyACM0 .

Page 24: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Run Arduino Sketch software. You will see Arduino Yún board connected. You can verify it by clicking menu Tools -> Port -> COMx (/dev/ttyACM0 forLinux).

Page 25: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Now your Arduino Sketch is ready to develop app.

2.6.2 Remote via Wifi

If your computer already connect to the same Wifi (same SSID), you will see Arduino with IP on Arduino Sketch software, shown in Figure below.

Page 26: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

If you don't see it, you can verify with this check list:

Upgrade Arduino Yún with the latest firmwareCheck if your computer block Arduino Yún port. It uses UDP port 5353 for broadcasting.Check your firewall. Make sure your firewall doesn't block the Arduino SketchReinstall Bonjour if you use Windows platform. The latest Bonjour can be downloaded on https://support.apple.com/kb/DL999?locale=en_US .

2.7 Hello World: Blinking LEDNow we start to write program for Arduino Yún. We create a blinking LED app.

2.7.1 Wiring

There is no configuration on your board. We use a built-in LED, (Digital pin 13).

2.7.2 Writing Blinking LED Program

Run Arduino Sketch. Click menu File -> Examples -> 01.Basics -> Blink .

Page 27: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Then, you get a blink code sample.

Page 28: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

2.7.3 Deploying and Testing via Serial Port

Now you can compile and deploy to Arduino Yún. Firstly, we select board type on Arduino Sketch. Click menu Tools -> Board -> Arduino Yún .

Page 29: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

The first scenario for deploying can be applied through Serial Port. Plug in your Arduino Yún via micro USN to your computer. After that, ArduinoSketch will detect your Arduino Yún, for instance, COM8 for Windows platform. Then, select this serial Port by clicking menu Tools -> Port -> <<SerialPort>>.

After that, you can compile and deploy by clicking those icons, see them in Figure below.

Page 30: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

After deployed, you will see LED L13 is blinking.

Page 31: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

2.7.4 Deploying and Testing via Remote Wifi

If you want to deploy Blink program to Arduino Yún via remote Wifi, select remote port on Arduino Sketch. Click menu Tools -> Board -> Arduino Yún .

Page 32: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Then, select remote Wifi. Click menu Tools -> Port -> Arduino at xxxxx (Arduino Yún).

Then, try to compile and deploy to board.

Page 33: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

When you deploy to the board, you will be asked to entry password for Arduino Yún.

Page 34: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

If done, you can see LED L13 is blinking.

Page 35: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

2.7.5 Deploying Program via Web Panel

We also deploy Sketch program to Arduino Yún via web panel. You open a browser and navigate to http://arduino.local . Entry Arduino password.

After that, you can see UPLOAD SKETCH. Upload compiled program from Sketch program. Click UPLOAD button to upload program file.

Page 36: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

2.8 SSHArduino Yún has installed SSH so we can connect to Arduino Yún via SSH. Firstly, your computer already connect to Arduino Yún or both yourcomputer and Arduino Yún connect to the same Wifi or one IP segment (Wired).

You can use SSH client to connect Arduino Yún. For Windows users, you can use PuTTY,http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html .

Run SSH client, for instance, I use PuTTY. Fill hostname of Arduino Yún is arduino.local and port 22.

Page 37: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Click Open button. You will get a security alert.

Page 38: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Click Yes button.

Now you get SSH Terminal. Entry user: root. Password is your Arduino password.

2.9 Restart and ShutdownWe can restart Arduino Yún using two methods. You can restart by press YUN RST, shown in Figure below.

You can restart Arduino Yún via SSH. You can type this command on your Terminal.

$ reboot

Page 39: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Arduino Yún will restart. It takes several minutes to restart.

To turn off your Arduino Yún, you can plug out power adapter from the board. You also can turn off Arduino Yún via SSH and type this command.

$ poweroff

Page 40: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

3. Arduino Yún Sketch Programming

In this chapter I'm going to explain how to write program for Arduino Yún using Arduino Sketch.

3.1 Getting Started

In section 2.7, we have tried to write a simple program, Blinking LED. Now we go through how to develop program for Arduino Yún using ArduinoSketch. We focus on I/O programming. Several cases will be provided to show how to work with Arduino Yún I/O. The following is our list of problemcase:

Digital I/OSerial CommunicationAnalog I/OSPII2C/PWI

Let's go!.

3.2 Digital I/OIn this section, we build a program to access Arduino Digital I/O. In this section, we need the following components:

LEDPush buttonCables

When a push button is pressed, LED will ON.

Let's start.

3.2.1 Wiring

The demo wiring is simple. Connect your LED to digital pin 8 and push button to digital pin 9. The complete wiring can be seen in Figure below.

Page 41: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

A sample of hardware implementation is shown in Figure below.

Page 42: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

3.2.2 Writing Program

Now we can write program. Run Arduino Sketch and write this code.

int led = 8;int button = 9;int val = 0;

void setup() { pinMode(led, OUTPUT); pinMode(button, INPUT);

}

void loop() { val = digitalRead(button); digitalWrite(led, val);}

Save this code as DigitalIO.

Page 43: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

3.2.3 Testing

After written program, you can compile and deploy the program to the board.

For testing, you press that push button. Then, you can see LED is ON.

3.3 Serial CommunicationWe can access Serial Port (UART) from Arduino Sketch using Serial object, http://arduino.cc/en/Reference/Serial .

Open software Arduino Sketch and write this code.

unsigned long counter;int led = 13;

void setup(){ counter = 0; Serial.begin(9600); }

void loop(){ Serial.println(counter); delay(1000); counter++; if(counter>=100) counter = 0;}

Save this code with name SerialDemo. Now you can compile and deploy this program to board.

To verify this program, you can use Serial Monitor. It can be called by clicking menu Tools -> Serial Monitor.

If success, you get a dialog. It shows message from Arduino UART.

Page 44: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

3.4 Analog I/OIn this section, we develop Arduino Sketch to access Analog I/O. Arduino Yún has 6 analog inputs, A0, A1, A2, A3, A4, A5. For Analog output, it'scalled PWM.

You obtain PWM pins easily but if you have Arduino Yún, it writes DIGITAL (PWM ~). It means your PWM pins can be found on DIGITAL pins which pinwith ~, for instance, ~3,~5,~6,~9, ~10, ~11.

3.4.1 Potentiometer

We use a Potentiometer to illustrate how to read analog input.

3.4.1.1 Wiring

We use Potentiometer as analog input source. Connect Potentiometer pins to VCC and Vout to Analog input A0 on Arduino Yún.

Page 45: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

For implementation, I use a slide Potentiometer, http://store.linksprite.com/linear-slide-potentiometer-of-linker-kit-for-pcduino-arduino/ . Thefollowing is hardware implementation.

3.4.1.2 Writing Program

To read analog input, we use analogRead(), http://arduino.cc/en/Reference/AnalogRead . Open Arduino Sketch and write this code.

Page 46: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

int val = 0;int pot = 0;

void setup(){ Serial.begin(9600); }

void loop(){ val = analogRead(pot); Serial.print("A0="); Serial.println(val); delay(500); }

Save this code, called PotDemo.

3.4.1.3 Testing

Compile and deploy the program to the board. To see analog input value, open Serial Monitor. It can get it by clicking menu Tools -> Serial Monitor.

Try to change value on Potentiometer and see it on Serial Monitor. A sample output can be seen in Figure below.

3.4.2 RGB LED

This chapter explains how to control RGB LED connected to Arduino board using Arduino Sketch. We explore how to access PWM (Pulse WidthModulation) Arduino.

In this scenario we build Arduino Sketch application to control RGB LED color using Arduino Analog output (PWM). RGB LED has 4 pins that youcan see it on Figure below.

Page 47: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

To understand these pins, you can see the following Figure.

Note:

Pin 1: RedPin 2: Common pinPin 3: GreenPin 4: Blue

Now we can start to build an Arduino application and hardware implementation.

3.4.2.1 Wiring

Page 48: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Firstly we implement RGB LED hardware. The following is a hardware schema with Arduino Yún.

For our testing, we configure the following PWM pins.

RGB LED pin 1 (red) is connected to Arduino PWM pin 9RGB LED pin 2 is connected to Arduino VCC 5VRGB LED pin 3 (green) is connected to Arduino PWM pin 10RGB LED pin 4 (blue) is connected to Arduino PWM pin 11

Here is a sample implementation with Arduino Yún.

Page 49: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

3.4.2.2 Writing Program

Now we implement our RGB LED controller in Arduino. This is for testing. Open Firstly, we define our RGB LED pins. The following is RGB LED pinsfor Arduino Yún.

int redPin = 11;int greenPin = 10;int bluePin = 9;

Now we initialize pins on setup().

void setup(){ pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600);}

Page 50: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

We define a function, called setColor(). This function aims to write RGB values on PWM pins. We can use analogWrite(),http://arduino.cc/en/Reference/AnalogWrite .

void setColor(int red, int green, int blue){ analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue);}

Now we control RGB values on RGB LED, for instance, Red, Green, Blue, Yellow, Purple, Aqua.

void loop(){ setColor(255, 0, 0); // red Serial.println("red"); delay(1000); setColor(0, 255, 0); // green Serial.println("green"); delay(1000); setColor(0, 0, 255); // blue Serial.println("blue"); delay(1000); setColor(255, 255, 0); // yellow Serial.println("yellow"); delay(1000); setColor(80, 0, 80); // purple Serial.println("purple"); delay(1000); setColor(0, 255, 255); // aqua Serial.println("aqua"); delay(1000);}

Save this code, called rgbdemo.ino.

Page 51: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Compile and verify this code. If success, you can upload it to Arduino board.

3.4.2.3 Testing

If success, you can see RGB LED blinking with different colors. Here is a sample output of RGB LED with Arduino Yún.

Page 52: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf
Page 53: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

If you open Serial Monitor, you will get the following output.

Page 54: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

3.5 SPIThe Serial Peripheral Interface (SPI) is a communication bus that is used to interface one or more slave peripheral integrated circuits (ICs) to asingle master SPI device; usually a microcontroller or microprocessor of some sort. Note that the SPI pins are not connected to any of the digital I/Opins as they are on the Uno, They are only available on the ICSP connector.

Page 55: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

The following is a schema of ICSP connector.

(source: http://arduino.cc/en/Reference/SPI)

For illustration, we build a program to write and read data to/from Arduino Yún SPI. We build a SPI loopback which ICSP MOSI pin is be connected toICSP MISO. Hardware implementation can be seen in Figure below.

Now we can write program. Open Arduino Sketch and write this code.

#include "SPI.h"int val = 0;

void setup() { SPI.begin(); Serial.begin(9600); val = 0;}

void loop() {

Page 56: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

int ret = SPI.transfer(val); Serial.print("send="); Serial.print(val); Serial.print(", recv="); Serial.println(ret); delay(1000); val++; if(val>=100) val = 0;}

This code send a counter number using SPI.Transfer() and return reading value from SPI. Because we connect MOSI to MISO, we get the samedata from sending and receiving.

Save this code as SPIDemo.ino.

Compile and deploy this program to the board. Open Serial Monitor to see SPI communication.

3.6 I2C

The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early '80s to allow easy communication between components which reside onthe same circuit board. TWI stands for Two Wire Interface and for most marts this bus is identical to I²C. The name TWI was introduced by Atmeland other companies to avoid conflicts with trademark issues related to I²C.

I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line). Arduino Yún has I2C/TWI on Digital 2 (SDA) and Digital 3 (SCL).

For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices. You can find it on the following online store:

Amazon, http://www.amazon.com/PCF8591-Converter-Module-Digital-Conversion/dp/B00BXX4UWC/eBay, http://www.ebay.comDealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-to-analog-converter-module-w-dupont-cable-deep-blue-336384Aliexpress, http://www.aliexpress.com/

In addition, you can find this device on your local electronics store/online store.

Page 57: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

This module has mini form model too, for instance, you can find it on Amazon, http://www.amazon.com/WaveShare-PCF8591T-Converter-Evaluation-Development/dp/B00KM6X2OI/ .

This module use PCF8591 IC and you can read the datasheet on the following URLs.

http://www.electrodragon.com/w/images/e/ed/PCF8591.pdfhttp://www.nxp.com/documents/data_sheet/PCF8591.pdf

The following is our wiring lab:

Page 58: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

PCF8591 AD/DA SDA --> Arduino Yún Digital 2 (SDA)PCF8591 AD/DA SCL --> Arduino Yún Digital 3 (SCL)PCF8591 AD/DA GND --> Arduino Yún GNDPCF8591 AD/DA VCC --> Arduino Yún VCC +3.3V

Hardware implementation can be shown in Figure below.

Now you can write the program.

Open Arduino Sketch and write this code.

#include <Wire.h> #define PCF8591 (0x90 >> 1) // Device address #define PCF8591_ADC_CH0 0x40 // thermistor #define PCF8591_ADC_CH1 0x41 // photo-voltaic cell #define PCF8591_ADC_CH2 0x42 #define PCF8591_ADC_CH3 0x43 // potentiometer byte adc_value; byte getADC(byte config) { Wire.beginTransmission(PCF8591); Wire.write(config); Wire.endTransmission(); Wire.requestFrom((int) PCF8591,2); while (Wire.available()) { adc_value = Wire.read(); //This needs two reads to get the value. adc_value = Wire.read(); } return adc_value; } void setup() { Serial.begin(9600); Wire.begin(); Serial.println("ADC Test"); } void loop()

Page 59: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

{ adc_value = getADC(PCF8591_ADC_CH0); Serial.println("thermistor: "); Serial.println(adc_value); delay(500); adc_value = getADC(PCF8591_ADC_CH1); Serial.println("photo-voltaic cell: "); Serial.println(adc_value); delay(500); adc_value = getADC(PCF8591_ADC_CH3); Serial.println("potentiometer: "); Serial.println(adc_value); delay(500); }

Save this code, called I2CDemo.uno.

Compile and deploy this program to the board. To see I2C output, you can open Serial Monitor, shown in Figure below.

Page 60: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

4. Arduino Yún Linux Programming

This chapter explains how to work with Arduino Yún Linux and then write programs to access Arduino I/O

4.1 Getting Started

In this chapter, we explore Arduino Yún Linux. We try to do basic Linux operations. Then, we write program for Arduino Yún. To access Arduino YúnLinux, we use SSH client and connect to Arduino Yún.

4.2 YÚN Package ManagerTo update Arduino Yún package, we can use opkg command. Firstly, Arduino Yún must be connected to Internet network. You can do this byconnecting Arduino Yún Wifi to existing Wifi which has Internet access. You can also connect your Arduino Yún to Internet network via Ethernet cable.

After Arduino Yún connected to Internet network, you can update Arduino Yún package by typing this command on SSH Terminal.

$ opkg update

Further information about opkg, you can read it on this site, http://wiki.openwrt.org/doc/techref/opkg .

4.3 SFTP

If you want to access SFTP to Arduino Yún, we can install openssh-sftp-server via opkg. Type this command on SSH Terminal.

$ opkg update$ opkg install openssh-sftp-server

Page 61: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

After installed, you can test using SFTP client. For Windows users, you can use WinSCP, http://winscp.net/ . Create a new site. Fill arduino.local forHost name and port 22. Entry user: root and Arduino password.

Click Login to connect Arduino Yún SFTP server.

If success, you get SFTP panel, shown in Figure below.

Page 62: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

4.4 PythonArduino Yún Linux has installed Python for you. You can test it via SSH Terminal. Type these commands.

$ python>>> a = 3>>> b = 5>>> c = a*b>>> c>>> quit()

Page 63: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

If you want to install Python pip, you can type these commands.

$ opkg update$ opkg install distribute$ opkg install python-openssl$ easy_install pip

4.5 Bridge Library and PythonIt is not possible to access the I/O pins of the Atheros AR9331. All I/O lines are tied to the 32U4. Arduino Yún provides a bridge which connectbetween Arduino and Linux OpenWrt. The following is a bridge architecture.

Page 64: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

(source: http://arduino.cc/en/Guide/ArduinoYun)

In this section, we try to use Yun Bridge Library, http://arduino.cc/en/Reference/YunBridgeLibrary, to communicate between Arduino and Linux(Linino).

Firstly, we open Arduino Sketch and write this code.

#include <Bridge.h>

unsigned long counter;

void setup(){ counter = 0; Bridge.begin(); }

void loop(){ counter++; if(counter>=100) counter = 0; Bridge.put("counter", String(counter++)); delay(1000); }

Page 65: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Compile and deploy to Arduino Yún board.

This app will send data to Linino with name "counter". This value will be retrieved by Linino. We use Python application.

Open SSH Terminal. Create a file hello.py and write this code.

import sysimport time

sys.path.insert(0, '/usr/lib/python2.7/bridge/')from bridgeclient import BridgeClient as bridgeclient

client = bridgeclient()client.begin()current_val = 0while True: try: val = client.get('counter') print val time.sleep(1) except (KeyboardInterrupt, SystemExit): client.close() raise

Now you can run this file into Terminal.

$ python hello.py

Page 66: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

If success, you get the output, shown in Figure below.

4.6 BridgeWe explore again Bridge using Python. In this scenario, we try to turn on/off a LED from Python. We use LED pin 13 for testing. The scenario can bedescribed as follows:

Python app send 0 or 1 via Bridge clientArduino app will receive this messageIf Arduino receives value 1, Arduino will turn on LED 13Otherwise, if value 0 is received, Arduino will turn off LED 13

Firstly, we createa Arduino app using Arduino Sketch. Write this code.

#include <Bridge.h>

int led = 13;char val[1];

void setup(){ Serial.begin(9600); Bridge.begin(); }

void loop(){ Bridge.get("led",val,1); if(String(val)!="") { Serial.print("val="); Serial.println(val[0]); int n = val[0]-'0'; if(n==1) digitalWrite(led,HIGH); else digitalWrite(led,LOW); delay(1000); } }

Save this code called LedDemo. Compile and deploy this program to the board.

Now we work on SSH Terminal. Create a file, called leddemo.py, and write this code.

import sysimport time

sys.path.insert(0, '/usr/lib/python2.7/bridge/')from bridgeclient import BridgeClient as bridgeclient

client = bridgeclient()client.begin()current_val = 0

Page 67: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

while True: try: client.put('led', str(current_val)) print current_val if current_val == 0: current_val = 1 else: current_val = 0

time.sleep(1) except (KeyboardInterrupt, SystemExit): client.close() raise

Now you can run this program.

$ python leddemo.py

The following is a program output on Python app.

If we open Serial Monitor from Arduino Sketch, we can see message.

Page 68: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

You also see LED 13 is blinking.

Page 69: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

5. Servo Motor

This chapter explains how to work with servo motor connected to Arduino Yún board using Arduino Sketch. We explore how to access PWM (PulseWidth Modulation) Arduino Yún.

5.1 Servo Motor

Servo motor provides a shaft movement 360 angle. We can control this movement based on its angle. In this scenario, you can use any DC motor(servo) that will be connected to Arduino. I used a mini servo from Arduino Sidekick Basic kit.

The following is a picture of my mini servo motor.

To understand servo's cables, you can identify as follows:

Red cable is be connected to 5VBlack or brown cable is be connected to GNDThe rest (yellow or orange cable) is be connected to Arduino PWM pin

The next step we are going to build app with Arduino and servo motor.

5.2 Hardware ImplementationTo build hardware implementation, you can connect servo motor to Arduino Yún by following configuration:

Red cable is be connected to 5VBlack or brown cable is be connected to GNDThe rest (yellow or orange cable) is be connected to Arduino PWM pin. You must choose PWM pin 9 or 10. I used pin 10 for Arduino Yún

Page 70: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

The following is a sample of hardware implementation.

5.3 Arduino Sketch ImplementationIn this section, we create Arduino application to control servo motor. For testing, I used Arduino Servo library, http://arduino.cc/en/reference/servo .

Open Arduino Sketch and write this code.

#include <Servo.h> Servo servo;int servoPin = 10;int angle = 0;int angleStep = 15;

void setup() { servo.attach(servoPin); Serial.begin(9600); angle = 0;}

void loop() { servo.write(angle); Serial.print("degree="); Serial.println(angle); delay(1000); angle = angle + angleStep; if(angle>180) { angle = 180; angleStep = -15; } if(angle<0){ angle = 0;

Page 71: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

angleStep = 15; }}

Save this code, called ServoDemo.uno.

5.4 Testing

Compile and deploy this program into the board. If success, servo motor will move with a certain angle. You also can verify by opening SerialMonitor.

A sample output of Serial Monitor can be seen in Figure below.

Page 72: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

6. Using REST with Arduino Yún

In this chapter We access Arduino Yú from REST.

6.1 Getting Started

We can access Arduino Yún using REST. In this chapter, we try to implement this feature.

6.2 Deploy Bridge LibraryTo enable Arduino Yún to be accessed using REST, we must install Bridge library. Open Arduino Sketch. Click menu File -> Examples -> Bridge ->Bridge. You will get a Bridge code.

Compile and deploy to Arduino Yú.

6.3 Testing Digital I/O

Page 73: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

After deployed Bridge library to Arduino Yú, we can use a browser.

In this scenario, we turn on/off on LED 13. Firstly, we set digital pin 13 as Output. Navigate this URL:

http://arduino.local/arduino/mode/13/output

You can change arduino.local domain if you already change it.

If you activate authentication mode, you will get the authentication dialog. Entry username and password.

Now we can turn on LED 13 by passing value 1 to pin 13. Navigate this URL.

http://arduino.local/arduino/digital/13/1

Page 74: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

You will see LED 13 turns on.

To turn off LED 13, you can type this URL.

http://arduino.local/arduino/digital/13/0

6.4 Testing Analog I/OWe also can work with analog I/O. We don't need to set mode for analog I/O. For instance, we want to read analog input A1. Navigate this URL onyour browser.

http://arduino.local/arduino/analog/1

If you want to set analog output, for instance A1, with value 258. Navigate this URL on your browser.

http://arduino.local/arduino/analog/1/258

Page 75: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf
Page 76: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

7. Logic Debugging

In this chapter I'm going to explain how to work with Logic debugging on Arduino Yú.

7.1 Logic Debugging

Sometimes you want to analyze Arduino Yú output either digital I/O or analog I/O. We can use external tools to analyze Arduino Yú output. Thefollowing is a list of logic analyzer tools:

Saleae, https://www.saleae.com/Scanalogic-2 Logic Analyzer and Signal Generator, http://www.ikalogic.com/Open Workbench Logic Sniffer, http://dangerousprototypes.com/

Page 77: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

For testing, I use Scanalogic-2 Logic Analyzer and Signal Generator tool to analyze Arduino Yún.

7.2 Analyzing Digital OutputTo test digital output, we create Arduino app using Arduino Sketch. We use digital output pin 9 and 8.

Write this code on Arduino Sketch.

int pin1 = 9;int pin2 = 8;

void setup() { pinMode(pin1, OUTPUT); pinMode(pin2, OUTPUT);}

void loop() { digitalWrite(pin1,HIGH); digitalWrite(pin2,LOW); delay(100); digitalWrite(pin1,LOW); digitalWrite(pin2,HIGH); delay(200);}

Page 78: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Using Scanalogic-2, connect CH1 to digital output pin 9 and CH2 to digital output pin 8.

The following is a sample output from Scanalogic-2.

7.3 Analyzing Analog OutputWe also can analyze analog output. We use PWM pin 10 and 9. Connect these pins to Scanalogic-2 CH1 and CH2.

Open Arduino Sketch and write this code.

int pin1 = 10;int pin2 = 9;int val1 = 0;int val2 = 0;void setup() { }

void loop() { analogWrite(pin1,val1); analogWrite(pin2,val2); delay(100); val1 = val1 + 10; if(val1>255) val1 = 0; val2 = val2 + 30; if(val2>255) val2 = 0; delay(200);}

The following is a sample output from Scanalogic-2.

Page 79: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf
Page 80: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Source Code

You can download source code on http://www.aguskurniawan.net/book/codes-yun.zip .

Page 81: The Hands-on Arduino Yun Manual - Agus Kurniawan.pdf

Contact

If you have question related to this book, please contact me at [email protected] . My blog: http://blog.aguskurniawan.net