HC-SR04 ultrasonic sensor motion detection

Hi,
I would like to ask if anyone could help me edit the code for Arduino. My project is about motion detection with HC-SR04 sensor.

I found this code from Mr. Wolfgang Ewald:

int triggerPin = 12;
int echoPin = 11;
int ledPin = 13;
int cmDistancePrevious, cmDistanceCurrent;
const int sensitivity = 20;

void setup () {
pinMode (triggerPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (echoPin, INPUT);
cmDistancePrevious = measureDistance ();
}

void loop () {
cmDistanceCurrent = measureDistance ();

if (abs (cmDistanceCurrent - cmDistancePrevious)> sensitivity) {
digitalWrite (ledPin, HIGH);
delay (1000);
digitalWrite (ledPin, LOW);
cmDistanceCurrent = measureDistance ();
}
cmDistancePrevious = cmDistanceCurrent;
delay (50);
}

int measureDistance () {
unsigned long duration = 0;

digitalWrite (triggerPin, HIGH);
delayMicroseconds (10);
digitalWrite (triggerPin, LOW);
duration = pulseIn (echoPin, HIGH);

int cmDis = duration * 0.03432 / 2;

if (cmDis> 400) {
cmDis = 400;
}

return cmDis;
}

I would like to insert a 433 Mhz transmitter into this code (I have Transmitter STX882 + Receiver SRX882) so that it can do this:

After detecting movement by the sensor, the transmitter sends a character such as "1".
Wait for 10 seconds, for example.
Thats all :slight_smile:

I tried to program this, but I'm not a programmer so the code doesn't work at all. Would anyone help with that?

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Is the motion that you are trying to detect across the field of the detector or backwards and forwards to and from the detector ?

"I tried to program this", you say. Please post the code that you tried to make.

You want someone to help you merge two codes, one for the distance sensor, one for the transmitter. You haven't shown us the transmitter code.

Ideally it should be such that, for example at a distance of 2 meters, detect an object - do not detect an object that is, for example, 0.6 meters and 2.8 meters away. Ideally :slight_smile: But mainly I can't add a transmitter :slight_smile:

I'm sorry, but I'd rather not publish, it was just a not good attempt :slight_smile:

You at least have to tell us how you performed 433, e.g. provide a link to the library?

In the introductory threads in the "Programming Questions" sub forum, there are some links to tutorials about merging codes. Have you seen those?

Also, have you put together separate, working sketches that successfully run distance sensor, and 433 MHz, respectively? Tested and working with hardware?

Library for 433 is : VirtualWire.h

Here is one code :

#include <VirtualWire.h>

#define PIN_MOTION 2
#define PIN_LIGHT 13
#define PIN_TX 12

char *message;

void setup() {
  pinMode(PIN_LIGHT, OUTPUT);
  pinMode(PIN_MOTION, INPUT);

  digitalWrite(PIN_LIGHT, LOW);

  vw_set_ptt_inverted(true);
  vw_set_tx_pin(PIN_TX);
  vw_setup(2000);

  delay(1000);
}

void loop(){
  byte input = digitalRead(PIN_MOTION);

  if (input == HIGH){
    digitalWrite(PIN_LIGHT, HIGH);

    message = "1" ;
    vw_send((uint8_t *)message, strlen(message));
    vw_wait_tx();
   delay(10);
  } else {
    digitalWrite(PIN_LIGHT, LOW);
    
  }
}

Great, but you didn't answer all my questions. Please read carefully.

I'm a complete beginner, I don't know how to deal with it. Yes I tried the hadrware separately with the codes, they work fine. I just can't put the transmitter and sensor together.

Good, they both work. So please post the transmitter sketch. Really, you can not expect us to help you merge a sketch that we can't see.

I'm sorry, I had a mistake in the code, I fixed it. This is example code where the transmitter is used, which sent "1" when movement is detected on the sensor.

#include <VirtualWire.h>

#define PIN_MOTION 2
#define PIN_LIGHT 13
#define PIN_TX 12

char *message;

void setup() {
  pinMode(PIN_LIGHT, OUTPUT);
  pinMode(PIN_MOTION, INPUT);

  digitalWrite(PIN_LIGHT, LOW);

  vw_set_ptt_inverted(true);
  vw_set_tx_pin(PIN_TX);
  vw_setup(2000);

  delay(1000);
}

void loop(){
  byte input = digitalRead(PIN_MOTION);

  if (input == HIGH){
    digitalWrite(PIN_LIGHT, HIGH);

    message = "1" ;
    vw_send((uint8_t *)message, strlen(message));
    vw_wait_tx();
   delay(10);
  } else {
    digitalWrite(PIN_LIGHT, LOW);
    
  }
}

As I have already described, I would like to use ultrasonic sensor to detect motion, so that the transmitter then sends, for example, "1".
I don't know how to " mount " transmitter in this code for example :

int triggerPin = 12;
int echoPin = 11;
int ledPin = 13;
int cmDistancePrevious, cmDistanceCurrent;
const int sensitivity = 20;

void setup () {
pinMode (triggerPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (echoPin, INPUT);
cmDistancePrevious = measureDistance ();
}

void loop () {
cmDistanceCurrent = measureDistance ();

if (abs (cmDistanceCurrent - cmDistancePrevious)> sensitivity) {
digitalWrite (ledPin, HIGH);
delay (1000);
digitalWrite (ledPin, LOW);
cmDistanceCurrent = measureDistance ();
}
cmDistancePrevious = cmDistanceCurrent;
delay (50);
}

int measureDistance () {
unsigned long duration = 0;

digitalWrite (triggerPin, HIGH);
delayMicroseconds (10);
digitalWrite (triggerPin, LOW);
duration = pulseIn (echoPin, HIGH);

int cmDis = duration * 0.03432 / 2;

if (cmDis> 400) {
cmDis = 400;
}

return cmDis;
}

I am also sorry for my English :slight_smile:

The way to see it:

At some point in the US sketch, a defined condition is met (i.e. something is detected in your desired range). Find that point.

At some point in the 433 sketch, a message is sent. Find that point.

Combine all the hardware, library, and initialization code in setup() and loop() into one sketch (anything in setup(), put in setup()...etc.).

Now, put those points together. Use an 'if', e.g.

if (measured distance < upperlimit and measured distance > lowerlimit)
{
send a 433 message
}

Your program will loop like:

loop
{
check US
if US in range, send a message
}

Try to do it, if you have problems, post the new sketch.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.