Control a servo motor from up to 1 kilometer away! 📶

The HC-12 is a wireless communication module that is very useful, extremely powerful, and easy to use. With this module, you can have many applications like remote control, data logging, wireless programming, etc.


In this tutorial, we will use the HC-12 module to control a servo motor using a push button. You can use it to open and close doors, operate machinery, or even control valves.


Overview


The HC-12 is a wireless communication module that has a maximum transmission distance of up to 1 km (in ideal conditions).


The module can be configured as either a transmitter or receiver. When used as a receiver, it can receive data from up to 8 different transmitters (each with a unique ID).


It can be used to create a wireless link between two devices, such as between a PC and an Arduino board, which will be very useful in many applications.


Getting the items


For this project you will need the following components: 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Positional Rotation Servo - Generic (Micro Size)
20 QAR
Sale Off
Voltaat Push button Switch (5 Pack) - 12mm
2 QAR

Make sure you also have a breadboard and jumper wires to connect everything together

Sale Off
Voltaat 1/4 Watt Resistor (20 Pack)
2 QAR
Sale Off
Voltaat Half-size Breadboard
15 QAR
Sale Off
Voltaat Jumper Wires - Male to Female (40 Pack)
10 QAR
Sale Off
Voltaat Jumper Wires - Male to Male (40 Pack)
10 QAR

Wiring it up


This circuit is based on two separate Arduino circuits; we will consider one of them a transmitter and the other as a receiver.


You must follow the following instructions in wiring the two circuits to control the servo motor to open and close the door from a distance:


The transmitter circuit:



Connections from the Arduino to the breadboard:

• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line


Connections from the HC-12 module:

• GND pin→ Breadboard ground line

• VCC pin→ Breadboard 5V line

• TXD pin → Arduino pin 7

• RXD pin → Arduino pin 8


Connections from the first push button:

• First pin→ Breadboard 5v line

• Second pin→ Arduino pin 2

• Second pin→10KΩ resistor → Breadboard Ground line


Connections from the second push button:

• First pin→ Breadboard 5v line

• Second pin→ Arduino pin 5

• Second pin→10KΩ resistor → Breadboard Ground line


The receiver circuit:


Connections from the Arduino to the breadboard:

• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line


Connections from the HC-12 module:

• GND pin→ Breadboard ground line

• VCC pin→ Breadboard 5V line

• TXD pin → Arduino pin 7

• RXD pin → Arduino pin 8


Connections from the servo motor:

• GND pin→ Breadboard ground line

• VCC pin→ Breadboard 5V line

• Signal pin → Arduino pin 9


Coding


This code function is to remotely control a door with two push buttons and a servo motor. The door will be opened when one push button is pressed and closed when the other push button is pressed.


The code also makes sure that the door cannot be opened and closed at the same time or repeat the operation.


Let's start with the code, and then you'll be able to understand it after following the instructions and reading the comments.


The transmitter circuit code:


/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial: https://bit.ly/3GgVWYb
Servo library: https://bit.ly/3FUQk5z

Tutorial: Control a servo motor from up to 1 kilometer away

The transmitter circuit code:

The transmitter circuit wiring:
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line

Connections from the HC-12 module:
• GND pin→ Breadboard ground line
• VCC pin→ Breadboard 5V line
• TXD pin → Arduino pin 7
• RXD pin → Arduino pin 8

Connections from the first push button:
• First pin→ Breadboard 5v line
• Second pin→ Arduino pin 2
• Second pin→10KΩ resistor → Breadboard Ground line

Connections from the second push button:
• First pin→ Breadboard 5v line
• Second pin→ Arduino pin 5
• Second pin→10KΩ resistor → Breadboard Ground line

The receiver circuit wiring:
Connections from the Arduino to the breadboard:
• Arduino pin GND → Breadboard ground line
• Arduino pin 5V → Breadboard 5V line
Connections from the HC-12 module:
• GND pin→ Breadboard ground line
• VCC pin→ Breadboard 5V line
• TXD pin → Arduino pin 7
• RXD pin → Arduino pin 8

Connections from the servo motor:
• GND pin→ Breadboard ground line
• VCC pin→ Breadboard 5V line
• Signal pin → Arduino pin 9

*/


//Allows serial communication on other digital pins of an Arduino board
#include < SoftwareSerial.h>

//A library to control servo motors
#include < Servo.h >

//Configure software serial port with digital pins 7,8 (TR,RX)
SoftwareSerialHC12(7,8);

//Create servo object to control a servo
Servo myservo;

//Define variables
char buttonstate =0;
int pos =0;

//Commands inside void setup run once
void setup()
{
//Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);
//Arduino communicates with HC12 module at a baud rate of 9600 (9600 bits per second)
HC12.begin(9600);
//print to serial monitor
Serial.println("Reciver is ready");
//Attach the Servo variable to pin 9
myservo.attach(9);

}

//Commands inside void setup run forever
void loop()
{

//If Arduino's HC12 rx buffer has data
while(HC12.available()>0)
{
//assign data from HC12 to buttonstate variable
buttonstate = HC12.read();
//Print to serial monitor
Serial.println(buttonstate);
switch(buttonstate)
{
case'1':
//move the servo motor to origin
myservo.write(0);
//delay for 100 milliseconds
delay(100);
// set servo to mid-point (90 degree angle)
myservo.write(90);
break;
case'2':
//move the servo motor back to origin
myservo.write(0);

}
}
}

The reviver circuit code: 


/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial: https://bit.ly/3GgVWYb

Tutorial: Control a servo motor from up to 1 kilometer away

The transmitter circuit code:

The transmitter circuit wiring:
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line

Connections from the HC-12 module:
• GND pin→ Breadboard ground line
• VCC pin→ Breadboard 5V line
• TXD pin → Arduino pin 7
• RXD pin → Arduino pin 8

Connections from the first push button:
• First pin→ Breadboard 5v line
• Second pin→ Arduino pin 2
• Second pin→10KΩ resistor → Breadboard Ground line

Connections from the second push button:
• First pin→ Breadboard 5v line
• Second pin→ Arduino pin 5
• Second pin→10KΩ resistor → Breadboard Ground line

The receiver circuit wiring:
Connections from the Arduino to the breadboard:
• Arduino pin GND → Breadboard ground line
• Arduino pin 5V → Breadboard 5V line
Connections from the HC-12 module:
• GND pin→ Breadboard ground line
• VCC pin→ Breadboard 5V line
• TXD pin → Arduino pin 7
• RXD pin → Arduino pin 8

Connections from the servo motor:
• GND pin→ Breadboard ground line
• VCC pin→ Breadboard 5V line
• Signal pin → Arduino pin 9

*/


//Allows serial communication on other digital pins of an Arduino board
#include < SoftwareSerial.h >

//Configure software serial port with digital pins 7,8 (TR,RX)
SoftwareSerialHC12(7,8);

//Define push button to arduino digital pin 2
int Button1 =2;

//Define push button to arduino digital pin 5
int Button2 =5;

//Define variables
int button1State =0;
int button2State =0;
boolean doorState =false;
boolean oneTime =true;

//Commands inside void setup run once
void setup()
{
//Button1 is defined as an input
pinMode(Button1,INPUT);

//Button2 is defined as an input
pinMode(Button2,INPUT);

//Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);

//Arduino communicates with HC12 module at a baud rate of 9600 (9600 bits per second)
HC12.begin(9600);

//delay 200 milliseconds
delay(200);
}

//Commands inside void setup run forever
void loop()
{
//If condition to make the push button works one time
if(digitalRead(Button1))
{
//change doorState variable state
doorState =true;

}
//If condition to make the push button works one time
if(digitalRead(Button2))
{
//change doorState variable state
doorState =false;

}

//If condition to make the push button works one time when pressed
if(doorState && oneTime )
{
//print to serial monitor
Serial.println("Button1 is pressed, Door Is opened");
//print to serial monitor
Serial.println('1');
//send '1' to the HC12 receiver module
HC12.print('1');
//change oneTime variable state
oneTime =false;
}

//If condition to make the other push button works one time when pressed
elseif(!doorState &&!oneTime)
{
//print to serial monitor
Serial.println("Button2 is pressed, Door Is closed");
//print to serial monitor
Serial.println('2');
//send '2' to the HC12 receiver module
HC12.print('2');
//change oneTime variable state
oneTime =true;

}

elseif( doorState &&!oneTime)
{
//print door state to serial monitor
Serial.println("Door state : Opened, you can press Button2 to close");

}

elseif(!doorState && oneTime)
{
//print door state to serial monitor
Serial.println("Door state : Closed, you can press Button1 to open");

}

}

Testing it out


Now you must have correctly wired the transmitter and receiver circuits as we explained in the wiring section, as well as uploaded the code to your Arduino boards.


When you press the first push button, you will observe the servo motor moving at 90 degrees to open the door, but when you press the second push button, you will observe the servo motor going back to its origin again!


Resources 


Transmitter Code

Reviver Code

Servo Library

Transmitter Wiring file

Reviver Wiring file

Related Tutorials


In this tutorial, we will use the Arduino to control the servo motor shaft to sweep back and forth across 180 degrees. Let's have a look at it.


In this tutorial, we will learn more about bush buttons and how to use them. We'll use the push button in a simple project to control the Arduino's internal LED and print the current state of the button directly on your commuter.


In this tutorial, we will use the Arduino to control a relay on and off every three seconds. You can use the same code to control a fan to be turned on and off automatically every 10 minutes to save power!