Você está na página 1de 19

Sensores Digitais e

Protocolos de Comunicação
Sensores Digitais
 Muitos sensores são considerados do tipo digitais não porque possuem
dois distintos estados (0/1), mas porque utilizam as portas da placa de
prototipagem eletrônica para comunicação entre ambos.
 Alguns protocolos de comunicação que o Arduino suporta são: interface
serial, One-Wire, SPI e I2C.

One-Wire

SPI

I2C
Exemplo da aula passada
#include <FreqCount.h>
/*Incluir biblioteca FreqCount-1.3.0.zip
no Arduino IDE*/
// pino 5 é o de entrada na biblioteca FreqCount
int valorrpm = 0;
int tempodeciclo = 200; //em milissegundos
int numeroressaltos = 4;

void setup() {
Serial.begin(57600);
FreqCount.begin(tempodeciclo);
}

void loop() {
if (FreqCount.available()) {
unsigned long numeropulsos = FreqCount.read();
valorrpm = (60*numeropulsos*1000)/(tempodeciclo*numeroressaltos);
Serial.print("Rpm: ");
Serial.println(valorrpm);
}
}
Exemplo de código 1
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C


//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime; Tensão de alimentação: 1.8 - 5V DC


Interface: I2C (até 3.4MHz), SPI (até 10 MHz)
void setup() {
Faixas de medição:
Serial.begin(9600); Temperatura: -40 a +85°C
Serial.println(F("BME280 test")); Umidade: 0-100%
bool status; Pressão: 300-1100 hPa (1 hPa = 100 Pa)
// default settings
// (you can also pass in a Wire library object like &Wire2) Resolução:
status = bme.begin(); Temperatura: 0.01°C
if (!status) { Umidade: 0.008%
Serial.println("Could not find a valid BME280 sensor, check wiring!"); Pressão: 0.18Pa
while (1);
Precisão:
}
Temperatura: +-1°C
Serial.println("-- Default Test --"); Umidade: +-3%
delayTime = 1000; Pressão: +-1Pa
Serial.println();
delay(100); // let sensor boot up Endereço I2C: SDO LOW : 0x76 SDO HIGH:
} 0x77
Exemplo de código 1
void loop() {
printValues();
delay(delayTime);
}

void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");

Serial.print("Pressure = ");

Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");

Serial.print("Approx. Altitude = ");


Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");

Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");

Serial.println();
}
Exemplo de código 2
#include <Ultrasonic.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 13

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

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

void loop()
{
float cmMsec, inMsec; Tensão de Alimentação: 5VDC
long microsec = ultrasonic.timing(); Corrente quiescente: < 2mA
Corrente em funcionamento: 15mA
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM); Ângulo de medida: < 15°
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN); Distância de detecção: de 2cm a 400cm
Serial.print("MS: "); Resolução: 3mm
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
}
Exemplo de código 3
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();

if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
Exemplo de código 3
void loop () {
DateTime now = rtc.now();

Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

Serial.print(" since midnight 1/1/1970 = ");


Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");

// calculate a date which is 7 days and 30 seconds into the future


DateTime future (now.unixtime() + 7 * 86400L + 30);

Serial.print(" now + 7d + 30s: ");


Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();
delay(3000);
}
Datalogger e Arduino

Depende do
modelo do
módulo
Datalogger e Arduino
#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:


if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
Datalogger e Arduino
void loop() {
// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:


for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:


if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
Sensores e Datalogger
 Para usar os mais diferentes sensores com o datalogger, basta unirmos
os códigos, fazendo-se ajustes e evitando os possíveis conflitos.

+
Datalogger de RPM #include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

#include <FreqCount.h> void setup() {


// Open serial communications and wait for port to open:
/*Incluir biblioteca FreqCount-1.3.0.zip Serial.begin(9600);
no Arduino IDE*/ while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
// pino 5 é o de entrada na biblioteca FreqCount }
int valorrpm = 0;
int tempodeciclo = 200; //em milissegundos Serial.print("Initializing SD card...");
int numeroressaltos = 4; // see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {

+
Serial.println("Card failed, or not present");
void setup() { // don't do anything more:
Serial.begin(57600); return;
}
FreqCount.begin(tempodeciclo); Serial.println("card initialized.");
}
} void loop() {
// make a string for assembling the data to log:
String dataString = "";
void loop() {
if (FreqCount.available()) { // read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
unsigned long numeropulsos = FreqCount.read(); int sensor = analogRead(analogPin);
dataString += String(sensor);
valorrpm = (60*numeropulsos*1000)/(tempodeciclo*numeroressaltos); if (analogPin < 2) {
Serial.print("Rpm: "); dataString += ",";
}
Serial.println(valorrpm); }
}
// open the file. note that only one file can be open at a time,
} // so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:


if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
Datalogger de RPM
#include <FreqCount.h>
/*Incluir biblioteca FreqCount-1.3.0.zip void loop() {
no Arduino IDE*/ if (FreqCount.available()) {
// pino 5 é o de entrada na biblioteca FreqCount unsigned long numeropulsos = FreqCount.read();
int valorrpm = 0; valorrpm = (60*numeropulsos*1000)/(tempodeciclo*numeroressaltos);
int tempodeciclo = 1000; //em milissegundos Serial.print("Rpm: ");
int numeroressaltos = 4; Serial.println(valorrpm);
// make a string for assembling the data to log:
#include <SPI.h> String dataString = "";
#include <SD.h> dataString += String(valorrpm);
dataString += ",";
const int chipSelect = 4;
// open the file. note that only one file can be open at a time,
void setup() { // so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// Open serial communications and wait for port to open:
Serial.begin(57600); // if the file is available, write to it:
FreqCount.begin(tempodeciclo); if (dataFile) {
dataFile.println(dataString);
while (!Serial) { dataFile.close();
; // wait for serial port to connect. Needed for native USB port only // print to the serial port too:
} Serial.println(dataString);
}
// if the file isn't open, pop up an error:
Serial.print("Initializing SD card..."); else {
Serial.println("error opening datalog.txt");
// see if the card is present and can be initialized: }
if (!SD.begin(chipSelect)) { }
Serial.println("Card failed, or not present"); }
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
Datalogger de RPM com horário

+
Datalogger de RPM com horário

Código em anexo!
Múltiplos Sensores Digitais
 É possível compartilhar portas quando há sensores que utilizam mesmo
protocolo (SPI ou I2C).
 Para tanto, é necessário que se façam ajustes em software e conexões
físicas.
Exercício
 Atividades no Laboratório de montagem e testes do Datalogger de RPM
com horário.
Bibliografia
Bibliografia Básica:
ALVES, José Luiz Loureiro. Instrumentação, controle e automação de
processos. 2.ed. Rio deJaneiro: LTC, 2010.
BALBINOT, Alexandre; BRUSAMARELLO, Valner João. Instrumentação e
fundamentos de medidas. 2.ed. São Paulo: LTC , 2010. v.1
FIALHO, Arivelto Bustamante. Instrumentação Industrial: conceitos,
aplicações e análises. 6. ed. São Paulo: Érica, 2010.

Bibliografia Complementar:
ALBERTAZZI, Armando; SOUZA, André Roberto de. Fundamentos de
metrologia científica e industrial. Barueri: Manole, 2008.
BEGA, Egídio Alberto (org.). Instrumentação industrial. 2.ed. Rio de Janeiro:
Interciência, 2006.
BOLTON, Willian. Mecatrônica: uma abordagem multidisciplinar. 4. ed. Porto
Alegre: Bookman, 2010.
LIRA, Francisco Adval de. Metrologia na indústria. 7.ed. São Paulo: Érica,
2010.
SOISSON, Harold E. Instrumentação industrial. 2.ed. São Paulo: Hemus.
1991.

Você também pode gostar