Reader small image

You're reading from  Arduino Data Communications

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781837632619
Edition1st Edition
Right arrow
Author (1)
Robert Thas John
Robert Thas John
author image
Robert Thas John

Robert Thas John is a data engineer with a career that spans two decades. He manages a team of data engineers, analysts, and machine learning engineers – roles that he has held in the past. He leads a number of efforts aimed at increasing the adoption of machine learning on embedded devices through various programs from Google Developers and ARM Ltd, which licenses the chips found in Arduinos and other microcontrollers. He started his career as a software engineer with work that has spanned various industries. His first experience with embedded systems was in programming payment terminals.
Read more about Robert Thas John

Right arrow

Communicating via HC-12

So far, we have learned about various communication technologies that require that we connect to a gateway to send and receive data over long distances. There are situations in which we don’t have access to a gateway and are also not in a position to set one up. That is where HC-12 could come in handy. Using HC-12 modules, we can communicate at a distance of up to 1 km outdoors.

In this chapter, we are going to cover the following main topics:

  • Learning about HC-12
  • Acquiring HC-12 modules
  • Setting up our hardware
  • Sending and receiving data

Let’s begin by listing what you will need to complete this chapter.

Technical requirements

You will need the following components to execute the labs in this chapter:

  • Microcontroller: 2x Arduino MKR WiFi 1010
  • 2x USB cables
  • 2x breadboards
  • 2x HC-12 modules
  • 1x MKR ENV Shield
  • Diodes: 2x 1N4007
  • Capacitors: 2x (22uF to 1mF)
  • Dupont cables
  • 1x MKR ENV Shield
  • 2x antennae

All the code for this chapter is available in this book’s GitHub repository: https://github.com/PacktPublishing/Arduino-Data-Communications/tree/main/chapter-10/.

Let’s proceed to learn about HC-12.

Learning about HC-12

HC-12 is a peer-to-peer broadcast technology and communication module that you can utilize to send and receive data between two or more devices. It is a low-cost, easy-to-use module that you can put into a variety of applications, such as robotics, automation, and industrial control.

HC-12 operates in the 433 MHz frequency band and has a maximum data rate of 100 kbps. It is a half-duplex module, which means that only one device can transmit at a time. However, you can use it to create a peer-to-peer network where multiple devices can communicate with each other.

HC-12 has several features that make it a versatile and reliable module. It has a built-in CRC checksum to ensure data integrity, and it can be configured to work with a variety of modulation schemes. It also has several power-saving features that allow it to operate for extended periods on a single battery.

HC-12 is a popular choice for a variety of applications. You can find it used in robotics...

Acquiring HC-12 modules

You can purchase HC-12 modules in generic form from several online stores, including Amazon, eBay, Banggood, and AliExpress. Simply put HC-12 into the search bar and several options will appear. You can also purchase these modules from local electronics stores.

HC-12 modules don’t carry a brand. As such, you should purchase them from a retailer that you trust, especially if it’s an online vendor. Make sure you read the reviews from previous buyers so that you don’t end up with a poor-quality knockoff.

The modules contain transmitters and receivers. As such, you might want to avoid importing them if you can purchase them locally. This is because you might be required to prove that the modules meet certain certification requirements for where you live. If you can pick these modules up locally, you will be sure that the importer has satisfied all requirements.

If you have to import these modules, try to check if there are any import...

Setting up the hardware

The HC-12 module comes with five holes on one side. You can either mount it castellated or solver headers on it. Two of the connectors are meant for power (VCC) and ground (GND), while the other two are meant for serial communications (TXD and RXD). The final connector is labeled SET and is meant for reconfiguring the module. The opposite side of the module has an antenna connector (ANT) that is meant for RF input/output:

Figure 10.1 – The rear of the HC-12 module

Figure 10.1 – The rear of the HC-12 module

You should ensure that your modules are physically apart by a distance of at least 2 meters. This prevents unusual behavior. This distance requirement exists because of how the modules transmit and receive data using radio waves.

The module works with voltages between 3.2 V and 5.5 V. The required current must exceed 200 mA. Similar to setups that utilize cellular radios, you can’t power the setup via a USB cable. Instead, you will need to power it externally...

Sending and receiving data

You can configure a microcontroller to communicate with an HC-12 module using software serial, as we have seen previously. However, make sure that the SET pin is not pulled to the ground when in normal operation.

Let’s consider a simple sketch that will output whatever the HC-12 module receives to the serial console of the computer to which the microcontroller is connected.

Exercise 10.1 – displaying whatever the HC-12 module receives

Follow these steps to read whatever the HC-12 module receives and print it out on the serial monitor on your computer:

  1. Begin by importing SoftwareSerial:
    #include <SoftwareSerial.h>
  2. Create an instance of SoftwareSerial called HC12. This takes two parameters, the first being the pin that TX is connected to, and the second being the pin that RX is connected to:
    SoftwareSerial HC12(10, 11);
  3. Initialize the serial port on the microcontroller to a baud rate of 9600. Do the same for the software...

Summary

In this chapter, you learned about HC-12 and how to use it to send and receive data. You can now implement peer-to-peer networks out in the field when using a technology such as cellular or LoRaWAN is not suitable.

In the next chapter, we will look at how to communicate when radio signals are not convenient and would constitute interference to other equipment nearby.

Further reading

To learn more about AT commands, you can visit https://1ot.mobi/resources/blog/beginners-guide-to-at-commands.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Arduino Data Communications
Published in: Nov 2023Publisher: PacktISBN-13: 9781837632619
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Robert Thas John

Robert Thas John is a data engineer with a career that spans two decades. He manages a team of data engineers, analysts, and machine learning engineers – roles that he has held in the past. He leads a number of efforts aimed at increasing the adoption of machine learning on embedded devices through various programs from Google Developers and ARM Ltd, which licenses the chips found in Arduinos and other microcontrollers. He started his career as a software engineer with work that has spanned various industries. His first experience with embedded systems was in programming payment terminals.
Read more about Robert Thas John