3 jul 2012

Sensor de temperatura MLX90614

Otro sensor de temperatura. Este a diferencia de los demas mide la temperatura por infrarrojos por lo que no necesita contactar con el cuerpo para medir la temperatura. También funciona con el protocolo i2c. Por si se necesitan más detalles aqui va el datasheet del mlx90614

Actualizado:


Y aqui el código .ino para el arduino

#include <Wire.h>
 
void setup(){
Serial.begin(9600);
Serial.println("Setup...");

i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups
}
 
void loop(){
    int dev = 0x5A<<1;
    int data_low = 0;
    int data_high = 0;
    int pec = 0;
 
    i2c_start_wait(dev+I2C_WRITE);
    i2c_write(0x07);
 
    // read
    i2c_rep_start(dev+I2C_READ);
    data_low = i2c_readAck(); //Read 1 byte and then send ack
    data_high = i2c_readAck(); //Read 1 byte and then send ack
    pec = i2c_readNak();
    i2c_stop();
 
    //This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
    double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
    double tempData = 0x0000; // zero out the data
    int frac; // data past the decimal point
 
    // This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
    tempData = (double)(((data_high & 0x007F) << 8) + data_low);
    tempData = (tempData * tempFactor)-0.01;
 
    float celcius = tempData - 273.15;
    float fahrenheit = (celcius*1.8) + 32;
 
    Serial.print("Celcius: ");
    Serial.println(celcius);
 
    Serial.print("Fahrenheit: ");
    Serial.println(fahrenheit);
 
    delay(1000); // wait a second before printing again
}


Os dejo los enlaces para descargar el archivo fritzing y el código fuente

2 jul 2012

Pulsadores y leds

Seguimos con el ABC de arduino. En este caso implementaremos dos pulsadores y tres leds. Un pulsador contará en binario mediante los leds rojo cuantas veces lo pulsamos, mientras el otro pulsador, incrementará la intensidad de la luz del led amarillo.

Por ello vamos a poner antes de nada el código fuente del archivo .ini.

const int ledA = 11;
const int ledB = 10;
const int ledC = 9;

const int pushButton1 = 7;
const int pushButton2 = 6;

int pushed = 0;
int count = 0;

void setup(){

pinMode( ledA, OUTPUT);
pinMode( ledB, OUTPUT);
pinMode( ledC, OUTPUT);

pinMode( pushButton1, INPUT);
pinMode( pushButton2, INPUT);
}

void loop(){

int pushedA = digitalRead( pushButton1 );
int pushedB = digitalRead( pushButton2 );

if( pushedA == HIGH ){
pushed++;
if( pushed > 4 ) pushed = 0;
analogWrite( ledC, pushed*255/4 );
}

if( pushedB == HIGH ){
count++;
if( count > 4 )
count=0;

switch( count ){
case 1: showCount( 0, 0); break;
case 2: showCount( 0, 255); break;
case 3: showCount( 255, 0); break;
case 4: showCount( 255, 255); break;
}
}

delay(250);
}

void showCount(int a, int b ){
analogWrite( ledA, a );
analogWrite( ledB, b );
}


Y os dejo el videotutorial por si lo quereis seguir.

Y como no por si queres podeis descargar también los archivos fritzing e .ino