RF transmitter and receiver won't work

I am tying to use RF receiver to take the input from a sound sensor then depending on the input send a signal to the receiver and activate a servo I uploaded the code with no problems but when I tested it nothing worked and the servo spun right when I plugged it in
I am using an Arduino Nano

I am using this Transmitter and receiver: HiLetgo 5 Sets 433M Transmitter + Receiver Kit High Frequency Super Regenerative Transceiver Module for Burglar Alarm

A mini servo

a sound sensor

here are the schematics

Here is the code for the transmitter

#include <VirtualWire.h>

int  sensorAnalogPin = A0;    // Select the Arduino input pin to accept the Sound Sensor's analog output
int  sensorDigitalPin = 3;    // Select the Arduino input pin to accept the Sound Sensor's digital output
int  analogValue = 0;         // Define variable to store the analog value coming from the Sound Sensor
int  digitalValue;

void send (char *message)
{
    vw_send((uint8_t *)message, strlen(message));
    vw_wait_tx(); // Wait until the whole message is gone
}

void setup()
{
  // Initialize the IO and ISR
  vw_setup(2000); // Bits per sec
  Serial.begin(9600);               // The IDE settings for Serial Monitor/Plotter (preferred) must match this speed
  pinMode(sensorDigitalPin, INPUT);
}

void loop()
{
  analogValue = analogRead(sensorAnalogPin); // Read the value of the analog interface A0 assigned to digitalValue
  digitalValue = digitalRead(sensorDigitalPin); // Read the value of the digital interface 7 assigned to digitalValue
  Serial.println(analogValue); // Send the analog value to the serial transmit interface

  if (analogValue > 300)   // When the Sound Sensor sends signla, via voltage present, light LED13 (L)
  {
    send("Hello, it's me");

    delay(1000);
  }
}

Here is the code for the receiver



#include <VirtualWire.h>
 #include <ServoTimer2.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message

ServoTimer2 myServo;
void setup()
{
  myServo.attach(9);
  Serial.begin(9600);
  Serial.println("Device is ready");
    Serial.begin(9600);               // The IDE settings for Serial Monitor/Plotter (preferred) must match this speed

  // Initialize the IO and ISR
  vw_setup(2000); // Bits per sec
  vw_rx_start(); // Start the receiver
     
}

void loop()

{
  if (vw_get_message(message, &messageLength)) // Non-blocking
  {
    
    Serial.print("Received: ");
    for (int i = 0; i < messageLength; i++)
    
  Serial.write(message[i]);
  
 
    myServo.write(1500);
     
    }

{
    Serial.println();
  }
}

The Arduino cannot be used to power servos. You need a separate power supply (4xAA battery pack works well). Don't forget to connect the grounds.

For the receiver code, it is required to reset the message length every time before attempting to receive a message:

{
  messageLength = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(message, &messageLength)) // Non-blockin

Be sure to work in steps, for example get the radio working before adding the servo, separately powered, of course.

how do I set that

Did you not notice the example in the post?

I made this change and got rid of the servo and sound sensor and it is still not working

"Not working" tells us nothing. Please describe what you expected to happen, and what happened instead.

In the new post, also post the revised code, using code tags.

The goal of the code is to send the message to the serial monitor of the other Arduino
Here is the code for the transmitter

#include <VirtualWire.h>

void setup()
{
  // Initialize the IO and ISR
  vw_setup(2000); // Bits per sec
}

void loop()
{
  send("Hello, it's me");
  delay(1000);
}

void send (char *message)
{
  vw_send((uint8_t *)message, strlen(message));
  vw_wait_tx(); // Wait until the whole message is gone
}

her is the code for the receiver

#include <VirtualWire.h>

byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message

void setup()
{
  Serial.begin(9600);
  Serial.println("Device is ready");
  // Initialize the IO and ISR
  vw_setup(2000); // Bits per sec
  vw_rx_start(); // Start the receiver
}

void loop()
{
  {

    if (vw_get_message(message, &messageLength)) // Non-blockin
messageLength = VW_MAX_MESSAGE_LEN;
    }
  {
    Serial.print("Received: ");
    for (int i = 0; i < messageLength; i++)
    {
      Serial.write(message[i]);
    }
    Serial.println();
  }
}

Describe what happens when you run the code.

These statements are in the wrong order and do nothing useful.

    if (vw_get_message(message, &messageLength)) // Non-blockin
messageLength = VW_MAX_MESSAGE_LEN;

Please read post #2 more carefully.

when i run the code in the transmitters serial monitor it says receiving and in the receiver's it says nothing

Do those radios need antennas to work?

Hi,
Can you please post some pictures of your project?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

yes I have attached antennas

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