39
National Taiwan University Department of Electrical Engineering Connected Things I: IoT Sensor Nodes & Arduino Alex Lai, Ph.D. Adjunct Professor [email protected]

Connected Things I: IoT Sensor Nodes & Arduino

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

Connected Things I:

IoT Sensor Nodes & Arduino

Alex Lai, Ph.D.

Adjunct Professor

[email protected]

Page 2: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

2

Learn Microcontrollers, Embedded Systems, and

their roles in the Connected Things, by using

Arduino and LinkIt 7697 as the example(s).

Understand the Basics of Sensors and Analog-

to-Digital Conversions.

Hands-on Connected Things and Sensors

software development through an Embedded

System IDE, including how to open, understand,

comment, upload/run, and edit r programs.

Microcontrollers & Embedded Systems 101

Arduino Overview

Arduino IDE

A Sensors-&-ADC Primer

Hands-On

Session ObjectivesAgenda

Page 3: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

3

A small computer on a

single chip

containing a processor,

memory, and input/output

Typically "embedded"

inside some device that

they control

A microcontroller is often

small and low cost

(relatively)

Microcontrollers 101 (微控制器)

Source: Adapted from www.mikroe.com/chapters/view/1

Page 4: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

4

Microprocessor 微處理器 Microcontroller 微控制器

通稱 CPU Single-chip (單晶片)

目的 廣泛用途運算 特定功能控制

RAM 外部定址 內建 (但難以擴充)

ROM/Flash 外部定址 內建 (但難以擴充)

Built-in I/O 少 多

Expansion 多 少或無

速度 相對高 相對低

功耗 相對高 相對低

Microprocessor vs. Microcontroller

Page 5: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

5

Embedded Systems (嵌入式系統)

Sources: Vahid/Givargis, Embedded Systems Design: A Unified Hardware/Software Introduction, 2000; as well as Wikipedia.org

Apollo Guidance Computer (1966)

shown at Computer History Museum

in CA, USA

An embedded system is a programmed

controlling and operating system with a

dedicated function within a larger

mechanical or electrical system. It is

embedded as part of a complete device

often including hardware and mechanical

parts. (“Computer within a Product”)

Modern embedded systems are often

based on microcontrollers, but ordinary

microprocessors are not uncommon in

complex systems.

Typical Usage in the Real World

Core of most end-point

Connected Things (a.k.a.

“Sensor Nodes”)!

Page 6: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

6

Arduino Overview

Page 7: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

7

An open-source electronics prototyping platform based

on flexible, easy-to-use hardware (i.e. Microcontrollers)

and software. It's intended for artists, designers, hobbyists,

and anyone interested in creating interactive objects or

environments.

What is (an) Arduino

Source: Adapted from the official Arduino Web site (arduino.cc)

因開放、易用與價廉,為許多IoT開發者與創客(Makers) 愛用為簡易型開發板 (Development

Board) 的一部分。

Page 8: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

8

Arduino之父-Massimo Banzi

Open-source electronics prototyping platform

Hardware (Arduino I/O board or “Shield”)

Software (Arduino IDE)

特色免費下載

豐富的感測器函式庫支援

可在Arduino官網取得許多硬體設計檔,依需求調整設計

需遵照姓名標示

採用創用CC許可

適用於簡單嵌入式系統的核心或大型複合系統的一部分

相容、衍生與類似產品眾多

What is Arduino (cont.)

Page 9: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

9

Arduino Product Family

Page 10: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

10

The Arduino Uno R3 Specs

Meet Arduino Uno

Item Description

Microcontoller ATMega 328P 8-bit

Clock 16MHz

SRAM 2KB on chip

EEPROM 1KB on chip

Flash Memory 32KB on chip

Operating Voltage 1.8~5.5V

Input Voltage 7~12V (Limit: 6~20V)

Digital I/O Pins 14 (with #6 as PWM)

Analog Inputs 6

DC Current per I/O 40mA

DC Current 3.3V 50mA

Page 11: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

11

8-bit Microcontroller

Pin Definition & Block Diagram*

The Uno’s Core: Atmel AVR Atmega 328

*Source: ATMega 328/P Datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf)

Page 12: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

12

6 Analog Inputs (電壓)

14 Digital I/O pins

PWM

亮度、馬達轉速、頻率

可用於模擬類比訊號

之後詳述

I2C

串列通訊埠 (Serial

Communication Port)

SDA、SCL

I/O Pins of Arduino Uno

Image from Theory and Practice of Tangible User Interfaces at UC Berkley

Page 13: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

13

Shields are circuit expansion boards stacking on top of an Arduino.

They extend the capabilities of an Arduino.

Examples:

Display

GPS

Motor

Prototype

shieldlist.org

Arduino Shields

Page 14: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

14

Integrated Development Environment

(IDE) for Arduino

Page 15: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

15

IDE: What and Why

An Integrated Development Environment

(IDE) is a software suite that

consolidates basic tools required to

develop and test software. An IDE

normally consists of a source code editor,

build automation tools, and a debugger.

IDE is convenient for Embedded System

and Connected Things Development!

Generally, Connected Things have zero UI

It is very inefficient to make executable

programs on most microcontrollers because

of limited resources (RAM, storage, speed)

Cross Compilation

Much easier (& cheaper) for debugging too

*Image adapted from todbot.com/blog/bionicarduino

Page 16: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

16

設立Arduino 整合開發環境 (IDE)

Download Arduino IDE for your Development Host OS (Windows/Linux/MAC OS) at:

https://www.arduino.cc/en/Main/Software

Page 17: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

17

The two LEDs near the USB connector

blink when data is transmitted

RX blinks when the Target (Uno) is

receiving data

TX blinks when the Target (Uno) is

transmitting data

Compiling turns your program into

binary data (ones and zeros)

Uploading sends the bits through USB

cable to the Arduino

Connect Arduino Uno to IDE Host via Serial (over USB) Port

Source: Adapted from http://www.ladyada.net/learn/arduino/lesson4.html

IDE Host (Your PC)

Target Dev. Board (Arduino etc.)

Page 18: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

18

Connect Arduino Uno to IDE Host via Serial (over USB) PortSelect right Serial (over USB) port and protocol/speed and target type (Uno)

工具序列埠監控視窗

Page 19: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

19

Tips: Status Messages

todbot.com/blog/bionicarduino

Page 20: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

20

First Arduino Program: Blink

Based on simplified C++ (case sensitive).

Typical Program (“sketch”) Structure

void setup() : Run once @ startup

void loop() : Repeat endlessly

How to Make

your Arduino’s

LED Blink??

Page 21: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

21

Data Types

void

boolean

char

unsigned

char

byte

int

unsigned int

word

long

unsigned long

short

float

double

string - char

array

string - object

array

A bit about Arduino Programming

Page 22: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

22

Control Structures

if

if…else

for

switch case

while

do…while

break

continue

return

goto

A bit about Arduino Programming

Page 23: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

23

所用到的API函式解析

Source: Adapted from http://www.fiz-ix.com/2013/02/introduction-to-arduino-serial-communication/ and www.mikroe.com/chapters/view/1

Serial.begin()

Serial.begin(9600)

Serial.print() or

Serial.println()

Serial.read()

Serial.available()

Serial.write()

Serial.parseInt()

pinMode(pin, mode)

OUTPUT

INPUT

INPUT_PULLUP:直接輸出5V

digitalWrite(pin, value)

pin:0~13

Value

• HIGH

• LOW

digitalRead(pin)

pin:0~13

pinMode

• INPUT:讀取值為0

• INPUT_PULLUP:讀取值為1

delay(ms)

Pauses for ms

milliseconds

Page 24: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

24

Put them Altogether

• Complete the sketch (program) below.

• What output will be generated by this program?

• What if the schematic were changed like the right?

www.ladyada.net/learn/arduino

Page 25: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

25

以 Blink為例:

1. 進入setup ()

2. 由上而下依序執行

3. 進入 loop()

4. 由上而下依序執行

5. 重複執行loop()

Arduino 程式執行流程

Exercise: Make your Arduino Uno’s LED Blinking slower or faster (How?)

Page 26: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

26

A Primer on Sensors &

Analog-to-Digital Conversion

Page 27: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

27

Infrared & light sensors

Hall effect sensor and

magnet

Gyroscope

Force

Image

感測器:用來捕捉森羅萬象的『現象』,常見的有

Microphone

Dials on a radio

Temperature

Angle

Accelerometer

Sensors

Q1: 這些Sensors捕捉的現象是用0與1呈現的嗎?

Q2: Connected Things如何”看懂” Sensors的讀數?

Page 28: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

28

Analog to Digital Conversion (ADC)

森羅萬象的「現象」是類比 (Analog) 信號, 數位系統 (含IoT)

要擷取它們必須將「現象」轉化為數位 (Digital) 「資料」

Real World Example: Digital Recording

Audio, Image, and Video

Image source: Wikipedia.org

Page 29: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

29

Outside the Scope of this Course but worth Learning

ADC – How It Works

Page 30: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

30

Quantization the SignalWith Errors (Quantization Noise), inevitably

Image source: Wikipedia.org

Page 31: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

31

Resolution: the number of different voltage levels (i.e., states) used to discretize an input signal

Arduino uses 1024 states (10 bits)

Sample Rate: Maximum speed of Arduino: 10,000 times/sec

Arduino’s Built-in ADC

Image credit: Tod Kurt

Exercise:

(1) What is the smallest measurable voltage change by an Arduino Uno? LinkIt 7697?

(2) How many bytes will an Arduino analog input accumulates in 1 second, maximally?

(3) What is the SNR (Signal-to-noise Ratio) in Arduino?

Page 32: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

32

Arduino Code

getDHT22.ino

Analog Sensing Exercise:溫溼光度感測 (顯示於OLED螢幕上)

Page 33: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

33

Grove - Temperature & Humidity Sensor

溼度感應器為數位訊號輸入輸出,此範例連接到 D2

OLED 顯示器使用 I2C 訊號getDHT_OLED.ino

Analog Sensing Exercise:溫溼光度感測 (顯示於OLED螢幕上)

OLED Display 0.96

Grove - Temperature & Humidity Sensor

Page 34: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

34

光感應器此範例接到A0

程式碼

getLight.ino

Analog Sensing Exercise:溫溼光度感測 (顯示於OLED螢幕上)

Page 35: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

35

Live!

Page 36: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

36

https://www.arduino.cc/en/Reference/HomePage

Arduino 官網資源

*Additional References cand be found at: www.ladyada.net/learn/Arduino; and

www.EarthshineElectronics.com

Page 37: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

37

LinkIt 7697 Arduino 討論區於 Labs Forum

https://forum.labs.mediatek.com/

Arduino Add-on 包 Source Code 可於 GitHub 取得

https://github.com/MediaTek-Labs/Arduino-Add-On-for-LinkIt-SDK

原始碼、問題回報與討論

Page 38: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

38

Microcontrollers (微控制器、 “單晶片”) 是將運算單元,記憶體,以及必須之I/O封裝於單一晶片。因其成本、功耗與整合度等優勢,經常用於以 Embedded Systems (嵌入式系統) 為核心之簡易型 Connected Things (所謂的Sensor Nodes) 作為其基本架構。

森羅萬象的「現象」是各種類比 (Analog) 信號, 數位系統 (包含 Connected Things) 要擷取它們必須透過Analog-to-Digital

Conversion (ADC,類比至數位轉換) 將「現象」轉化為數位「資料」,方能進一步處理與運用。

透過 Arduino 實務操作經驗,瞭解由 IDE 開發 Sensor Node

的基本操作。

Session Summary & Takeaways

Page 39: Connected Things I: IoT Sensor Nodes & Arduino

National Taiwan UniversityDepartment of Electrical Engineering

39

Q&A