How to create a remote control system of two LEDs with ESP8266,Arduino Cloud and PlatformIO

Introduction

Welcome to the article dedicated to the creation of an interactive circuit that exploits the potential of Arduino Cloud and the ESP8266, a microcontroller with integrated Wi-Fi module, to remotely control two LEDs. This project will walk you step-by-step through creating a system where one LED turns ON or OFF based on the Boolean value of a variable, while the light intensity of a second LED can be dimmed via Pulse Width Modulation (PWM), through an integer variable. Obviously this is an example made with LEDs but with this same system you can control other types of components or electronic devices.

This will be possible thanks to the use of Arduino Cloud, which will provide a personalized dashboard with a switch and a slider to control the two variables. The ESP8266, known for its Wi-Fi connectivity capabilities, will be at the heart of this system, allowing you to control the LEDs over a wireless connection. We will exploit the efficiency and flexibility of the Arduino Cloud to manage the communication between the ESP8266 device and the dashboard.

This solution will allow us to monitor and modify the variables remotely, offering practical and immediate control over the lighting of the LEDs. During the development process of this circuit, we will address fundamental concepts such as programming the microcontroller, using boolean and integer variables, interfacing with the Wi-Fi module, as well as managing the LEDs and controlling their brightness via PWM . We will also explore the features and possibilities offered by the Arduino Cloud, an intuitive and powerful platform for managing connected devices.

If you are passionate about electronics, automation and innovative technologies, this article will be your starting point for creating a customized LED control system through Wi-Fi connectivity.

What is Arduino Cloud?

Arduino Cloud is a powerful cloud-based platform that allows you to connect and manage your Arduino devices (and more) through the Internet. It is designed to simplify the interaction between physical devices and the digital world, offering advanced remote management, monitoring and control capabilities.

Arduino Cloud provides an intuitive and user-friendly interface that allows you to create custom dashboards (as in our case to control the circuit with LEDs using a switch and a slider). This means that, through a web interface or a mobile application, it is possible to access the dashboard and interact with the connected devices, without the need to manually program or configure the microcontroller.

Through Arduino Cloud, it is possible to monitor the status of the devices, send commands and receive data in real time. This opens up a wide range of possibilities for automation, remote monitoring and control of systems based on Arduino, NodeMCU and so on.

In essence, the Arduino Cloud is a complete solution for device connectivity, management and control, providing a simple and intuitive experience for remote monitoring and control. With its ease of use and the power of its features, the Arduino Cloud makes it possible to create interactive and connected systems that can be controlled and managed anywhere there is an Internet connection.

What components do we need?

The list of components is not particularly long:

Project realization

The wiring scheme

Before realizing the circuit let’s look at the pinout of the board:

NodeMCU ESP8266 pinout
NodeMCU ESP8266 pinout

We will use GPIOs 4 and 5 to connect the LEDs. The brightness of one of the two LEDs can be adjusted by driving it with the ESP8266 via a PWM signal whose duty cycle varies as the value contained in a specific variable varies, a value ranging from 0 (LED OFF) to 255 (LED completely ON) passing through intermediate values. The other LED, on the other hand, can only be ON or OFF. Also this LED will be driven by the ESP8266 with a boolean signal (true/false).

At this point you can proceed with the creation of the circuit by following the connection diagram below.

The LEDs are connected to the ESP32 via 100Ω resistors to limit the current flowing through them and avoid burning them (and burning the digital outputs to which they are connected).

The LED has two terminals (called anode and cathode) and, like all diodes, it is a component that has its own polarity: it passes the current when it is forward polarized (i.e. the voltage at the anode is greater than that at the cathode) and it blocks current when it is reverse polarized (i.e. the anode voltage is lower than the cathode voltage). The voltage between the anode and cathode, which we will indicate with Vd, varies according to the color of the light emitted. In particular we have that:

  • Vd = 1.8 V for red LEDs
  • Vd = 1.9 V for yellow LEDs
  • Vd = 2 V for green LEDs
  • Vd = 2 V for orange LEDs
  • Vd = 3 V for blue LEDs
  • Vd = 3 V for white LEDs

Below is the wiring diagram created with Fritzing:

The wiring diagram
The wiring diagram

How do we identify the anode and cathode of the LED? We do this by looking at its terminals. The longest corresponds to the anode. Also, the LED body has a flattening at one point on the edge indicating that the nearby terminal is the cathode.

So if an LED doesn’t light up it’s possible that it’s wired upside down. In this case, to make it work, simply reverse the connections.

How do you calculate the resistance to connect to the LED?

Please note: this paragraph deals with the calculation of the limiting resistance in a theoretical way and requires a minimum knowledge of the basics of Electrotechnics. Therefore it is not essential for understanding the rest of the project and can be skipped by the reader not interested in such theoretical aspects.

As we have already said, the resistor between the generic GPIO and the LED serves to limit the current flowing through the LED. But how can we calculate its resistance value? Ohm’s Law comes to our aid which says that the potential difference across a resistor (i.e. the voltage measured at the ends of the resistor) is proportional to the current I flowing through it and the constant of proportionality is precisely the resistance value of the resistor R:

V2 - V1 = RI

Please note: for the sake of precision it must be pointed out that while the resistor is the physical component (the actual object), the resistance is its value. So it is improper (even if it happens frequently) to call the resistor with the term resistance.

We can see Ohm’s Law on a simple circuit consisting of a voltage source (the circle on the left) and a resistor:

Representation of Ohm's Law
Representation of Ohm’s Law

The voltage (or potential difference) V2 – V1 impressed by the voltage source on the resistor is equal to the product of R by I.

Now let’s see a slightly more complex scheme where the usual voltage generator, the resistor and a red LED are present:

Circuit for calculating the current limiting resistor on the LED
Circuit for calculating the current limiting resistor on the LED

In our case the Vg represents the voltage present at the digital output of the ESP32 when it is HIGH and is therefore equal to 3.3V.

Vd is the voltage across the diode (between anode and cathode) when it is forward biased (ie when it is carrying current). Having chosen a red LED, we know from the previous table that Vd = 1.8V.

We need to determine the R-value of the resistor. We still have one unknown: the value of the current I which must flow in the circuit when the pin is in the HIGH state.

Please note: when the digital pin is in the LOW state its voltage (ie Vg) is zero, it follows that also the current I in the circuit is zero.

LEDs generally cannot withstand currents greater than 20mA, so we impose a maximum current of 15mA to be on the safe side.

By Kirchhoff’s voltage law we have that:

Vg - Vr - Vd = 0

From which we derive that:

Vr = Vg - Vd 

Passing to the real values, we have that:

Vr = 3.3V - 1.8V

It follows that:

Vr = 1.5V

But, by Ohm’s Law, we have that:

Vr = RI

from which:

R = Vr / I

Substituting the real values:

R = 1.5V / 0.015A

The result is a value of R equal to 100Ω.

Let’s create the objects and the dashboard on the cloud

First, if you haven’t already done so, you need to create an account on Arduino Cloud. Once you have logged in you will find this page:

Arduino Cloud home page
Arduino Cloud home page

Click on the GET STARTED button and you will be sent to the page:

Second page of Arduino Cloud
Second page of Arduino Cloud

Click on the IoT Cloud button. The Things page will open.

At this point you will proceed to:

  • create the device that will map the board
  • create the object (thing) with the variables (led_on_off and pwm_value) associated with the device created in the previous step
  • create the dashboard to be associated with the variables

We will see the procedure just listed with this video:

Project creation on Arduino Cloud

PLEASE NOTE: it is important to note that in the video, at 0:20, I have downloaded a pdf containing the connection parameters to the device. They are called Device ID and Secret Key and we will need them later. So download this pdf yourself and keep it somewhere.

Obviously the Device ID and Secret Key parameters are specific to each device, therefore they change when the device changes.

Once the procedure is complete, go to the test_thing object and click on the Sketch tab:

Object properties screenshot
Object properties screenshot

Then press the button Open full editor:

Sketch screenshot
Sketch screenshot

A window like this will open:

Screenshot of the complete code
Screenshot of the complete code

What interests us (and which we will use later) are the main .ino file (the one called test_thing_jul08a.ino in the photo and which will have a different name from yours) and the thingProperties.h file. We will use these two files later in our PlatformIO project.

Let’s create the PlatformIO project

We have already seen the procedure for creating a PlatformIO project in the article How to create a project for NodeMCU ESP8266 with PlatformIO.

Do not install the libraries indicated in that article because we don’t need them in this project. Instead, following the usual procedure, install the Arduino_ConnectionHandler library:

Installing the Arduino_ConnectionHandler library
Installing the Arduino_ConnectionHandler library

and the ArduinoIoTCloud library:

Installing the ArduinoIoTCloud library
Installing the ArduinoIoTCloud library

Now edit the platformio.ini file to add these two lines:

monitor_speed = 115200
upload_speed = 921600

so that it looks like this:

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_speed = 115200
upload_speed = 921600
lib_deps = 
	arduino-libraries/Arduino_ConnectionHandler@^0.7.3
	arduino-libraries/ArduinoIoTCloud@^1.10.0

At this point go back to the cloud platform, copy the contents of the .ino file:

The .ino file
The .ino file

and paste it into the main.cpp file of the PlatformIO project, in order to completely replace its contents.

Now create in the include folder a file that you will call thingProperties.h, go to the cloud platform, copy the content of the thingProperties.h tab:

The thingProperties.h file
The thingProperties.h file

and paste it into the thingProperties.h file you just created inside the Platformio project.

At this point you will have to edit this file in order to connect it to the cloud:

const char DEVICE_LOGIN_NAME[]  = "XXXXXXXXXXXXXXXXXXXXXXXXXX";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password

As you can see, in this section there are some parameters to set.

The first parameter (DEVICE_LOGIN_NAME) was automatically added and you can find it in the pdf you downloaded when you created the object (you can find the reference in the previous video). In the pdf the parameter is called Device ID.

The SECRET_SSID and SECRET_OPTIONAL_PASS parameters are, respectively, the name and password of your WiFi network. So, instead of SECRET_SSID you will put your network name between double quotes (“) and instead of SECRET_OPTIONAL_PASS you will put your network password (always between double quotes).

The last parameter, SECRET_DEVICE_KEY, can always be found in the pdf you downloaded from the platform with the name Secret Key.

So the section in question should look like this:

const char DEVICE_LOGIN_NAME[]  = "ddcb75fe-12e7-427f-a08e-262e4e3d8292";
const char SSID[]               = "my_wifi_network_ssid";    // Network SSID (name)
const char PASS[]               = "my_wifi_network_password";    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = "PH3GPO2W4GBOMKZVMJXM";    // Secret device password

As you can see, I put in the DEVICE_LOGIN_NAME field the value taken from the pdf under Device ID (in this case ddcb75fe-12e7-427f-a08e-262e4e3d8292), in the SSID field the SSID of my WiFi network (in this case my_wifi_network_ssid), in the PASS field the password of my WiFi network (in this case my_wifi_network_password) and in the DEVICE_KEY field the value taken from the pdf under the Secret Key item (in this case PH3GPO2W4GBOMKZVMJXM).

It’s time to edit the main.cpp file.

You will initially include the Arduino library and the thingProperties.h file like this:

#include <Arduino.h>
#include <thingProperties.h>

Next you will define the digital pins that will drive the LEDs:

#define  BOOL_LED_PIN_PIN 4
#define  PWM_LED_PIN_PIN  5

In the setup function, change the speed of the serial port from 9600 to 115200:

Serial.begin(115200);

instead of:

Serial.begin(9600);

At the end of the setup function define the two pins as output:

pinMode(BOOL_LED_PIN_PIN, OUTPUT);
pinMode(PWM_LED_PIN_PIN, OUTPUT);

Now you will need to modify the two functions onLedOnOffChange and onPwmValueChange.

Inside the onLedOnOffChange function you will put this piece of code:

Serial.println(led_on_off);
digitalWrite(BOOL_LED_PIN_PIN, led_on_off);

This code does nothing but print the value of the boolean variable led_on_off on the Serial Monitor and drive the corresponding LED based on the value of this variable. If led_on_off contains the FALSE value, the LED turns OFF (or stays OFF) while if it contains the TRUE value, the LED turns ON (or stays ON).

Inside the onPwmValueChange function you will put this piece of code:

Serial.println(pwm_value);
analogWrite(PWM_LED_PIN_PIN, pwm_value);

This code, as usual, prints the integer value contained in the pwm_value variable and drives the corresponding LED by assigning it a brightness value corresponding to the integer value contained in the variable. So, if pwm_value is 0 the LED will be OFF, if pwm_value is 255 the LED will be ON at full brightness. For intermediate values ​​the LED will have an intermediate brightness.

As you may have guessed, the two functions onLedOnOffChange and onPwmValueChange are called when the values ​​contained in the two variables change (values ​​which in turn change if we operate the switch and slider in the Arduino Cloud dashboard).

Of course you can download the project from the following link:

Now compile the project and upload it to the board. If all went well you should see how the two LEDs behave when you operate the dashboard controls, as shown in the following video:

Our project in operation

Newsletter

If you want to be informed about the release of new articles, subscribe to the newsletter. Before subscribing to the newsletter read the page Privacy Policy (UE)

If you want to unsubscribe from the newsletter, click on the link that you will find in the newsletter email.

Enter your name
Enter your email
0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
Scroll to Top