Hello,
This is my first post in the forum, and I am sort of new to Arduino, too.
I was looking for something related to RF data transmission into my Arduino board, but all I have found were Arduino-to-Arduino data exchange projects.
Since I have got only one Arduino board, I had to look for a legacy IC. That's why I came across HT12E, which is suited for what I need.
Basically, it is a 12-bit encoder. Of those 12 bits, 8 bits are the address code, and the remaining 4 bits are data.
Once you set up the address and the data pins, you can send the 12-bit serial stream by driving low the IC's Transmission Enable pin.
I plugged the IC's output to a Telecontrolli RF Transmitter, and an RF Receiver to Arduino's digital pin #7. According to the manufacturer, these tranceivers have a somewhat narrow bandwidth, so I set up the HT12E clock frequency around 3.0~4.0 kHz, which yields a baud rate around 1200 bps.
I wrote a library for decoding the HT12E serial stream, and it follows below.
HT12E.cpp
/*---
HT12E.cpp
HT12E Support for Arduino
Author: Marcelo Shiniti Uchimura
Date : May '08
---*/
#include "WProgram.h"
#include "HT12E.h"
HT12E::HT12E(int pin, unsigned int addrMask)
{
_pin = pin;
pinMode(_pin, INPUT);
_data = 0;
_mask = addrMask << 4; // the HT12E basic word is a stream with an 8-bit address
// followed by 4-bit data. I left shift the
// address mask 4 bits so I can match it to the entire word
}
int HT12E::read()
{
byte ctr; // for general error handling
_tries = 0;
do
{
/* look for HT12E basic word's pilot stream */
for(ctr = 0; ctr < 13; ++ctr)
{
while(digitalRead(_pin) == LOW); // wait for the signal to go HIGH
_dur = pulseIn(_pin, LOW);
if(_dur > 9000 && _dur < 12000) break; // 36x(clock tick interval)
}
/* if error, skip everything */
if(ctr == 13)
{
_tries = 4;
break;
}
/* now wait until sync bit is gone */
for(ctr = 0; ctr < 6; ++ctr)
{
if(digitalRead(_pin) == LOW) break;
delayMicroseconds(80);
}
/* if error, skip everything */
if(ctr == 6)
{
_tries = 5;
break;
}
/* let's get the address+data bits now */
for(_data = 0, ctr = 0; ctr < 12; ++ctr)
{
_dur = pulseIn(_pin, HIGH);
if(_dur > 250 && _dur < 333) // if pulse width is between 1/4000 and 1/3000 secs
{
_data = (_data << 1) + 1; // attach a *1* to the rightmost end of the buffer
}
else if(_dur > 500 && _dur < 666) // if pulse width is between 2/4000 and 2/3000 secs
{
_data = (_data << 1); // attach a *0* to the rightmost end of the buffer
}
else
{
/* force loop termination */
_data = 0;
break;
}
}
// check if buffered data matches the address mask
if((_data & _mask) < _mask)
{
/* data error */
_tries = 6;
}
else ++_tries;
} while(_tries < 3);
if(_tries > 3)
{
switch(_tries)
{
case 4: return 0xffff;
case 5: return 0xfffe;
case 6: return 0xfffd;
}
}
return (_data ^ _mask);
}
HT12E.h
/*---
HT12E.h
HT12E Support for Arduino
Author: Marcelo Shiniti Uchimura
Date : May '08
Note : make sure HT12E is operating at 3~4kHz clock range
---*/
#ifndef HT12E_h
#define HT12E_h
#include "WConstants.h"
class HT12E
{
public:
HT12E(int pin, unsigned int addrMask); // this is the constructor
int read(); // this is the main method
private:
byte _pin; // this is Arduino input pin
unsigned int _data; // this is data
unsigned int _mask; // this is the address mask
byte _tries; // this is how many times Arduino could find
// valid HT12E words
unsigned long _dur; // pulse duration
};
#endif
Usage example
#include <HT12E.h>
HT12E remote(7, B01111111); // pino 7, endereço 0111 1111b
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned int valor;
valor = remote.read();
if(valor > 0xFFF0) Serial.println(valor, HEX);
else
{
Serial.print("DATA ");
Serial.println(valor, BIN);
}
delay(1000);
}
I hope this might be helpful. Cheers!