Wireless Communication of Multiple Variables Between 2 Arduinos Using HC-12

by lincoln1clarke in Circuits > Arduino

3365 Views, 12 Favorites, 0 Comments

Wireless Communication of Multiple Variables Between 2 Arduinos Using HC-12

IMG_20220425_174014.jpg

In this tutorial, I will show you how to achieve wireless communication of variables between 2 Arduinos using the HC-12 module, that operates from 433,4-473 Mhz.

HC-12s are wireless modules for arduino to arduino communication, because they can send multiple variables over long distances-ones up to 1km-using only 2 digital pins, not even PWM pins, which is much better than the NRF24 pinout, making them ideal for wireless projects.

I couldn't find any tutorials out there showing this, so I decided to make one.

My inspiration for the code is this project by RootSaid, but I have modified it to make it easy to understand and to integrate into any project.

Supplies

-2 HC 12 modules

-2 Arduino boards of your choice

-Wires

-Whatever else you want to add to your wireless project

Understand and Upload These Codes to Your Arduinos

Here is the code for the transmitter:

#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial HC12(12, 13);//tx,rx


int variable1;//variables that will get sent
int variable2;
int variable3;
int variable4;
int variable5;


void setup() {
  HC12.begin(9600);//Sets the hc 12 communication to 9600 baud, which is their factory default
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
}
void loop() {
  variable1 = analogRead(A0);//assigns the variables that will be sent the value read on the analog pins
  variable2 = analogRead(A1);//you can change this to other data, such as a digital pin's read
  variable3 = analogRead(A2);
  variable4 = analogRead(A3);
  variable5 = analogRead(A4);
  //Uncomment the following lines if you want the remote to also send the analog input data to the serial monitor, to check if the data is being read properly
  //If you do, It will look the same as it should on the other end's serial monitor
  //Serial.print(variable1);
  //Serial.print(",");
  //Serial.print(variable2);
  //Serial.print(",");
  //Serial.print(variable3); 
  //Serial.print(","); 
  //Serial.print(variable4);
  //Serial.print(",");
  //Serial.print(variable5);
  //Serial.println("");
  HC12.print(variable1);//sends the variables
  HC12.print(",");
  HC12.print(variable2);
  HC12.print(",");
  HC12.print(variable3);//if you just need to send 2 variables,simply change this value and the following to 0
  HC12.print(",");
  HC12.print(variable4);//if you just need to send 3 variables,simply change this value and the next to 0
  HC12.print(",");
  HC12.print(variable5);//if you just need to send 4 variables,simply change this value to 0
//if you need 5 or less variables, disregard comments below, but keep the next line of code, it is essential
  HC12.println("");//you can add even numbers of variables, ex add 2, 4, 6 ...(so 3, 5, 7, 9... variables in total) before this line by adding a variable line, then a comma line, another variable line then a second comma line under the 5th variable line but before the println.  
//for the purpose of simplicity, it is easier to add in groups of 2, so that less modification is necessary on the 
other end
//if you have more variables than you are using, change the variable to 0, as above explained, again for the 
purpose of simplicity
// This change must be reflected on receiver end: see below
  delay(100);
}


Here is the code for the Receiver:

#include <SoftwareSerial.h>
#include <Wire.h>

SoftwareSerial HC12(12, 13);//tx,rx

int variable1=0;
int variable2=0;
int variable3=0;
int variable4=0;
int variable5;
String input;
int boundLow;
int boundHigh;
const char delimiter = ',';


void setup() {
  Serial.begin(9600);
  HC12.begin(9600);
}


void loop() {
  if(HC12.available())
  {
  input = HC12.readStringUntil('\n');
  if (input.length() > 0)
      {
        Serial.println(input);
       
       boundLow = input.indexOf(delimiter);
        variable1 = input.substring(0, boundLow).toInt();
    
        boundHigh = input.indexOf(delimiter, boundLow+1);
        variable2 = input.substring(boundLow+1, boundHigh).toInt();


        boundLow = input.indexOf(delimiter);//you can add 2 variables, or 4... by adding these 4 lines 
        variable3 = input.substring(0, boundLow).toInt();//you can add 2 variables, or 4... by adding these 4 lines
    
        boundHigh = input.indexOf(delimiter, boundLow+1);//you can add 2 variables, or 4... by adding these 4 lines 
        variable4 = input.substring(boundLow+1, boundHigh).toInt();//     under this line
    
        variable5 = input.substring(boundHigh+1).toInt();//but before this one
//if you change the number of variables, follow instructions in comments above
//duplicating the lines as explained adds 2 variables, then you can duplicate them again, and then change the variable names so they are in the right order
  
delay(10);
      }//after this you can choose what to do with each variable, which will now have the same value as it's value on the transmitter end
  }
}

Basically, the way it works is that the first transmitter sends the variables in order, with commas in between, forming a string, and the receiver knows the order(because you program it with the order in mind), and takes apart the string to assign the values to their corresponding variables.

So, the first number/variable sent will be the first one in the string that gets received, and the second, the second...

Modify the Codes to Your Liking to Fit With Your Project

Now that you have both Arduinos communicating, modify the codes so the receiver can use the variables to control lights, motors and more, specific to your project.

I used this for an RC boat, but it can be used for many things.

Demonstration of the Code Working

Demonstration of wireless communication between 2 arduinos using HC-12 modules

This video shows this code working: The remote sends the variables, and then the HC-12 on the receiver end receives the string, and prints it on the arduino serial monitor. It does not process the code, but the codes are saved into variables on the receiving arduino.

Wire Everything for Your Project

Now that the code is uploaded, start to wire your project:

On the transmitter side, attach your analog inputs to A0-A4, and then attach the TX pin of HC 12 to pin12, and RX to pin 13. Then connect the VCC to 3.3volts(or 5v, but 3.3 is safer) and the GND to GND.


On the Receiver, attach the TX pin of HC 12 to pin12, and RX to pin 13. Then connect the VCC to 3.3volts(or 5v, but 3.3 is safer) and the GND to GND, exactly the same way as for the transmitter. Then attach the other components of you project that will function based on the variables the HC 12 receives.

Make a Housing for the Remote

IMG_20220425_183124.jpg

Attached is an .stl file of a remote housing and it's lid, which fits an Arduino Nano, a 9v battery to power it, an HC 12 at the end, 2 potentiometers and a joystick, which has holes for screwing it in.


You can also design your own remote.

Explanation of the Functioning of This System, for Personal Understanding or If Used As Science Fair Project

f-d_0a6eb6ba678b209ac1c05e4f124bf965b59024c1ee34d7373af48a46+IMAGE_THUMB_POSTCARD_TINY+IMAGE_THUMB_POSTCARD_TINY.png

First, one arduino sends the value of x number of variables(in the above example 5), through serial transmission, to an HC-12 wireless module. This module emits these variables, spaced by commas in a string, in the form of a radio waves, not to be confused with sound waves. Then, the second HC-12 picks up these strings, from the radio waves it receives, which are sent every 100ms, and sends them to the receiving arduino through serial, that decodes these strings and can then process them.


HC-12 uses 433.4 Mhz frequency on factory default, which means that the signal contains 433 400 000 waves every second. One wave goes from the middle of the bottom wave, to the middle of the next bottom wave(see illustration). Frequency is the number of waves per second. It is also used in AC electricity: 60Hz means that the current switches from positive to negative 60 times per second.

The wavelength of this signal is 691 mm, because the Frequency(Hz)=Velocity(m/s)/wavelength, and radio waves move at the speed of light, so

Wavelength = Velocity/Frequency

Wavelength = 299 792 458/433 400 000

Wavelength(m) = 0.691 = 691 mm


Radio waves can travel through space, contrary to sound waves, with can only travel through matter.

Congratulations, You Now Know How to Make Wireless Projects