Archive

Archive for the ‘Uncategorized’ Category

Serial comunication protocol with arduino

February 19th, 2009 aprendiz 2 comments

A serial comunication example with a medium complex protocol:
I promise to expan it using crcs ;)

#define ledPin 13
#define waitForByte 200 //Time waiting for the next check of available data
#define maxItertionWaiting 20 //Maximun iteration before timeout

// Return values in checkResponse
#define errorTimeout -1
#define errorProtocol -2
#define responseOK 0

// first data to send
byte tSend1[]={0x15, 0x39, 0xF2, 0x78, 0x22, 0x11};
// first data to receive
byte tResp1[]={0x20, 0x1B, 0xF2, 0x79, 0x24, 0x11};
// second data to send
byte tSend2[]={0x25, 0x45, 0xF2, 0x7a, 0x09, 0x11};
// second data to receive
byte tResp2[]={0x30, 0xa0, 0xF2, 0x7b, 0x59, 0x11};

//buffer to put the response in
byte *LastResponse=NULL;

// Initialize everything
void setup()
{
Serial.begin(9600);

// wait a bit
delay(100);

// we will light on when the protocol finnalice ok
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}

// Read the response (in LastResponse
int checkResponse(byte response[],int iResponseSize)
{
int iReceivedCounter=0; // How many byte we have receive
int iReceivedValue=0;
int iIteration=0; // How many iterations we have wait for the last data
int iResult=responseOK;

if(LastResponse!=NULL) //Free memory
free(LastResponse);

LastResponse=(byte *)calloc(iResponseSize,1); //Alloc memory for new data

while(iReceivedCounter
{
if(Serial.available()>0)
{
iIteration=0;
iReceivedValue=Serial.read();
if(iReceivedValue!=response[iReceivedCounter]) // is it the waited byte?
{
iResult=errorProtocol;
break;
}
else
LastResponse[iReceivedCounter++]=(byte)iReceivedValue;
}
else
{
if(iIteration
{
delay(waitForByte); // One more try
iIteration++;
}
else
{
iResult=errorTimeout;
break;
}
}
}
return iResult;
}

void sendData(byte dataToSend[],int iSize)
{
for(int i=0;i
{
Serial.print(dataToSend[i],BYTE);
}
}

void loop()
{
int iImportantValue=0;

sendData(tSend1,6);
delay(200); // allow some time for the Serial data to be sent
if(checkResponse(tResp1,6)==responseOK)
{
sendData(tSend2,6);
delay(200); // allow some time for the Serial data to be sent
if(checkResponse(tResp2,6)==responseOK)
{
iImportantValue=LastResponse[3]*256+LastResponse[2];
digitalWrite(ledPin, HIGH);
}
}
}

Reading temperature with Maxim DS18x20

February 19th, 2009 aprendiz 2 comments

Its really easy to use the ds18x20 family of temperature sensors of Maxim. You can get a pair of them for free from Maxim.

The sensors use a one-wire protocol, only one wire for data and a pair more for gnd and voltage (even only one from data and other for gnd in parasit mode). Each sensor has an unic ID.
You need to search the “network” (filtering only the required sensor type) in order to get all the ids and then ask each one for the current temperature value.

You can send the data to the pc using the serial port.

Here is the example:

#include
/* OneWire Arduino
code from here */

/* DS18S20 Temperature chip i/o */

OneWire ds(10); // on pin 10

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

void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
ds.reset_search();
return;
}

Serial.print("R");
for( i = 0; i < 8; i++) {
Serial.print("-");
if(addr[i]<16)
Serial.print(0);
Serial.print(addr[i], HEX);
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}

if ( addr[0] != 0x10) {
Serial.print("Device is not a DS18S20 family device.\n");
return;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
int HighByte=data[0];
int LowByte=data[1];

// Teniendo LowByte y HighByte
int TReading = (HighByte << 8) + LowByte;
int SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
int Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25

int Whole = Tc_100 / 100; // separate off the whole and fractional portions
int Fract = Tc_100 % 100;

Serial.print(":");
Serial.print(HighByte/2);
Serial.print(".");
Serial.print(LowByte/2);
Serial.print(":");
Serial.print(TReading);

 

Serial.println();
}

More references: Arduino playground, arduino forum & phanderson

Breadboard Tips & Tricks

February 12th, 2009 aprendiz No comments

Talking about money…

February 8th, 2009 aprendiz No comments

Depending of the cash:

Basic Bare Bones System:

  • 2 x RBBB 2 x 11.5$
  • rf Module transmitter 3.95$
  • rfModule receiver 2 x 5.95$
  • 16 x 2 blue character LCD (using parallel conection) 9$
  • meteorological sensors —–

Basic System

  • 2 x RBBB 2 x 11.5$
  • rf Module transmitter 3.95$
  • rfModule receiver 2 x 5.95$
  • 16 x 2 blue character LCD 9$
  • LCD117 Serial LCD 17$
  • meteorological sensors —–

Big system

  • Big Led Matrix
  • 2 x RBBB 2 x 11.5$
  • 2x rf transceiver (bidirectional)
  • LCD117 Serial LCD 17$
Categories: Uncategorized, design Tags: