[SOLVED] 433 MHz RF module receiver code?

Hi,

I’m trying to learn RF modules with Arduino.I have bought these cheap 433 MHz RF modules from ebay and they work perfectly with my 2 x Arduino nano clones using the provided example scripts from the VirtualWire library.

But now I want to transmit a simple ‘Byte’ number with TX module and receive it on my RX module and also print it on the serial monitor.

I modified the transmit example code to include my Byte number “99” and successfully verified it and uploaded it to TX Arduino without any errors.

However my problem is that I don’t know how to receive it and print it. I’m posting my transmit code below. I’m a total noob so any help would be appreciated.

#include <VirtualWire.h>

const int led_pin = 13;
const int transmit_pin = 12;
const int receive_pin = 2;
const int transmit_en_pin = 3;


void setup()
{
    // Initialise the IO and ISR
    vw_set_tx_pin(transmit_pin);
    vw_set_ptt_pin(transmit_en_pin);
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);       // Bits per sec
    pinMode(led_pin, OUTPUT);
    Serial.begin(9600);
}


void loop()
{
  byte x = 69;
  digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
  vw_send((byte *)x, 2 );
  vw_wait_tx(); // Wait until the whole message is gone
  Serial.print(x);
  Serial.println();
  digitalWrite(led_pin, LOW);
  delay(1000);
}

any idea?

You seem to be sending 2 bytes instead of 1

(vw_send((byte *)x, 2 );

I haven't used the VirualWire library, so can't help with code, but perhaps a good starting point would be the "Receive" example.

Have you tried running the "Transmit" and "Receive" examples as typed in the documentation, without modifying them first?

Something I noticed in your code:-

byte x = 69;
.
.
.
 vw_send((byte *)x, 2 );

Casting x to a byte* won't work. You need to pass the address of x. Also, you indicate 2 bytes are being sent, when x is only one byte. Try this instead:-

byte x = 69;
.
.
.
 vw_send(&x, 1 );

Edit: I just spotted your reply, Riva, but will leave this post as-is, since I also spotted the other error. :)

Riva: You seem to be sending 2 bytes instead of 1

(vw_send((byte *)x, 2 );

is it because (byte *) or is it because 2? If so I thought 2 was supposed to be the length of the number

OldSteve: I haven't used the VirualWire library, so can't help with code, but perhaps a good starting point would be the "Receive" example.

Have you tried running the "Transmit" and "Receive" examples as typed in the documentation, without modifying them first?

I did try the transmit and receive example that came with the library - which is also mentioned here in this documentation https://www.pjrc.com/teensy/td_libs_VirtualWire.html The Example code worked fine but the problem is that whatever it transmits will be received in hexadecimal values.

All I want to do is to transmit any 2 digit number (0-99) and receive it as it is.

69 is not '6' '9'

Edit: 69 is a single byte, whereas '6' and '9' are two ASCII characters. I think you need to do a little reading on data types.

If you want to send a single byte, as your code indicated, then you do it as I said earlier.

If you want to send it as 2 ASCII characters, you first need to convert the 'value' 69 to 2 ASCII characters.

The reference gives a bit more info:- char byte ASCII CharCast

OldSteve: 69 is not '6' '9'

Edit: 69 is a single byte, whereas '6' and '9' are two ASCII characters. I think you need to do a little reading on data types.

If you want to send a single byte, as your code indicated, then you do it as I said earlier.

If you want to send it as 2 ASCII characters, you first need to convert the 'value' 69 to 2 ASCII characters.

The reference gives a bit more info:- char byte ASCII CharCast

I did go through those references and what confused me was the length of the message. I understood that 6 and 69 and 122 are all single a Byte, but their length varies, and I initially thought that since their length varies, I should change that value as well. eg: like 122 takes 3 places. Thanks for clarifying that.

No, I do not want to send ASCII characters, just 1 byte.

I will try it and let you know the result.

6, 69, 122 are all single byte (8 bits) if the data is sent as 'raw' hex data, say with Serial.write: 0x06, 0x45, 0x7A. If sending as human readable characters, then you have '6', '6' '9', and '1' '2' '2', and they are different lengths, needing one, two, and three bytes to send their ASCII equivalents, see www.asciitable.com, of 0x36, 0x36 0x39, and 0x31 0x32 0x32. (or 54, 54 57, 49 50 50)

Just tried out what OldSteve suggested and guess what... IT WORKZZZ.

Thanks man, You're a genius. Also you're a life saver.

Noobian:
Just tried out what OldSteve suggested and guess what… IT WORKZZZ.

Thanks man, You’re a genius. Also you’re a life saver.

Definitely not a genius, but I’ll accept the label “life saver”. Sure beats what I’m usually called. :smiley: