I have been trying to copy several tutorials to explore how an RF transmitter/receiver work and am stumped. I tried this one and it works lighting up an LED when sending a signal on A0: 433 MHz RF module with Arduino Tutorial 1
When I move on to any of the tutorials involving sending a message in an array, it fails. My current setup is shown in the first attachment. The temperature reads and is displayed in a serial print. I see the tx light blink and the led comes on. But nothing is happening on the receiver end.
Transmitter code:
#include <VirtualWire.h> //Load the library
float temp; //Define the temp float variable
int sensor = 0; // sensor middle pin on analog pin 0
char msg[6];
int led_pin = 13;
void setup()
{
Serial.begin(4800); //start the serial monitor
pinMode(13,OUTPUT);
vw_setup(1000); // VirtualWire communication speed
vw_set_tx_pin(12); // VirtualWire transmit pin
}
byte count = 1;
void loop()
{
temp = analogRead(sensor); //assigning the analog output to temp
temp = temp * 0.48828125; //converting volts to degrees celsius ----- 0.48828125 = [(5V*1000)/1024]10
Serial.print ("uint8 ");
Serial.println (temp);
dtostrf(temp, 6,2,msg); //converts the float into a char
Serial.print ("dtostrf ");
Serial.println (msg);
digitalWrite(led_pin,1);
delay(2000);
vw_send((uint8_t *)msg, 7); //transmits the data
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(led_pin,0);
delay(5000); // Wait five seconds and it again
}
Receiver code:
//load libraries
#include <VirtualWire.h>
#include <Wire.h>
//Define variables
int i; //and integer used to count
int led_pin = 13;
void setup()
{
//Define the receiver pin and rate
delay(1000);
Serial.begin(4800);
vw_set_rx_pin(4); //Sets pin D4 as the RX Pin
vw_set_ptt_inverted(true);
vw_setup(1000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
Serial.print(" waiting ");
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
Serial.print(i);
if( vw_get_message(buf,&buflen) )
{
Serial.println("Temp inside is: "); //this never prints!!
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i],HEX);
}
Serial.print((char)223);
Serial.println("C");
}
delay(1000);
Serial.println(digitalRead(4)); //this will change from 0 to 1 and back
}
I have tried many tutorials and never seem to receive a message as "Temp inside is: " never prints. I tried using the Radiohead library instead (see third attachment), but I did not see anything receiving. I never see the RX light on the receiving arduino light up or blink.
Any help you could give me on what to do next would be appreciated.
Which pin is used for the Transmitter?
Which pin is used for the Receiver?
Transmitter: Pin 12 in the sketch. Pin 13 written on the piece of paper. Pin 4 in the photo (orange wire). Pin 4 in the tutorial.
Receiver: Pin 4 in the sketch. Pin 12 written on the piece of paper. I think pin 11 in the photo (green wire). Pin A0 in the tutorial.
In the tutorial the receiver is powered with 3.3V, but you should use 5V. I think you already have 5V. The output of the receiver has already a weak high level.
Your array "char msg[6];" is too small. Make it 20 or 40.
No one uses a string of text to transmit with VirtualWire. That is only done in a (silly) tutorial. Everyone uses binary data. Either a single variable or an array of variables or a struct.
The RadioHead library has a few bug fixes, but uses more memory. In most cases the VirtualWire is still okay to use, but you have to use the latest version.
Thank you for your answer! The 12 and 13 written on the paper were the comm ports I was using at the time. I used different pins depending on which tutorial I was following, but I did double check those each time. Currently I have the green wire from the temperature sensor to A0, transmitter to pin 4, receiver to pin 12.
I started with 3.3 V, but switched to 5V.
The string of text was my desperate attempt to make anything receive! My original goal was to send data from a BME280 sensor. Simplified plan was to send the temperature from the simple tmp36 transducer. Even more simplified plan was make it send text or anything.
I will fix the char msg size. Thank you for that tip. Anything else look off?
If you know another tutorial that I should try, I would be glad to!
Thank you again!
You don't need more tutorials. The examples with the VirtualWire library should be enough.
Go back to when it was working. Can you make the led turn on again ?
If you can, change one little thing at a time, and try to send real data.
Sometimes a breadboard has bad contacts, you could try to move everything to other locations on the breadboard.
Do the wires have clean shiny metal ends ?
Have you read the VirtualWire page and the document at the bottom of that page ? You can test the VirtualWire by connecting the pins without the transmitter and receiver.
Try to use red wires for 5V and black wires for GND.
I'm guessing that bad contacts of the breadboard with the wires is the first to look at.
Thank you. I removed the wires from the breadboard and replaced them. I am now getting a temperature of 2036382E8350. My next step is to figure out what that means and convert it! I got an upside down question mark in place of the degree sign. Well, that is tomorrows task! Thank you for your help!
2036382E8350 = 0x20, 0x36, 0x38, 0x2E, 0x83, 0x50 = " 68.?P".
I'm not sure about the ASCII value of 0x83 and 0x50.
Since you did a dtostrf with '6' and not '-6', the text is filled up to 6 characters with spaces in the front. Since it starts with a space, it could be hexadecimal ASCII data.
I agree that it is hexadecimal ASCII. The 68. makes sense as the string I was trying to send ranged between 68.36 and 68.85. All the tutorials I saw just showed that using HEX in the line Serial.print(buf*,HEX); would let the receiver understand that it should read the incoming string as HEX. But, mine seems to just be bringing it over as a string and printing it out just like that. I don't see what I missed.*