Using arduino and raspberry pi for internet of things

69
Using Arduino and Raspberry Pi for Internet of Things Sudar Muthu (@sudarmuthu) http://hardwarefun.com / http://github.com/sudar

description

Guest lecture about using Arduino and Raspberry Pi for Internet of Things, that I gave at Anna University.

Transcript of Using arduino and raspberry pi for internet of things

Page 1: Using arduino and raspberry pi for internet of things

Using Arduino and Raspberry Pi for Internet of Things

Sudar Muthu (@sudarmuthu)http://hardwarefun.com/http://github.com/sudar

Page 2: Using arduino and raspberry pi for internet of things

2

Research Engineer by profession I build robots as a hobby Playing with Arduino for more than 4 years Blogger about Arduino at

http://hardwarefun.com Moderator for Arduino India forum

Who am I?

http://hardwarefun.com

Page 3: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 3

Introduce Arduino Introduce Raspberry Pi Emphasis on IoT See how both can be used for IoT

Objective

Page 4: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 4

Arduino

Page 5: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 5

What is Arduino? Visual Basic for hardware Includes both Hardware and software

Photo credit Arduino team

Page 6: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 6

Arduino Uno (The one I am going to use today)

Arduino Mega Arduino Due Lillypad Arduino BT Arduino Ethernet .. and clones

Different Arduino types

Page 7: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 7

Getting to know the Arduino

Page 8: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 8

Specs (Uno, Leonardo)

Type Value

Microcontroller ATmega328

Operating Voltage 5v

Digital I/O Pins 14 (of which 6 provide PWM output)

Analog Input Pins 6

Flash Memory 32 KB (ATmega328) of which 0.5 KB used by bootloader

SRAM 2 KB (ATmega328)

EEPROM 1 KB (ATmega328)

Clock Speed 16 MHz

Page 9: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 9

Microcontroller Power jacket USB jacket Digital pins Analog pins Reset button

Identify these components in Arduino

Page 10: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 10

Voltage Regulator Power Pins (how many are there?) Ground Pins (how many are there?) Vin Pin Rx and Tx Pins ICSP Headers

Identify these components in Arduino

Page 11: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 11

Power Led Rx and Tx Led’s Test Led Crystal Anything else?

Identify these components in Arduino

Page 12: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 12

Powering up Arduino

Page 13: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 13

Using USB cable Using DC power jacket Giving voltage directly into Vin pin Giving regulated voltage directly into 5V pin

Different ways to power up Arduino

Page 14: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 14

Setting up Arduino

Page 15: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 15

Testing the setup with a “Hello World” program

Page 16: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 16

Blinking LED

Page 17: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 17

Insert a LED in pin 13 Open File->Examples->Basics->Blink Select Tools->Boards->Arduino Uno Select File->Upload (or press ctrl+u) You should get the message “Done upload” Your Led should blink Congrats you can program Arduino now

Making a LED blink

Page 18: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 18

Did I miss anything?People with electronics background

Page 19: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 19

Did I miss anything?People with electronics background

Hint: Ohm’s Law

Page 20: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 20

Anatomy of an Arduino sketch

Page 21: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 21

Uno has one UART hardware port, using which we can exchange information with computer

Very useful for debugging Works at a specified baud rate Use Serial Monitor to read values SoftwareSerial is also available

Printing values through Serial

Page 22: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 22

Breadboard Basics

Page 23: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 23

The first two and the last two rows are connected

In all the other rows, columns are connected Connect the first and last row to power Connect the second and second last row to

ground

How to use a breadboard

Page 24: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 24

Digital Input and Output

Page 25: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 25

Digital Input

Page 26: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 26

The LED blink that we did at “setting up Arduino” is Digital output

Digital Output

Page 27: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 27

Analog Input

Page 28: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 28

Connect the LDR on pin A0 and Gnd LDR’s resistance varies based on the amount

of light present Read the current value using analogRead() Print the value in Serial Monitor

Reading Analog values from sensors

Page 29: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 29

Control an LED based on light

void setup(){ pinMode(13, OUTPUT);}

void loop(){ int val = analogRead(A0); if (val > 50) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); }}

Page 30: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 30

Analog Output

Page 31: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 31

What is PWM? Analog like behavior using digital output Works by switching the LED on and off

regularly Changing the brightness of a Led

Analog Output

Page 32: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 32

This is just the tip of an iceberg

There are tons of other features to Arduino which I have not talked about

Page 33: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 33

Internet of Things

Page 34: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 34

"Internet of Things" by Wilgengebroed on Flickr

Page 35: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 35

But I like this definition…

“The Internet of Things is the interconnection of

uniquely identifiable embedded computing devices within the existing Internet

infrastructure”

LoT is an overloaded term

Page 36: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 36

Ethernet Shield WIFI Shield 3G Shield Using another intermediate component

Connecting Arduino to Internet

Page 37: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 37

Demo of network connectivity using Arduino

Page 38: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 38

Let’s take a break

Page 39: Using arduino and raspberry pi for internet of things

Raspberry Pi

Page 40: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 40

Credit Card Sized

Computer

Page 41: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 41

GPIO Pins

http://learn.adafruit.com/assets/3052

Page 42: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 42

Setup Python

sudo apt-get install python-dev

sudo apt-get install python-rpi.gpio

Page 43: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 43

Set the status of GPIO Pins

https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py

Page 44: Using arduino and raspberry pi for internet of things

44

Set the status of GPIO Pins

import RPi.GPIO as GPIOimport time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(12, GPIO.OUT)

try: while True: GPIO.output(12, GPIO.HIGH) time.sleep(1) GPIO.output(12, GPIO.LOW) time.sleep(1)finally: GPIO.cleanup()

https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.pyhttp://hardwarefun.com

Page 45: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 45

Demo

Let there be Light

https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py

Page 46: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 46

Changing the brightness of the LED

import RPi.GPIO as GPIOimport time

GPIO.setmode(GPIO.BOARD)GPIO.setup(12, GPIO.OUT)p = GPIO.PWM(12, 50) # channel=12 frequency=50Hzp.start(0)

try: while True: for dc in range(0, 101, 5): p.ChangeDutyCycle(dc) time.sleep(0.1) for dc in range(100, -1, -5): p.ChangeDutyCycle(dc) time.sleep(0.1)finally: p.stop() GPIO.cleanup()

https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/pwm.py

Page 47: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 47

Demo

Can you see the brightness changing?

https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/pwm.py

Page 48: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 48

Reading the status of the Pin

import RPi.GPIO as GPIOimport time

GPIO.setmode(GPIO.BOARD)GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

try: while True: if GPIO.input(11): print "Button is on" else: print "Button is off" time.sleep(0.1)

finally: GPIO.cleanup()

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py

Page 49: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 49

Reading the status of the Pin

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py

Page 50: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 50

Demo

What happens when the button is pressed?

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py

Page 51: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 51

Combining Input and Output

import RPi.GPIO as GPIOimport time

GPIO.setmode(GPIO.BOARD)GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)GPIO.setup(12, GPIO.OUT)

try: while True: if GPIO.input(11): print "Button is on" GPIO.output(12, 1) else: GPIO.output(12, 0) time.sleep(0.1)

finally: GPIO.cleanup()

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-and-led/button-and-led.py

Page 52: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 52

Combining Input and Output

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-and-led/button-and-led.py

Page 53: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 53

Let’s control the LED by pressing the button

Demo

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-and-led/button-and-led.py

Page 54: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 54

What more can be done?

Page 55: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 55

More protocols

I2C SPI Serial

Page 56: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 56

Interacting with webcam

“PyGame” provides easy interface Can get fancy using “opencv” Both USB and GPIO interface are supported

Page 57: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 57

Distributed Computing

Each Pi can be used as cheap node Form grids using a cluster of Pi’s Can share CPU, memory and disk space

http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/distributed-computing/

Page 58: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 58

Limitations

No built-in Analog to Digital support Can’t run Inductive load (motors) Is not real-time (CPU might be busy) No “safe circuits” present Operates at 3.3V and is not directly

compatible with Arduino voltage

Page 59: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 59

Arduino vs Raspberry Pi for IoT

Page 60: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 60

Entire Linux software stack is available It is very easy to connect to internet Can be programmed using variety of

programming languages

Advantages of Raspberry Pi

Page 61: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 61

Accessing hardware is not real-time. If the CPU is busy, then interfacing with hardware can be delayed

No built-in Analog to Digital converter available

Does not have enough power to drive inductive loads

The hardware design is not open source. Even though it is not a big deal, for some people it might a deal breaker

Disadvantage of Raspberry Pi

Page 62: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 62

Very easy to get started Very easy to extend it and has tons of user

contributed shields and libraries. Shields are available to do pretty much anything

Can be used to for real-time applications Everything (both hardware, software and IDE)

are open source Not much programming knowledge needed to

do basic stuff

Advantages of Arduino

Page 63: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 63

Not very powerful when compared with Raspberry Pi (Micro processor vs Micro controller)

You need to program using either Arduino or C/C++ (or assembly if you really want to)

Connecting to internet is slightly difficult (you have shields and libraries, but is not straight forward), but not impossible.

Disadvantages of Arduino

Page 64: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 64

Feature Raspberry Pi Arduino

Processor Speed 700 MHz 16 MHz

Programming Language

No limit Arduino, C/C++

Real-time Hardware No real-time In real-time

Analog to Digital Convertor

No Yes

Hardware Design Closed source Open source

Internet Connection Very easy Not easy, but doable

In Short..

Page 65: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 65

My Solution?

Page 66: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 66

Use both together

http://learn.adafruit.com/assets/3199 http://learn.adafruit.com/assets/2123

Best of both worlds

Page 68: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 68

Links

Arduino – http://arduino.cc Asimi – A simple bot using Arduino

http://hardwarefun.com/project/asimi Getting started with hardware programming

http://hardwarefun.com/tutorials/getting-started-with-hardware-programming

Getting started with Arduino http://hardwarefun.com/tutorials/getting-started-with-arduino-and-avr

Page 69: Using arduino and raspberry pi for internet of things

http://hardwarefun.com 69

Questions

Thank You

Sudar Muthu (@sudarmuthu)http://hardwarefun.com/

https://github.com/sudar/arduino-robotics-workshophttps://github.com/sudar/raspberry-pi-sketches