Spectra IoT conference logo

The IoT Conference for Particle Customers + Users

Join Virtually

June 21, 2023

Building Your First IoT Device: A Smart Plug Prototype

Eric Yuan article author avatarEric YuanMarch 24, 2023

You’ve probably heard of the Internet of Things (IoT) and used various IoT-enabled products like Amazon Echo or Nest Thermostats. But what exactly is IoT, and can anyone build an IoT product?

IoT refers to devices that can be controlled over the internet using wired or wireless technology. It doesn’t include traditional TV remote controls, as they use infrared lights without any internet connection. In contrast, your smart refrigerator is an IoT device because it connects to the internet via Wi-Fi, enabling it to report its status to your phone.

Building an IoT product is more accessible than ever, and in this blog post series, we’ll guide you through building an IoT-featured product from concept to reality. In this first installment, we will focus on creating a smart plug prototype.

Choosing a Demo: The Smart Plug

We decided to create a smart plug as our demo project because it is relatively simple in concept and can be both a useful and educational tool. The only concern when building a smart plug is handling high voltage during debugging, so safety precautions are necessary.

Product Requirements Document (PRD)

Before diving into the actual building process, we need to define the features and functions our smart plug should have. Here’s a brief PRD for our smart plug:

  1. Must use 2.4G or 5G Wi-Fi to connect to the internet.
  2. Must have one high power relay to control the plug on and off.
  3. Must have a button to control the plug on and off manually.
  4. Must have the ability to monitor voltage, current, and power used by connected devices.
  5. Must be controllable and display power data via an app.
  6. The plug itself should be ultra-energy efficient.

Function Diagram

We’ve defined our requirements, so now we need to create a function diagram to visualize how to achieve these goals.

Figure 1

Our smart plug will consist of the following components:

  1. DPST relay: Controls the power line for connected devices.
  2. Power Monitor IC: Monitors voltage, current consumption, and total power consumption.
  3. AC to DC converter: Supplies DC power source for the MCU, Wi-Fi module, power monitor IC, and DPST relay.
  4. MCU + Wi-Fi module: The plug’s brain, connected to the internet for user control and information retrieval.
  5. Enclosure: Houses all components to create a finished product.

Components Selection

For the MCU, we chose the Particle P2, which includes Wi-Fi capabilities.

Figure 2

For the power relay, we opted for a generic 20A SPST Relay rated at 125VAC (SRA-12VDC-CL).

The power monitor IC plays an important role, controlling the accuracy of current and voltage.  I selected the MCP39F521 based on publicly-available datasheet, and an available demo board.

Figure 3 – Power Monitor Demonstration Board (Part # ADM00686)

Selecting an AC to DC converter was initially daunting, as it requires analog experience to design the converter efficiently and safely, especially in a compact space. The datasheet for my demoboard indicated it’s straightforward if I use the LNK30x series.

The only concern is to deliver enough power for our application, which prompted me to build the below table to calculate the total power consumption needed from this chip.

Figure 4

It’s 1.7484w total needed for my components. If we choose a 12V to 3.3V DC-DC regulator with at least 80% converter efficiency, then the minimum power required for the AC-DC converter would be 2w.

0.6w+(1.0725w+0.0429w+0.003w)/0.8 = 1.998w

The LNK306 meets our demand, delivering 2.7w(12v/225mA), and allowing 0.7w power margin to be safe.



Now, to the hands-on stage!  It’s time to build a Minimum Viable Product (MVP) by assembling the hardware and developing the firmware.

Hardware: To quickly prove the compatibility of our selected hardware, I’ll use the P2 Hardware Development Kit (HDK), the MCP39F521 evaluation board, a relay breakout board, and a DC-DC regulator for prototyping.

Our goal is to validate the smart plug’s functionality based on the PRD using these components without any schematic design or PCB layout work.

Figure 5 – Assembled prototype


I’m a hardware engineer and far from an expert on coding, let alone how to send the voltage/current value from the monitor IC to the internet. But, my goal is to show you how easy it is for anyone to build an IOT product!

The P2 module I selected simplifies much of this complexity for me. Tasks like connecting a controller to the internet, how to send data to the cloud, etc. I can simply invoke a function from the Particle Library to publish the data to the cloud, then subscribe to the event so that the MCU will know it’s time to turn the relay on or off.  And during the prototype stage, I can easily verify all the functions by using the Particle Console 

Alternatively, the P2 module can be coded by a seasoned embedded firmware engineer using  C/C++. 

The firmware architecture is designed to control the relay via a physical button or a wireless command, and it will report energy monitor values to the user interface at scheduled intervals.

Figure 6

There are two ways to control the relay for the plug, through the physical button or by a command transmitted by wireless.  The physical button will always have the top priority and will toggle the plug’s current ON and OFF status once it’s pressed, while the command will put the plug into a fixed status. Essentially, if the plug was turned on by button, then a command to turn on the plug will not override the current plug’s “ON” status.

The energy monitor value will be reported to GUI, including voltage, current and power consumption from any plugged-in devices.

Below is the sample Arduino type code which runs on the P2 HDK board.

void setup() {               
 Serial.begin(9600);  //turn on serial communication
 smartPlug.begin(); // Pass in the appropriate address. Defaults to 0x74
 //register particle variables and functions call.
 Particle.function("plugRemoteControl", plugRemoteControl);
unsigned long readInterval = 0;
void loop(){
 uint8_t btnState = buttonStat();
 if (btnState==1){
   plugStatus =!plugStatus;
 }else if(btnState==2){
   //Serial.println("long press");
   readInterval = millis();
   UpbeatLabs_MCP39F521_Data data;
   UpbeatLabs_MCP39F521_FormattedData fData;
   int readMCPretval = smartPlug.read(&data, NULL);
   if (readMCPretval == UpbeatLabs_MCP39F521::SUCCESS) {
     smartPlug.convertRawData(&data, &fData);
     lineVoltage   = fData.voltageRMS;
     lineCurrent   = fData.currentRMS;
     lineFrequency = fData.lineFrequency;
     activePower   = fData.activePower;
   } else {
     Serial.printf("Error returned!%d\r\n",readMCPretval);

The Demo

In our demonstration, we successfully controlled a light bulb using IoT methods as well as manual finger press. Additionally, we reported voltage, current, and power usage, which effectively met our goals.


As promised, we have shown you how to build a quick prototype based on a rough concept. Our demo illustrates that the selected components are suitable for meeting our requirements. In the next installment of this series, we will dive deeper into both hardware and software design to achieve the following goals:

  1. Design a smart plug that meets EVT standards.
  2. Incorporate all the features found in similar products on the market.
  3. Develop a desktop GUI to display all the features.
  4. Create a fully functional smart plug that can be used just like an off-the-shelf product.

Stay tuned for the next post, where I’ll continue the journey and transform our IoT prototype into a polished and practical smart plug.

Comments are not currently available for this post.