Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014...

30
Introduªo Sistemas SOM (System On Module) Plataforma Arduino Descriªo de perifØricos Introduªo plataforma Arduino PARTE I Prof. Dr. Juan C . Cutipa Luque Departamento de Engenharia ElØtrica Faculdade SATC Associaªo Benecente da Indoestria Carbonfera de Santa Catarina Workshop em Engenharia ElØtrica - 2014 Faculdade SATC, Cricioema - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C . Cutipa Luque VSNT

Transcript of Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014...

Page 1: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Introdução à plataforma ArduinoPARTE I

Prof. Dr. Juan C. Cutipa Luque

Departamento de Engenharia ElétricaFaculdade SATC

Associação Beneficente da Indústria Carbonífera de Santa Catarina

Workshop em Engenharia Elétrica - 2014Faculdade SATC, Criciúma - Santa Catarina

21-22 de outubro, 2014

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 2: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Conteúdo

1 Introdução

2 Sistemas SOM (System On Module)

3 Plataforma Arduino

4 Descrição de periféricos

5 Exemplos

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 3: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Qual é o propósito?

Page 4: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

SOM - System On Module

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 5: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Plataforma Arduino

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 6: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Periféricos

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 7: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 1

Ligar o arduíno na porta USB. Abra os software e na aba e configure"Tools>Board>Arduino Uno". Utilize os ícones "Verify"e "Upload"paracompilar e carregar o código no arduíno.

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 8: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

void setup() {// put your setup code here , to run once:

}

void loop() {// put your main code here , to run repeatedly:

}

Page 9: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 2

Led intermitente pela saída digital 13. Logo de comprovar ofuncionamento correto, mude o "delay"para 500.

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 10: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

/*BlinkTurns on an LED on for one second , then off for one second ,

repeatedly.

This example code is in the public domain.*/

// Pin 13 has an LED connected on most Arduino boards.// give it a name:int led = 13;

// the setup routine runs once when you press reset:void setup() {

// initialize the digital pin as an output.pinMode(led , OUTPUT);

}

// the loop routine runs over and over again forever:void loop() {

digitalWrite(led , HIGH); // turn the LED on (HIGH is the voltagelevel)

delay (1000); // wait for a seconddigitalWrite(led , LOW); // turn the LED off by making the

voltage LOWdelay (1000); // wait for a second

}

Page 11: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 3

Trabalhando com número flotantes e os possíveis erros de aproximação(adaptado de [1]). Utilize o visualizar de dados (Tools>Serial Monitor).

Tipo de número | bytes | faixa | uso

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 12: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

/** Floating -point example* This sketch initialized a float value to 1.1* It repeatedly reduces the value by 0.1 until the value is 0*/

float value = 1.1;

void setup(){

Serial.begin (9600);}

void loop(){

value = value - 0.1; // reduce value by 0.1 each time throughthe loop

if( value == 0)Serial.println("The value is exactly zero");

else if(almostEqual(value , 0)){

Serial.print("The value ");Serial.print(value ,7); // print to 7 decimal placesSerial.println(" is almost equal to zero");

}else

Serial.println(value);

delay (100);}

// returns true if the difference between a and b is small// set value of DELTA to the maximum difference considered to be

equalboolean almostEqual(float a, float b){

const float DELTA = .00001; // max difference to be almost equalif (a == 0) return fabs(b) <= DELTA;if (b == 0) return fabs(a) <= DELTA;return fabs((a - b) / max(fabs(a), fabs(b))) <= DELTA ;

}

Page 13: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Continuação// returns true if the difference between a and b is small// set value of DELTA to the maximum difference considered to be

equalboolean almostEqual(float a, float b){

const float DELTA = .00001; // max difference to be almost equalif (a == 0) return fabs(b) <= DELTA;if (b == 0) return fabs(a) <= DELTA;return fabs((a - b) / max(fabs(a), fabs(b))) <= DELTA ;

}

Page 14: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 4

Acionamento de um Led pelo switch da entrada digital. Uso de resistorpull-down (adaptado de [1]).

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 15: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

/*Pushbutton sketcha switch connected to pin 2 lights the LED on pin 13

*/

const int ledPin = 13; // choose the pin for the LEDconst int inputPin = 2; // choose the input pin (for a

pushbutton)

void setup() {pinMode(ledPin , OUTPUT); // declare LED as outputpinMode(inputPin , INPUT); // declare pushbutton as input

}

void loop(){int val = digitalRead(inputPin); // read input valueif (val == HIGH) // check if the input is HIGH{

digitalWrite(ledPin , HIGH); // turn LED on if switch ispressed

}else{

digitalWrite(ledPin , LOW); // turn LED off}

}

Page 16: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 5

Acionamento de um Led pelo switch sem necessidade de resistorpull-down externo (adaptado de [1]).

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 17: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

/*Pullup sketcha switch connected to pin 2 lights the LED on pin 13

*/

const int ledPin = 13; // output pin for the LEDconst int inputPin = 2; // input pin for the switch

void setup() {pinMode(ledPin , OUTPUT);pinMode(inputPin , INPUT);digitalWrite(inputPin ,HIGH); // turn on internal pull -up on the

inputPin}void loop(){

int val = digitalRead(inputPin); // read input valueif (val == HIGH) // check if the input is HIGH{

digitalWrite(ledPin , HIGH); // turn LED OFF}else{

digitalWrite(ledPin , LOW); // turn LED ON}

}

Page 18: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 6

Supressão de falso contato (’debouncing’), causado pelo contatomecânico dos ’switchs’ e pulsadores. O ’sketch’ evita que falsos contatossejam percebidos. Esses falsos contatos se geram nos instantes deabertura e de fechadura dos ’switch’ e dos pulsadores (adaptado de [1]).

Imagens de internet usadas para fins ilustrativos

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 19: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

/** Debounce sketch* a switch connected to pin 2 lights the LED on pin 13* debounce logic prevents misreading of the switch state*/

const int inputPin = 2; // the number of the input pinconst int ledPin = 13; // the number of the output pinconst int debounceDelay = 10; // milliseconds to wait until stable

// debounce returns true if the switch in the given pin is closedand stable

boolean debounce(int pin){

boolean state;boolean previousState;

previousState = digitalRead(pin); // store switch statefor(int counter =0; counter < debounceDelay; counter ++){

delay (1); // wait for 1 millisecondstate = digitalRead(pin); // read the pinif( state != previousState){

counter = 0; // reset the counter if the state changespreviousState = state; // and save the current state

}}// here when the switch state has been stable longer than the

debounce periodreturn state;

}

void setup(){

pinMode(inputPin , INPUT);pinMode(ledPin , OUTPUT);

}

void loop(){

if (debounce(inputPin)){

digitalWrite(ledPin , HIGH);}

}

Page 20: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Continuaçãovoid setup(){

pinMode(inputPin , INPUT);pinMode(ledPin , OUTPUT);

}

void loop(){

if (debounce(inputPin)){

digitalWrite(ledPin , HIGH);}

}

Page 21: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 7

Determinação do tempo em que um ’switch’ é pressionado. O pulsador éconectado na entrada digital 2, como no exemplo anterior (semnecessidade de resistores externos pull-up ou pull-down). Use o monitorserial para visualizar a temporização [1].

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 22: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

/*SwitchTime sketchCountdown timer that decrements every tenth of a secondlights an LED when 0Pressing button increments count , holding button down increases rate

of increment*/const int ledPin = 13; // the number of the output

pinconst int inPin = 2; // the number of the input

pin

const int debounceTime = 20; // the time in millisecondsrequired

// for the switch to bestable

const int fastIncrement = 1000; // increment faster afterthis many

// millisecondsconst int veryFastIncrement = 4000; // and increment even faster

after// this many milliseconds

int count = 0; // count decrements everytenth of a

// second until reaches 0

void setup(){

pinMode(inPin , INPUT);digitalWrite(inPin , HIGH); // turn on pull -up resistorpinMode(ledPin , OUTPUT);Serial.begin (9600);

}

void loop(){

int duration = switchTime ();if( duration > veryFastIncrement)

count = count + 10;else if ( duration > fastIncrement)

count = count + 4;else if ( duration > debounceTime)

count = count + 1;

else{

// switch not pressed so service the timerif( count == 0)

digitalWrite(ledPin , HIGH); // turn the LED on if the countis 0

else{

digitalWrite(ledPin , LOW); // turn the LED off if the countis not 0

count = count - 1; // and decrement the count}

} Serial.println(count);delay (100);

}

// return the time in milliseconds that the switch has been inpressed (LOW)

long switchTime (){

// these variables are static - see Discussion for an explanationstatic unsigned long startTime = 0; // the time the switch state

change was first detectedstatic boolean state; // the current state of the

switch

if(digitalRead(inPin) != state) // check to see if the switch haschanged state

{state = ! state; // yes , invert the statestartTime = millis (); // store the time

}if( state == LOW)

return millis () - startTime; // switch pushed , return time inmilliseconds

elsereturn 0; // return 0 if the switch is not pushed (in the HIGH

state);}

Page 23: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Continuaçãovoid loop(){

int duration = switchTime ();if( duration > veryFastIncrement)

count = count + 10;else if ( duration > fastIncrement)

count = count + 4;else if ( duration > debounceTime)

count = count + 1;

else{

// switch not pressed so service the timerif( count == 0)

digitalWrite(ledPin , HIGH); // turn the LED on if the countis 0

else{

digitalWrite(ledPin , LOW); // turn the LED off if the countis not 0

count = count - 1; // and decrement the count}

} Serial.println(count);delay (100);

}

Page 24: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Continuação// return the time in milliseconds that the switch has been in

pressed (LOW)long switchTime (){

// these variables are static - see Discussion for an explanationstatic unsigned long startTime = 0; // the time the switch state

change was first detectedstatic boolean state; // the current state of the

switch

if(digitalRead(inPin) != state) // check to see if the switch haschanged state

{state = ! state; // yes , invert the statestartTime = millis (); // store the time

}if( state == LOW)

return millis () - startTime; // switch pushed , return time inmilliseconds

elsereturn 0; // return 0 if the switch is not pushed (in the HIGH

state);}

Page 25: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 8

Entrada analógica A0 para utilizar sensor LDR (resistor dependente deluz) [2]. O tempo de liga e desliga do Led 13 mudará em proporção daquantidade de iluminação.

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 26: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

/* Created by David Cuartiellesmodified 30 Aug 2011By Tom Igoe

This example code is in the public domain.

http :// arduino.cc/en/Tutorial/AnalogInput

*/

int sensorPin = A0; // select the input pin for the potentiometerint ledPin = 13; // select the pin for the LEDint sensorValue = 0; // variable to store the value coming from the

sensor

void setup() {// declare the ledPin as an OUTPUT:pinMode(ledPin , OUTPUT);

}

void loop() {// read the value from the sensor:sensorValue = analogRead(sensorPin);// turn the ledPin ondigitalWrite(ledPin , HIGH);// stop the program for <sensorValue > milliseconds:delay(sensorValue);// turn the ledPin off:digitalWrite(ledPin , LOW);// stop the program for for <sensorValue > milliseconds:delay(sensorValue);

}

Page 27: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Exemplo 9

Uso da saída PWM para iluminação de um Led. A frequência do PWM éde aproximadamente 490 Hz. No Arduino Uno e similares, os pinos 5 e 6têm frequência de 980 Hz. No Arduino Leonardo, os pinos 3 e 11trabalham a 980 Hz. O Arduino Due tem DAC (conversores analógicosdigitais).

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 28: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

int ledPin = 9; // LED connected to digital pin 9

void setup() {// nothing happens in setup

}

void loop() {// fade in from min to max in increments of 5 points:for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {

// sets the value (range from 0 to 255):analogWrite(ledPin , fadeValue);// wait for 30 milliseconds to see the dimming effectdelay (30);

}

// fade out from max to min in increments of 5 points:for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {

// sets the value (range from 0 to 255):analogWrite(ledPin , fadeValue);// wait for 30 milliseconds to see the dimming effectdelay (30);

}}

Page 29: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Introdução Sistemas SOM (System On Module) Plataforma Arduino Descrição de periféricos Exemplos

Referências

[1] Michael Margolis. Arduino Cookbook. Second Edition. Publisher:O’Reilly Media. 2011.[2] Arduino. Página web oficial. www.arduino.cc.

Prof. Dr. Juan C. Cutipa Luque VSNT

Page 30: Introdução à plataforma ArduinoFaculdade SATC, Criciúma - Santa Catarina 21-22 de outubro, 2014 Prof. Dr. Juan C. Cutipa Luque VSNT. IntroduçãoSistemas SOM (System On Module)Plataforma

Fim da apresentação.Muito Obrigado!