Reading pulses

I'm working on a small project using the arduino with a 433MHz receiver to detect wireless signals from smoke detectors. The smoke detectors are connected wirelessly in series so when one triggers it triggers the rest of the detectors. From what I understand the signal consists of a start signal (8ms high + 1ms low) and then either 800us high + 1400us low, or 800us high + 2800us low for a total of 25 pulses. I found this out connecting the 433MHz receiver to an oscilloscope. The goal of the project is to have the arduino connected to ethernet or even gsm and to send sms or email when the smoke detectors triggers, but so far I'm working on getting the arduino to recognize the signals from the detectors.

I understand that I need a code that recognizes this start condition, and then decides if a pulse is "1" or "0" for the following 25 bits of data and then consecutively writes the bit to an array or to a long integer, but I can't even get it to recognize the start pulse correctly. It just prints out random numbers.

The following code was just to find out if I use the pulseIn function correctly (which I obviously don't):

#define rxPin 8

long durationH, durationL; // duration of high & low pulse
long rx = 0;
boolean good = 0; // wether or not a pulse has the correct duration.

void setup() {
  Serial.begin(9600);
  pinMode(rxPin, INPUT);
}

void loop() {
  rx = receiveData();
  Serial.println(rx);
  delay(1000);

}

const unsigned long timeOut = 5000000; // 5 seconds

long receiveData() {
  durationH, durationL = 0;
  good = 0;
  while (!good) { // loop untill it receives a 8ms high pulse or timeout
    durationH = pulseIn(rxPin, HIGH, timeOut); // wait for high pulse
    if ( 7000 <= durationH <= 9000) { // check if high pulse is ~8ms
      good = 1;  // if high pulse is ~8ms then break while loop and continue to wait for 1ms low pulse
    }
    if (durationH == 0) return -1; // if timeout return with "error code" -1
  }

  good = 0;

  while (!good) { // loop untill it receives a 1ms low pulse or timeout
    durationL = pulseIn(rxPin, LOW, timeOut);
    if ( 900 <= durationL <= 1000) {
      good = 1;
    }
    if (durationL == 0) return -2;
  }
  return durationL;
}

Try the RCSwitch library with your 433Mhz receiver.
Use the receive example(s) that comes with it.
If you're lucky, it prints the code (numbers) in the serial monitor.
Leo..

Wawa:
Try the RCSwitch library with your 433Mhz receiver.
Use the receive example(s) that comes with it.
If you're lucky, it prints the code (numbers) in the serial monitor.
Leo..

I will try that, but if I was to use pulseIn to do this (recognize a high pulse of certain length followed by a low pulse of certain length, both with a specified margin of error) and return with error if timeout?, how should I do that?

I've decided to try another approach by using interrupts to detect the pulses and record the signal. From what I understand the start pulse is 8000us HIGH followed by 1000us LOW, and then the data follows. The data is encoded as 800us HIGH followed by 1400us LOW or 2800us LOW, defining a "0" or "1" (or the other way around).

I decided to just try something simple with interrupts like detecting the start signal (8000us + 1000us) and then printing the duration, but I can't even get that to work, it just spews out random numbers, even negative ones even though the type is defined as unsigned long.

const byte rxPin = 2;

volatile unsigned long stop, start, duration = 0;



void setup() {
Serial.begin(9600);
pinMode(rxPin, INPUT);
attachInterrupt(0, rise, RISING);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void rise(){
  stop = start; // time of previous rising edge
  start = micros(); // time of current rising edge
  duration = stop - start;
  if (8800 <= duration <= 9200) printSomething(duration); // print the duration of the signal only if it's ~9000us
}

void printSomething(int data) {
  noInterrupts();
  Serial.println(data);
  delay(1000);
  interrupts();
}

All 433Mhz receivers I know are outputting pulses all the time (noise).
And many car remotes, garage door openers, toys, remotes etc. use this frequency.
Dangerous to use a simple approach.
The way I told you spits out an exact number that can be used in an if statement.
AFAIK the RCSwitch code used interrupts (use the interrupt pin that's in the example code).
Leo..

Wawa:
All 433Mhz receivers I know are outputting pulses all the time (noise).
And many car remotes, garage door openers, toys, remotes etc. use this frequency.
Dangerous to use a simple approach.
The way I told you spits out an exact number that can be used in an if statement.
AFAIK the RCSwitch code used interrupts (use the interrupt pin that's in the example code).
Leo..

I tried the examples with the RCSwitch library, but I couldn't get it to work.

I also tried to look at the signal from receiver on an oscilloscope and it was indeed very noisy, however the noise was only short spikes of tens of us, nothing close to 9ms pulses. I don't understand why the code I tried doesn't work and even prints numbers that shouldn't be possible (negative numbers, when the variable is defined as unsigned).

What is printed on the transmitter chip inside the smoke alarm.
Maybe a picture if you're not sure.
Leo..

Wawa:
All 433Mhz receivers I know are outputting pulses all the time (noise).

Leo..

Most only output when the button is pressed.

This can often be a problem.
Measure the current consumption of you receiver and compare to specs.

Standby and operating current are often very different.

A high current consumption when tx is not operating would indicate interference.

Lars81:
I also tried to look at the signal from receiver on an oscilloscope and it was indeed very noisy, however the noise was only short spikes of tens of us, nothing close to 9ms pulses. I don't understand why the code I tried doesn't work and even prints numbers that shouldn't be possible (negative numbers, when the variable is defined as unsigned).

You have a scope , big advantage. those spikes i would suggest are interference which may easily interfere with decoding.

Boardburner2:
Most only output when the button is pressed.

The data slicer (output) of cheap AM receivers is constantly switching on the background noise.
Leo..

Wawa:
All 433Mhz receivers I know are outputting pulses all the time (noise).
And many car remotes, garage door openers, toys, remotes etc. use this frequency.
Dangerous to use a simple approach.
The way I told you spits out an exact number that can be used in an if statement.
AFAIK the RCSwitch code used interrupts (use the interrupt pin that's in the example code).
Leo..

I tried the RCSwitch library and the examples that came with it, but I couldn't get it to work. There is noise from the receiver when nothing is transmitting, but when I trigger the alarms it produces a very nice pwm-signal.

Boardburner2:
You have a scope , big advantage. those spikes i would suggest are interference which may easily interfere with decoding.

There are almost no spikes or noise when the alarms transmit. Also the spikes from the background noise are far to short to trigger the print command in the code I made. The strange thing is that it even prints negative numbers sometimes, even though the type is unsigned.