Wednesday, March 27, 2013

Cubieboard + arduino (thermometer I²C DS1621)

Exercise 2
Temperature sensor.

Now let me introduce to the integrated circuit DS1621, which is a thermometer with a communication system based on a I²C (Integrated-integrated communication)

Our cubieboard also has I²C connection, but I'm still working on a system to send the information from the senses (arduino) to the brain (cubieboard)

This is the video about the construction of all circuit.
I began, doing the circuit to work with the arduino, and checked.
I send the information to the serial, and show it on the screen

After that I connected the serial cable between the arduino y and cubieboard (the cable is an old cd-cable from my old computer)

Here is the video




and here is the result at the cubieboard


There are a lot of information about how to program the arduinos and I²C


I've changed the library "Protocol", cause it's not really necessary a port, cause in the case of I2C its possible to add few of integrated circuirt at the same ports, which will use the same channel of communications


a little bit of code
[CODE TO READ THE DS1621]

#include <Wire.h>
#include <protocol.h>

#define DEV_ID 0x90 >> 1


Protocol protocolTemperature("Temperature");

void setup()
{
  Serial.begin(9600);//I'm using 9600 bps to comunicate the arduino and the cubieboard
  Wire.begin();
  Wire.beginTransmission(DEV_ID); //connect to DS1621
  Wire.write(0xAC); //Access config
  Wire.write(0x02);
  Wire.beginTransmission(DEV_ID); //restart the DS1621
  Wire.write(0xEE); // start conversion
//read the documentation its a very interesting temperature sensor
}

void loop()
{
 protocolTemperature.send(readTemperature());
}
// This will go to another library
//I love the libraries :)
float readTemperature()
{
int8_t firstByte;
int8_t secondByte;
float temp = 0;
 delay(1000); // give time for measurement
 Wire.beginTransmission(DEV_ID);
Wire.write(0xAA); // read temperature command
Wire.endTransmission();
Wire.requestFrom(DEV_ID, 2);    // request two bytes from DS1621 (0.5 deg. resolution)

firstByte = Wire.read();    // get first byte
secondByte = Wire.read();    // get second byte

temp = firstByte;

if (secondByte)    // if there is a 0.5 deg difference
temp += 0.5;
return (temp);
}


[PROTOCOL.cpp]

void Protocol::send(float value)
{
    char buffer[16];
//NOTE: HOW TO CONVERT A FLOAT TO STRING IN ARDUINO
    dtostrf(value,5,2,buffer);
    Serial.println(_id+":"+buffer);
}

[PROTOCOL.h]
//This class will be growing
class Protocol
{
    public:
        Protocol (String id);
        String getId();
        void send(String msg);
        void send(float value);

    private:
        String _id;
};

The circuit to use I²C is quite simple









2 comments:

  1. Pablo, en la tableta no veo el video ¿dónde lo has puesto?

    ReplyDelete
  2. También lo tengo colgado en youtube :)

    https://www.youtube.com/watch?v=Yt6DiXYnFMY

    ReplyDelete