TUTORIAL
Here is a simple tutorial on how to use a transmitter and a receiver to send signal using 3 push buttons when they are pressed.
You will need:
- 315Mhz Transmitter
- 315Mhz Receiver
- Arduino Board
- Breadboard
- 3x Push Buttons
- 3x 10k Resistors
Connecting the transmitter and push buttons to the arduino
Connect the transmitter data to pin 12
Connect the transmitter VCC to power
Connect the transmitter GND to ground
Connect the push buttons to pin 2, 4, and 7 and 10k resistors to ground. And connect to power.
Connecting the receiver to the arduino
Connect the receiver data to pin 12
Connect the receiver VCC to power
Connect the receiver GND to ground
Then download RadioHead Library here and add to the Arduino library.
Code for transmitter
// transmitter pin
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compileRH_ASK driver;
//Connect data to pin 12#define buttonPin1 2
#define buttonPin2 4
#define buttonPin3 7
boolean pressing = false;
int currentState = 0;int sensorData = 0;
void setup()
{
Serial.begin(4800); // Debugging only
if (!driver.init())
Serial.println("init failed");pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}void loop()
{
int toggleState1 = digitalRead(buttonPin1);
int toggleState2 = digitalRead(buttonPin2);
int toggleState3 = digitalRead(buttonPin3);
//Serial.println(toggleState);
if (toggleState1 == HIGH) {
pressing = true;sensorData++;
String sensorMsg = "Sensor1: I feel sick!! ";
char charBuf[sensorMsg.length()];
sensorMsg.toCharArray(charBuf, sensorMsg.length());
char *msg = charBuf;
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
}
if (toggleState2 == HIGH) {
pressing = true;sensorData++;
String sensorMsg = "Sensor2: I need to talk to you. ";
char charBuf[sensorMsg.length()];
sensorMsg.toCharArray(charBuf, sensorMsg.length());
char *msg = charBuf;
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
}
if (toggleState3 == HIGH) {
pressing = true;sensorData++;
String sensorMsg = "Sensor3: HELP!! ";
char charBuf[sensorMsg.length()];
sensorMsg.toCharArray(charBuf, sensorMsg.length());
char *msg = charBuf;
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
}
if ((toggleState1 || toggleState2 || toggleState3) == LOW && pressing == true) {
pressing = false;
currentState++; // currentState = currentState + 1;
delay(50);
}
}
Code for receiver
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compileRH_ASK driver; //Connect data to pin 11
String message = "";void setup()
{
Serial.begin(4800); // Debugging only
if (!driver.init())
Serial.println("init failed");
}void loop()
{
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);if (driver.recv(buf, &buflen)) // Non-blocking
{
int i;// Message with a good checksum received, dump it.
//driver.printBuffer("Got:", buf, buflen);
message = parseMsg(buf, buflen);
Serial.println(message);
}
}String parseMsg(unsigned char* msg, uint8_t msgLen){
String fullMsg = "";
for(int i = 0; i<msgLen; i++){
fullMsg += (char)msg[i];
}
return fullMsg;}
Make sure to upload the transmitter code first, and then the receiver code.
After uploading the code, open the Serial Monitor box from the receiver Arduino file and make sure 4800 baud is selected
Press any of the push buttons, and you should see the message get printed.
The range could go up to 500 ft for the 315Mhz Transmitter and Receiver.
Enjoy!!