Radio Frequency module plus Sonar

Hi there, I am working on a project that needs sonar (I am using a HC-SR04) and RF transmitter and receiver between two Arduinos. Is it possible to use both of these at the same time or do you expect interference? I can get both of these things to work individually, but when I combine them nothing works!

Radio waves and sound waves do not interfere with each other, but it is possible that the code for the radio may conflict with code for the sonar (use of internal timers, for example).

The Arduino can do only one thing at a time, but can switch between tasks so rapidly that they may seem to be running simultaneously.

For hints on how to post and get help, see "How to get the best out of the forum" at the head of every forum topic.

LoPu00092:
I can get both of these things to work individually, but when I combine them nothing works!

Without seeing your programs it is impossible to see what the problem might be,

...R

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :slight_smile:

Hey folks, thanks very much for your responses. Here is my code right now, everything works perfect for the sonar sensor until I add in the bottom if statement which is what the RF transmitter is sending to the other Arduino with the receiver. Any help is greatly appreciated :))

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
#include <SoftwareSerial.h>
RH_ASK driver;
SoftwareSerial BTserial(0, 1); // RX | TX


const int trigPin = 9;
const int echoPin = 10;
unsigned long previousTime = 0;
float seconds ;
byte minutes ;
byte hours ;
int timer = 0;
int counter = 0;
long duration;
int distance;


void setup()
{
    Serial.begin(9600);	  // Debugging only
    pinMode(trigPin, OUTPUT); // Sets the input for the sonar sensor
    pinMode(echoPin, INPUT);  // Sets the input for the sonar sensor
    BTserial.begin(9600); //Starts the bluetooth serial communication
    if (!driver.init())
         Serial.println("init failed");
    Serial.println("Setup Complete");
}


void loop()
{
      // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
  Serial.println(distance);
  
  if(distance<30){
       const char *msg = "Triggered";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    Serial.println("Sent!");
    delay(1);
}
}

Please describe what the program is supposed to do, and what it does instead. Also, post a wiring diagram and links to the modules you are using.

The goal of the program is to get the value in cm from the Sonar sensor HC-SR04 sensing proximity, and to then relay that number to another Arduino that has the receiver end of the radio frequency 433mhz module

LoPu00092:
The goal of the program is to get the value in cm from the Sonar sensor HC-SR04 sensing proximity, and to then relay that number to another Arduino that has the receiver end of the radio frequency 433mhz module

Okay, now the schematic and links please...

LoPu00092:
everything works perfect for the sonar sensor until I add in the bottom if statement which is what the RF transmitter is sending to the other Arduino with the receiver.

You are not giving enough detail about what the program does when the IF statement causes a problem.

For example does the line Serial.println(distance); work?
Does the receiver get a message?
And anything else you can tell us.

What is the line driver.waitPacketSent(); supposed to do? Is it waiting for an acknowledgement from the receiver? If so maybe it is not getting it and the problem lies in the receiver?

My wild guess is that you are trying to send the messages too often. Try inserting delay(1000); as the last thing in loop();

...R