All we 90s kids have seen mobile phones taking over our lives. We have been taught in school that Electromagnetic waves carry our signals. In school, like most people, I too accepted that mobile phones work with electromagnetic waves, no further thought given..!! The objective of this blog post is to give a brief historical context and tell you how exactly electro-magnetic waves can be generated from scratch at home and how modern telephony works. Finally, I will explore a communication module (HC-12) with my Arduino board.

Physics Time [Don’t Skip ;)]

Ever since I started my UG studies (Engineering), I always had a question, how can I generate electromagnetic waves? I was too embarrassed to ask this question. Also, internet (at that time) was full of vague answers. I finally figured out this puzzle, only in around 2016 or so. My source of the answer was MIT’s Walter Levin’s 8.02 Youtube course on Electricity and Magnetism and later this fantastic book by Nancy Forbes and Basil Mahon . Especially the discussion on Maxwell’s equations. The hidden meaning behind those 4 equations truly opened my eyes. Let me summarize these two sources.

So, around 1860s, Maxwell published his equations and he predicted the existence of some kind of waves, which were later named electro-magnetic waves because these were produced by the interaction of electricity and magnetism. But he didn’t know how to prove their existence. 30 years later, around 1890s Heinrich Hertz in Germany did this interesting experiment which decisively proved the existence of electro-magnetic waves. A few people on youtube have recreated this experiment. Do checkout the following video:

Let me give a straight answer to “How to generate electromagnetic waves“. So the point is if in a conducting loop you put in AC current with a frequency in the MHz range (ie. current changing direction 1 million times a second, the home AC power is 60 Hz, meaning current changes direction 60 times per second). Such high-frequency AC current can be generated in the lab with an oscillating crystal or a function generator. As you do this, electromagnetic waves are generated, that’s in the nature of things. As a result of these generated electromagnetic waves, a nearby loop, which is physically disconnected also gets a small current in it. This is also sometimes known as electromagnetic induction. The waves which carry this information, which instructs the other loop to conduct current are called the electromagnetic waves. Look at the figure below for a simple setup on how you can generate electromagnetic waves.

How to generate electromagnetic waves

I’ll be honest, I have personally not tried the above setup but whenever I get the inspiration for this in the future I am wanting to witness the generation of electromagnetic waves. But for now, I am reasonably convinced that this can generate electromagnetic waves and the frequency of these waves can be controlled, by varying the AC source frequency.

Digital Communication

Now let’s talk about how can I send my 0s and 1s with electromagnetic waves. Roughly speaking, there are 4 primary ways to achieve this. a) You can make the rule like, if no waves are produced it is zero if the led glows than it is one. This is called “On-Off-Keying” in the literature. b) Higher voltage at the source will cause a more intense electromagnetic wave which will make the LED glow brighter. So you can make a rule like brighter LED means 1, dimmer LED means 0 and no glow in LED means no signal. In the literature, this is called “Amplitude Modulation (AM)”. This is how those old days radios worked. c) AC signal frequency can be measured with AC meters. If you install one of those at the receiver end, you can know the frequency of the current in the loop. So a rule like, if you detect a frequency of 1 Mhz than it is 0 if a frequency of 2 Mhz is detected than that implies a 1 can be used. This is referred to “Frequency Modulation (FM)”. This technique was used in FM radios. d) It is possible to change the phase of AC signals. Remember, BLDC motors needed 3 phases which were 120 degrees apart. A circuit like that can generate AC signals with a phase shift. Similarly, it is possible to measure phase shifts. So a rule like no phase shift is 0 (compared to a base phase) and a phase shift of say 60 degrees is 1 will do the job. This is referred to as Phase Shift Keying (PSK). This is the most efficient way and variants of this method are in use in mobile phones, WIFI, Bluetooth and other communication devices.

Image result for sine phase shift
Image result for PSK digital
A summary Image of this section. Visual for AM, FM and PSK.

Hopefully, my simplistic explanation gives you some insight into how we sapiens are communicating with electromagnetic waves. Depending on which country you live in, different frequencies are reserved for different applications like, Bluetooth, Wifi, Military applications, police communication, GPS etc. In the US, the National Communication and Telecommunication Administration handles this. In Hong Kong, Office of Communication Authority handles this. In India, Department of Telecommunication (DOT) does this job. Irrespective of the countries, some frequency bands remain open for the public free of cost and registration. Notably the Bluetooth band (433 Mhz, 900 Mhz), and Wifi bands (2.4Ghz and 4.8 GHz). In spite of this, if you design your own circuits it is possible to transmit and receive at any frequency. Doing this is illegal and can easily land you in jail (you have been warned). At this point, it is worth going over to your county’s Telecommunication authority and checking out the allocation for your country and noting the public-free frequency bands.

One another, subtle thing to note is that most of the modules out there uses PSK’s variants. So it will specify a frequency at which it transmits and the bandwidth. Typically you can change the frequency to allow multiple devices within an allowed range. In the communication modules lingo, it is called channels. So, when you change the channel you are actually changing the transmission/receiving frequency (within a small range). Also, if you send more currents in your circuits, they consume more power and hence are able to send your signal farther away. It is worthwhile knowing how far your module’s transmission works. A typical module available in the market will let you change its power consumption. If you reduce the power consumption it will invariably reduce your range. So understand what you are doing. The antenna also plays a critical role in how far your module’s range is.

I hope, this brief explanation helps you get some insight into how wireless works. There is no magic there. If I have made any mistake in my explanation please do correct me on it.

Electronics Time!

Let’s get the crux of electronic circuits that can produce sine waves with desired frequency and phase from a DC power supply. The short answer is, oscillator circuits can be harnessed to generate sine waves. It can also be produced by brute force programming of a microcontroller (like Arduino) with AnalogWrite(), however, it is an overkill and unnecessarily putting pressure on your processor. Once you can generate these high-frequency sine waves the wire (Antenna) magically produces the Electro-magnetic waves. There is excellent introductory material on Oscillators, youtube series by the channel learnelectronics on oscillator, do check it out. For a more in-depth treatment, you may refer to Sedra and Smith’s chapter on ‘Oscillators and Waveform-Shaping Circuits’.

Here I summarize circuits using the Op-Amp. However, using Op-Amps is not the only way to generate sine waves. Other common ways to generate oscillations are i) Quartz crystals (most common in microprocessors) and ii) Vaccum tubes (generally used for 100 Ghz+ frequencies).

Image result for op amp
Various Op-Amps. Most common is IC741. However, 2, 4, 8 and more op-amps in 1 IC are also available. Even if you are not an electronics person, it is worth knowing what an Op-Amp can do.
Quartz Crystal. 2-pin and 4-pin config can be bought as low as US$ 0.25, see: digikey: https://bit.ly/2Rdibkm
Image result for gunn diode
Gunn Diode. These are going to be a lot harder to obtain. But you probably have no utility to generate 100 Ghz+ waves unless you are into developing satellites communications or military equipment.

Sine Waves from Op-Amps (IC 741)

The logic to this procedure is that we take square waves (PWMs) as input and use integrator circuits. An integrator circuit literally integrates (yes integration from calculus) the input waveform. So if you integrate square wave you get a triangular wave. Then you stack another integrator circuit which takes as input the triangular waves. This produces parabola shaped waveform. These look almost like sinewaves. This is a simple way to generate sine waves.

So, there are really 2 sub-circuits here a) Square wave generation b) Integrator circuits. Both of these can be achieved by using an Op-Amp and a few resistors and capacitors. Next, I simply list the circuits, explanations on how these work can be obtained from Sedra-Smith book (authentic source) or just try searching on Google.

Image result for op amp astable multivibrator
Astable Multivibrator with Op-Amp aka Square Wave Generator
Image result for integrator with op amp
Integrator Circuit with Op-Amp

Sine Wave – Wein Bridge Oscillator

The above way using square waves was rather a hacky wave to generate look alike sine waves. They were not real sine waves. The Wein-Bridge Oscillator is a way to generate true sine waves. It can be realized with an Op-Amp. You will need far fewer components that the above hacky way. The full explanation on how Mr. Wein came up with these circuits is beyond my scope for now, but it is easily possible to dig it up. Changing the values of resistances and capacitances, you can achieve various frequencies. The governing equation (formula) is available on wikipedia page.

Image result for wien bridge oscillator op amp
Wein-Bridge Oscillator

Phase Shift Oscillator

To achieve phase shift in an input sine wave, you really want to delay the signal in time. RC circuits offer a convenient way to achieve this. So you stack up a few RC circuits at the output of your sine wave generator and you should achieve the phase shift. Below is an example phase shift generator but it is not a true sine wave phase shift. For true sine wave phase shift, you want to copy the RC circuits below in front of the Wein-bridge. Changing the capacitances and resistance will let you shift the phase by various angles.

Image result for phase shift oscillator
Phase Shift Oscillator

Key Take Away from these Circuits

You might ask, what’s the point in understanding this. Here is my explanation. Speaking from a purely practical standpoint, you do not need to understand the oscillator circuits, but when it comes to debugging, it is useful to know what lies under the hood. In a typical communication module like the HC-12, there is a communication IC and a microprocessor which controls this IC. The oscillations are generated with a crystal. This IC has oscillator circuits most likely built with crystal oscillators and Op-Amps (and Op-Amps can be built with transistors). Finally, your microprocessor (Arduino or whatever computer you use) talks to this microprocessor via the serial communication on the HC-12 module which in turn signals the oscillator circuits on the communication IC.

Antenna

Designing an antenna is whole another fascinating field of study. The shape of the antenna will influence the range of your module. There are two general kinds: a) omnidirectional antenna (same transmitting power in all direction) and b) directional antennas (more transmitting power in particular directions). This video from RCModelReview youtube channel is an excellent and easy to understand resource on Antenna and some of the underlying mathematics.

Antenna 101

Common Communication Modules

Often time, you will likely never do wireless communication absolute basics but typically make use of modules available in the market. Even the super hardcore people buy the communication ICs and the ‘not too hardcore people’ buy the modules. Some common modules are HC-05, HC-06, HC-12, NRF24 etc. However, there are several more available. I have with me HC-12 which I am going to play with. HC-12 is a module that implements Bluetooth LoRa (Long Range). Do check out its datasheet for more details.

Time for Fun!

HC-12 with USB-TTL device

Here I connect the device to my PC using the USB-TTL device. I use the cutecom software on Ubuntu to send and receive the serial commands to the HC-12. In general, any programming language that can write files on the system can be used here. Even google-chrome has a way to write to serial devices.

HC-12 with Arduino Serial (Teensy)

Here I code up a simple Arduino Sketch. It receives data from PC-serial communication and sends this data to HC-12. Also, the data it receives from HC-12 is sent to PC-serial using the Arduino.

Gotchas: I spent a few painful hours trying to figure out why things are not working. And my conclusion was that “SoftwareSerial.h” is the problem. Do not use it. Instead, use AltSerial or NewSoftwareSerial. Also, a subtle point is that not all the ports on Arduino can be used for serial communication. Check your Arduino device’s modules page, it varies by the exact make of your board. I have teensy with me. Also the HC-12 has no reverse polarity protection, I ended up burning one of my modules, so I recommend, you ensure all your VCC voltage is 5V and you plug the power supply pins for the HC-12 module correctly. Also note the RX of HC-12 goes to TX of Arduino and vice versa.

Here is the Arduino code to accomplish this:

#include <AltSoftSerial.h>
AltSoftSerial altserial;//for Teensy 2++ TX=25 RX=4
#define led 6
void setup() {
  Serial.begin(9600);
  altserial.begin( 9600 );

  //blink 1 time - indicates that setup is complete
  digitalWrite(led, HIGH);
  delay( 500 );
  digitalWrite(led, LOW);
  delay( 250 );
  while( !Serial ) { delay(100); } //wait until usb serial is connected
  digitalWrite(led, HIGH); // once the PC serial is established ON the LED
  Serial.println( "AltSoftTest begins" );

}

void loop() {
  if( !Serial ) {
    // if PC serial is terminated, than turn off the led
    digitalWrite(led, LOW);
    delay(100);
  } else {

    // When you receive the data from PC send it to HC12
    while( Serial.available() ) 
    {
      altserial.write( Serial.read() );
    }

    // When you receive the data from HC12 send it to the PC
    while( altserial.available() )
    {
      Serial.write( altserial.read() );
    }
  }

}

HC-12 to HC-12 Communication

Here I set the SET pin of the HC-12 module to logic, 5 volts. The code on both Arduino is same as above. You can see whatever we type into the serial monitor of 1st Arduino shows up in serial monitor of 2nd Arduino. So also, whatever we type into the serial monitor of 2nd Arduino shows up in the 1st Arduino. Notice that, I have set the communication channels to C001 using the AT commands on both modules. If you wish to enter the AT mode be sure to ground the SET pin of the HC-12 module.

In the next short clip, I connect an LED to the receiver Arduino. A simple button is connected to sender Arduino. Whenever the button is pressed the sender starts transmitting ‘a’s and when the button is unpressed it transmits ‘b’s. On the receiver side, whenever ‘a’ is received the LED lights up whenever ‘z’ is received the LED turns off. All the other things received are ignored.

// Sender

#include <AltSoftSerial.h>
AltSoftSerial altserial;

int button = 17;
int led = 6; 
void setup() {
  pinMode(button, INPUT);
  //Serial.begin(9600); 
  altserial.begin( 9600 );

  // Blink the status led for info. 
  pinMode( led, OUTPUT );
  digitalWrite( led, HIGH ); 
  delay(500);
  digitalWrite( led, LOW );
  delay(500);
  digitalWrite( led, HIGH ); 

}

void loop() {
  // Read the button
  int val = digitalRead( button );

  // Send via HC12 based on what it sees on the button
  if( val == 0 )
    altserial.write( 'a' );
  if( val == 1 )
    altserial.write( 'z' );
  delay(100);

}
// Receiver
#include <AltSoftSerial.h>
AltSoftSerial altserial;
#define led 6
#define display_led 15
void setup() {
  Serial.begin(9600);
  altserial.begin( 9600 );

  //blink 1 time - indicates that setup is complete
  pinMode( led, OUTPUT );
  digitalWrite(led, HIGH);
  delay( 500 );
  digitalWrite(led, LOW);
  delay( 250 );
  while( !Serial ) { delay(100); } //wait until usb serial is connected
  digitalWrite(led, HIGH); // once the PC serial is established ON the LED
  Serial.println( "AltSoftTest begins" );

  // output led 
  pinMode( display_led, OUTPUT ); 
  digitalWrite( led, HIGH );
  delay( 100 );
  digitalWrite( led, LOW );

}

void loop() {
  if( !Serial ) {
    // if PC serial is terminated, than turn off the led
    digitalWrite(led, LOW);
    delay(100);
  } else {

    
    // When you receive the data from HC12 send it to the PC
    while( altserial.available() )
    {
      char ch = altserial.read();
      if( ch == 'a' )
        digitalWrite( display_led, LOW );
      if( ch == 'z' ) 
        digitalWrite( display_led, HIGH );
      Serial.write( ch  );
    }
  }

}

Final Thoughts

In this post, we explored how to use the HC-12 module with a PC and with an Arduino. We also discussed some Gotchas.

Some other points that need to be verified: a) What is the range and effect of antennas b) How about the security. Can anyone with an HC-12 module read our messages and send us messages? How should we protect against such attacks c) There is an Arduino-JSON library which lets us encode and decode json messages on Arduino. It needs to be explored. Be sure to set appropriate buffer sizes. d) What happens when multiple HC-12s send messages on the same channel. How about multiple HC-12s on different channels.

Finally, if you see any mistakes, please do let me know via comments.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s