analogRead() function affects 433 MHz RF module transmission.

Hello.

I have a pair of cheap wireless communication modules, that I bought off eBay.

it is the ones used in this tutorial:

I also copied most of my code from that tutorial.

I am using the virtual wire library.

here is all my code for the transmitter:

//Transmitter Code
#include <VirtualWire.h>

int potPin = 0;
int val;

void setup()
{
  Serial.begin(9600);
  
  pinMode(13, OUTPUT);
  
  vw_setup(2000);
  vw_set_tx_pin(7);
}

void loop()
{
  if(Serial.available())
  {
    char c = Serial.read();
    
    if(c == '1')
    {
      vw_send((uint8_t *)c, 1);
    }
    else if(c == '0')
    {
      vw_send((uint8_t *)c, 1);
    }
  }
  
  //val = analogRead(potPin);
}

and here is all my code for the reciever:

//Reciever Code
#include <VirtualWire.h>

void setup()
{
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  
  vw_setup(2000);
  vw_set_rx_pin(7);
  vw_rx_start();
}

void loop()
{
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  uint8_t buf[buflen];
  
  if(vw_get_message(buf, &buflen))
  {
    for(int i = 0;i < buflen;i++)
    {
      if(buf[i] == '1')
      {
        digitalWrite(13,HIGH);
      }
      else if(buf[i] == '0')
      {
        digitalWrite(13,LOW);
      }
    }
  }
}

As it is there the code works as expected. I type a ‘1’ or a ‘0’ into the Serial terminal of the transmitter and then it will transmit over and the light on pin 13 on the receiver Arduino will turn on or off. (I normally have to type the 1 or the 0, two to three times for it to register, I’m pretty sure this is just because the modules are cheap and terrible).

However, if on the transmitter code I uncomment the line:

val = analogRead(potPin);

this reads from analog pin 0 which is hooked up to a potentiometer.
If I uncomment that line the transmission is really bad. Now when I have to type the ‘1’ or the ‘0’ into the Serial terminal 20-30 times for it to be registered by the receiver, and that’s only if I don’t give up before it actually decides to register at all.

So I was messing around for a long while trying to fix it and just randomly decided to unplug the wire on the transmitter Arduino from the 5V and plug it into the 3.3V and suddenly now it works, however the potentiometer only goes up to a value of ~750 I think it was.

so I was hoping that you could help me out and figure out what is going on here.
Why does it work for the 3.3V and not the 5V?
why does just reading from the analog pin affect the transmission so much?
Is there a way I can have 5V and have the transmission working properly?

thankyou.

Hi, welcome to the forum.

Let's start by forgetting the 3.3V and that it did work somehow ::slight_smile:

The vw_send() in the transmitter wants a pointer to the data. You use the data itself and convert that to a pointer, but the address of 'c' is needed with '&c'.

vw_send((uint8_t *) &c, 1);

Yes, those modules are cheap and terrible, but the VirtualWire library is very good.
Add the piece of 17cm wire to the antenna of receiver and transmitter, and the VirtualWire will make them work well.

The receiver has a LM358, which is not capable to output a good high signal.
I use the receiver at 5V of course, and I turn on the internal pullup resistor of the RX pin to help the signal a little.

og_the_trog:
Is there a way I can have 5V and have the transmission working properly?

Just wait until the transmission is finished:

vw_wait_tx();

thankyou Peter_n the solution worked perfectly.