Arduino Leonardo and Arduino Micro A Hands-On Guide For Beginner - Agus Kurniawan
Arduino Leonardo and Arduino Micro A Hands-On Guide For Beginner - Agus Kurniawan
Table of Contents
Copyright
Preface
1. Preparing Development Environment
1.1 Arduino Leonardo and Arduino Micro
1.2 Electronics 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 v2
1.2.5 Grove - Starter Kit for Arduino
1.2.6 DFRobot - Arduino Kit for Beginner v3
1.3 Development Tools
1.4 Testing
2. Setting Up Arduino Leonardo and Arduino Micro
2.1 Getting Started
2.2 Installing Arduino Software
2.3 Connecting Arduino Leonardo/Micro board to Computer
2.4 Hello Arduino Leonardo/Micro: Blinking LED
2.5 Updating Program
3. Writing and Reading Digital Data
3.1 Getting Started
3.2 Wiring
3.4 Testing
4. Serial Communication (UART)
4.1 Getting Started
4.2 Hello UART
4.2.1 Wiring
4.2.2 Writing a Program
4.2.3 Testing
4.3 Working with SoftwareSerial
5. PWM and Analog Input
5.1 Getting Started
5.2 Demo Analog Output (PWM) : RGB LED
5.2.1 Wiring
5.2.2 Writing Program
5.2.3 Testing
5.3 Demo Analog Input: Working with Potentiometer
5.3.1 Wiring
5.3.2 Writing Program
5.3.3 Testing
6. Working with I2C
6.1 Getting Started
6.2 Writing Program
6.3 Writing Program
6.4 Testing
7. Working with SPI
7.1 Getting Started
7.2 Wiring
7.3 Writing a Program
7.4 Testing
8. Accessing EEPROM
8.1 Getting Started
8.2 EEPROM Demo
9. Arduino Networking
9.1 Getting Started
9.2 Arduino Ethernet Shield
9.3 Demo 1 : Getting IP Address from DHCP Server
9.3.1 Writing Program
9.3.2 Testing
9.4 Demo 2: Building Web Server
9.4.1 Writing Program
9.4.2 Testing
10. Keyboard and Mouse HID
10.1 Getting Started
10.2 Keyboard Demo
10.3 Mouse Demo
Source Code
Contact
Preface
This book was written to help anyone want to get started with Arduino Leonardo and
Arduino/Genuino Micro boards development. It describes the basic elements of Arduino
Leonardo and Micro development using Arduino software.
Agus Kurniawan
Depok, January 2016
Officially you can buy this board on https://store.arduino.cc. You can also buy these
products on the following stores:
SparkFun, https://www.sparkfun.com/
Adafruit, https://www.adafruit.com
Exp-Tech, http://www.exp-tech.de
Amazon, http://www.amazon.com
e-Bay, http://www.ebay.com
You also can buy this product on your local electronic store.
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.
1.4 Testing
For testing, I used Arduino Leonardo on Windows 10, OS X and Ubuntu.
I also used Arduino Sidekick Basic kit for electronic components and several sensor
devices.
Click menu Tools -> Board . You should see Arduino Leonardo and Arduino/Genuino
Micro boards in the list.
If youre working on Windows platform, you can see Arduino Leonardo/Micro detect on
Device Manager. A sample of output from Device Manager (Windows) which detected my
Arduino Micro.
Firstly, connect your Arduino Leonardo/Micro board into your computer. Then, select
board target by clicking menu Tools -> Board and select Arduino/Genuino
Leonardo/Micro.
Select serial port for your Arduino Leonardo/Micro board, for instance COM5 in
Windows platform.
To upload the program, you can click Verify icon and then click Upload icon.
In this chapter Im going to explain how to work with digital input/output on Arduino
Leonardo/Micro board.
The following is a scheme of Arduino Micro board pinouts for digital Input/Output.
3.2 Wiring
We use built-in LED which is attached on pin 13 Arduino Leonardo/Micro. Then, we
connect a pushbutton to pin 3 The following is a sample of wiring.
3.4 Testing
Now you can upload and run this program to Arduino Leonardo/Micro board. For testing,
try to press pushbutton. You should see a lighting LED on the board.
In this chapter, we try to access Arduino Leonardo UART via serial adapter which is used
to upload a program too.
Lets start!.
4.2.1 Wiring
In this scenario, we use the same wiring from chapter 3. We will show pressed state from
push button on Serial.
4.2.3 Testing
Now you can upload and run program. Dont forget to set board target with Arduino
Leonardo/Micro. Read section 2.4 to upload the program.
After clicked Verify and Upload icons, you can see serial port for Arduino
Leonardo/Micro board on your computer. To see the UART output, open Serial Monitor
tool from Arduino IDE.
You should see the UART output on this window. A sample output can seen in Figure
above.
In this scenario, I use Foca tool from iTead. We connect UART/Serial tool to Arduino
Leonardo/Micro pin 10 as RX and 11 as TX.
After connected to computer, its recognized as serial port, for instance, in my Windows, it
is detected as COM3.
Now you can open Arduino IDE and write these codes.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(9600);
// check Serial activation
while (!Serial) { ; }
mySerial.begin(9600);
}
void loop() {
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
Configure your Serial application to connect UART/Serial USB. Select Arduino Leonardo
port and Baudrate 9600.
If done, you should them on Serial Monitor tool from Arduino. A sample output for Serial
Monitor and Serial application.
This chapter explains how to work with Arduino Leonardo/Micro Analog I/O.
In this chapter, we try to access Arduino Leonardo/Micro Analog I/O using Arduino
software. There are two scenarios for our cases:
Controlling RGB LED
Reading Analog input using Potentiometer
Lets start.
Note:
Pin 1: Red
5.2.1 Wiring
For our testing, we configure the following PWM pins.
RGB LED pin 1 (red) is connected to Arduino Leonardo/Micro digital pin 9
RGB LED pin 2 is connected to Arduino Leonardo/Micro 5V (VCC +5V)
RGB LED pin 3 (green) is connected to Arduino Leonardo/Micro digital pin 10
RGB LED pin 4 (blue) is connected to Arduino Leonardo/Micro digital pin 11
To display a certain color, we must combine colors from red, green, blue. Arduino
Leonardo/Micro provides API for PWM like Arduino API such as analogWrite() and
analogRead() with analog value from 0 to 255.
Lets start to build a program. Firstly, open Arduino Software. Then, write these scripts.
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
setColor(0, 255, 255); // red
Serial.println("red");
delay(1000);
setColor(255, 0, 255); // green
Serial.println("green");
delay(1000);
setColor(255, 255, 0); // blue
Serial.println("blue");
delay(1000);
setColor(0, 0, 255); // yellow
Serial.println("yellow");
delay(1000);
setColor(80, 255, 80); // purple
Serial.println("purple");
delay(1000);
setColor(255, 0, 0); // aqua
Serial.println("aqua");
delay(1000);
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
5.2.3 Testing
Upload and run the program. You should see several color on RGB LED.
The following is a sample demo on RGB LED.
5.3.1 Wiring
To understand Potentiometer, you see its scheme in Figure below.
You can connect VCC to Arduino Leonardo board on 3V3 pin (VCC +3.3V). Vout
to Arduino Leonardo/Micro board Analog input A0. In addition, GND to Arduino
Leonardo/Micro board GND. The following is hardware implementation. I use slide
potentiometer.
5.3.3 Testing
Upload and run this program. If success, you can see analog value using Serial Monitor
tool from Arduino IDE.
In this chapter we learn how to work with I2C on Arduino Leonardo and Micro boards
using Arduino program.
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-DigitalConversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-toanalog-converter-module-w-dupont-cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/
In addition, you can find this device on your local electronics store/online store.
This module has mini form model too, for instance, you can find it on Amazon,
http://www.amazon.com/WaveShare-PCF8591T-Converter-EvaluationDevelopment/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.pdf
http://www.nxp.com/documents/data_sheet/PCF8591.pdf
In this chapter, we build a program to access sensor via I2C using Arduino software
on Arduino Leonardo or Micro board.
ADC3=Wire.read();
Serial.print("potentiometer=");
Serial.println(ADC3);
delay(500);
}
6.4 Testing
Now you can upload and run the program to Arduino Leonardo/Micro board.
If done, open Serial monitor tool and connect to Arduino Leonardo/Micro to see the
program output. The following is a sample output.
In this chapter Im going to explain how to work with SPI on Arduino Leonardo/Micro
board.
We can only use one SPI on Arduino Leonardo board with SPI master mode. We develop
program based SPI using SPI library, https://www.arduino.cc/en/Reference/SPI .
In this chapter, we build a SPI Loopback app. Lets start!.
7.2 Wiring
To develop SPI loopback, we can connect MOSI pin to MISO pin. If you use Arduino
Leonardo, you find MOSI and MISO pins on ICSP connector. For Arduino/Genuino
Micro, you can connect MOSI pin to MISO pin which their label name are shown.
The following is a sample of wiring for Arduino Micro.
7.4 Testing
Now you can upload program to Arduino Leonardo/Micro board. If done, open Serial
Monitor tool from Arduino. You should see received data from SPI.
8. Accessing EEPROM
To achieve these actions, you can start to write program on Arduino software.
Write this program.
#include <EEPROM.h>
byte value;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
// get a size of EEPROM
Serial.print("EEPROM size: ");
Serial.print(EEPROM.length());
Serial.println(" bytes");
// write data into EEPROM on address 0, 5, 10
// value in byte = 0 .. 255
Serial.print("Writing EEPROM data");
EEPROM.write(0, 30);
EEPROM.write(5, 135);
EEPROM.write(10, 211);
Serial.println("Done");
// read EEPROM on address 0, 5, 10
value = EEPROM.read(0);
Serial.print("value ADD 0 = ");
Serial.println(value);
value = EEPROM.read(5);
Serial.print("value ADD 5 = ");
Serial.println(value);
value = EEPROM.read(10);
Serial.print("value ADD 10 = ");
Serial.println(value);
// clear all EEPROM data
9. Arduino Networking
9.3.2 Testing
Now you can compile and upload the program into Arduino board. Then, open Serial
Monitor to see the program output. Press Reset button on Arduino board to reset your
board.
A sample of program output is shown in Figure below.
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be clos
client.println("Refresh: 5"); // refresh the page automatically eve
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++
9.4.2 Testing
Now you can compile and upload the program into Arduino board. Open Serial Monitor to
see the program output.
To test the program, open a browser and navigate to IP address of web server. Then, you
can see Analog input values on website.
In this chapter Im going to explain how to work with Keyboard and Mouse HID in
Arduino Leonardo and Micro.
Now you can press a pushbutton. Then, you should see some characters on your a text
editor.
A sample of text editor can be seen in Figure below.
Source Code
Contact