Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
150 min
Share

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.

Project Video

Overview

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.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
HC-12 RF Wireless Remote Serial Port Module
Get Item
SG90 Servo -Positional Rotation
Get Item
Push button Switch (5 Pack) - 12mm
Get Item
1/4 Watt Resistor (20 Pack)
Get Item
Half-size Breadboard
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

Wiring it Up

 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

Coding

The transmitter circuit code:


/*
 Voltaat learn (http://learn.voltaat.com)
 Link for full tutorial:
 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)
SoftwareSerial HC12(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
 else if ( !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;

 }

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

 }

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

 }

}

The reciver circuit code:


/*
 Voltaat learn (http://learn.voltaat.com)
 Link for full tutorial:
 Servo library:

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

 The reciver 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)
SoftwareSerial HC12(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);

   }
 }
}

Testing it Out

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

No items found.