visit
Cheapest Chinese sensors that you can find on the market (Alibaba, Aliexpress, Taobao, Amazon, etc.)
First, we need to disassemble the sensor case (Picture 5).
Finally, we received 4 different packets. Don't panic if you don't understand, I will describe the first packet in detail with an explanation.
We also see the body of the packet. I have marked only the high length of each signal with red and blue colors. Red represents a long signal (logic 1), while blue represents a short signal (logic 0).
The duration of the long, short, and start signals we will measure later. From this screen, we only need to extract the real binary data from each packet and write it down.
packet 1. 1000 1010 1111 1111 0
packet 2. 1000 1001 1111 1111 0
packet 3. 1000 1000 1111 1111 0
packet 4. 1000 1011 1111 1111 0
Finally, we got interesting data:
// our pin that supports PWM
int inputPin = 3;
// our packet, where we will store our 16bits information about sensor.
unsigned int packet = 0;
// our bit duration, for short = 80nS, long = 220nS and start bit = 1.02mS
unsigned long pulseDuration = 0;
void setup() {
pinMode(inputPin, INPUT);
Serial.begin(2000000);
}
void loop() {
// pulseIn is a build-in function, that waits for HIGH signal.
// We have defined that our logic is PWM-HIGH, that's why we are using HIGH.
// 20.000 is a timeout of waiting for signal.
pulseDuration = pulseIn(inputPin, HIGH, 20000);
// We detect the start packet pulse. In our case the start bit is 1.02mS,
// so it is between these values:
if (pulseDuration > 900 && pulseDuration < 1040) {
// We have successfully detected start bit, now we need to read the body of the packet.
packet = 0;
// Only read 16 bits of data.
for (int i = 15; i >= 0; i--) {
// Read packet (only HIGH bits, zeros are ignored, because we already have it)
pulseDuration = pulseIn(inputPin, HIGH, 3000);
// we detect only LONG bits with 220nS duration (logical 1);
if (pulseDuration > 210 && pulseDuration < 250) {
bitSet(packet, i);
}
}
// Print the packet;
Serial.println(packet, BIN);
}
}
11111
11111
11111
11111
10001000 11111111
10001001 11111111
10001010 11111111
10001000 11111111
// We delete last 8 bits of our packet to get the address value;
byte sensorAddressCheck = (byte)(packet >> 8);
// Using logical operation AND we remove first 8 bits;
byte sensorValueCheck = (byte)(packet & B11111111);
int inputPin = 3;
unsigned int packet = 0;
byte sensorValue = 0;
byte sensorAddress = 0;
unsigned long pulseDuration = 0;
void setup() {
pinMode(inputPin, INPUT);
Serial.begin(2000000);
}
void loop() {
pulseDuration = pulseIn(inputPin, HIGH, 20000);
if (pulseDuration > 900 && pulseDuration < 1040) {
sensorAddress = 0;
sensorValue = 0;
packet = 0;
for (int i = 15; i >= 0; i--) {
pulseDuration = pulseIn(inputPin, HIGH, 3000);
if (pulseDuration > 210 && pulseDuration < 250) {
bitSet(packet, i);
}
}
byte sensorAddressCheck = (byte)(packet >> 8);
byte sensorValueCheck = (byte)(packet & B11111111);
Serial.print("Sensor addr: ");
Serial.print(sensorAddressCheck, BIN);
Serial.print(" value: ");
Serial.println(sensorValueCheck);
}
}
Sensor addr: 10001011 value: 255
Sensor addr: 10000001 value: 80
Sensor addr: 10000010 value: 82
Sensor addr: 10001000 value: 255