Today, we are going to learn how to control the GPIO pins on a microcontroller based on
the current time, without having to write any code. Think of it as a digital timer that
can then be connected to a relay to automatically control the lights around you. We will be
using something called Blynk and if you are not familiar with it then it’s an extremely
easy way to program a microcontroller.
Say you want to switch ON a lamp, device or heater during a particular time but you do
not want to create a full home automation system using home assistant and so on. You
do get timers like these ones which can be connected to your sockets. You can control
the time by setting these switches and this will work for the most part. If you want to
create a DIY solution then the most obvious way is by adding a real time clock or RTC
module to an Arduino board so that you can keep track of time. You can then set the switch
ON and switch OFF times to create schedules. However, both these solutions are not convenient.
Any changes will have to be manually made which is not entirely straightforward. Also,
if you are not at home for instance, then you cannot make any changes which is also
an issue.
I personally think that the simplest way to solve these issues is by using Blynk - which
is an IoT platform that can be used with several internet enabled boards. The app allows you
to program your microcontroller using graphical widgets. You can also write code to interface
additional modules and integrate everything. The best part is that it is free to use and
provides everything you need to get started. Since it is connected to the internet, we
can use network time to creates schedules which also means that we do not have to worry
about daylight savings and so on as all of that will be taken care of for us.
Here’s an overview of how it works. First, we need an internet capable board along with
a smart phone. Blynk supports a number of boards but I will use the WeMos D1 mini board
as it is cost effective and has everything you need built into it. We then have the app
which we use to create a program, think of it as an Arduino sketch but instead of being
text based, it is graphical. The app is only used to program the board and make changes.
Once programmed, it can be closed and the phone can even be switched OFF as the board
will continue executing the program.
The board and the app are connected together by a server. By default, we will use the Blynk
server which is free to use, however you can also host your own server on a raspberry pi
for instance. In order for both to communicate, they need to be connected to the internet.
The app will generate something called an authentication token which needs to be programmed
to the board. This is unique for each program that we create and it links the two together.
By default, you can control the board from anywhere in the world and also view its status.
Start by downloading the app and setting it up. You will need to provide an email address
as the authentication token will be sent to it. You will then end up with something like
this. Tap this button to start creating a new project and give it a name. You also need
to select the correct board and as you can see, you have plenty of options to chose from.
Select the connection type which is WiFi and then hit the create button. This is the work
space where that will be used to create the program. This option gives you the project
settings and you can even share it with others. Clicking this button will show you all the
available widgets and you can click the information icon to learn more about them. Each widget
uses something called energy and by default you have a balance of 2000. This limits the
number of widgets that you can use within a program but you can also buy more energy
for bigger projects. Each time you delete a widget or project, you get back the energy
associated with it.
By now, you will have received an email like this which consists of an authentication token.
We need to add this to the microcontroller so start by opening the Arduino IDE. Head
over to the library manager, search for Blynk and then install it. Once done, open up the
examples option, select Blynk to view all the available template files that can be used
for different boards. I am going to select nodemcu for the board that I am using. This
is the default template for this particular board.
All we need to do here I copy the authentication token from the email and paste it like so.
You also need to add your WiFi network name and password. That’s all you need to do.
This statement will set up the connection to the cloud and this line will execute the
program that we create with the APP. Make sure you have the right board support package
installed for your board, select the correct COM port and then upload the sketch to your
board. You can open up the serial monitor to view the board status. I am going to connect
an LED to pin D1, using a 330 Ohm current limiting resistor so that we can control it.
All we need to do now is use the APP to program the board.
Add the button widget by tapping the + icon, then tapping the widget. It will be displayed
within the workspace. You can then tap it to view the options. Start by giving it a
name and then tap the pin option to select a pin. I am going to select pin D1 since we
connected an LED to that pin. Go back to the workspace and then click this play button.
The code will automatically get uploaded to the board. Now, the LED will switch ON whenever
you press the button within the app. The button is acting like a pushbutton but what if we
wanted it to act like a switch or toggle button instead? To do that, we first need to stop
the program by clicking this icon and we then need to tap the button to view its settings.
You can toggle the mode option to switch and then go back to run the program. Now, you
can tap the button to toggle the LED state.
As you can see, it’s extremely simple to control a microcontroller without having to
write code. You have a lot of widgets to choose from which allows you to create some interesting
programs. Before we create a schedule, I am going to add the Value display widget and
set it to display the status of pin D1. You can drag the widgets around as you like and
then run the program. Now, when I toggle the LED, I will be able to view the pin status.
In order to create a timer, we will need to use the eventor widget. This is what it looks
like so tap it to open up the settings. Start by creating a new event. We need to define
a trigger for this event. Select the time option as we want to set the time. You can
now set the schedule. Start by selecting the time zone, the start time and the days that
you will like the schedule to run. Make sure the days are listed in the action as shown
here. You can change these to suit your requirements as needed. Then click the set time option
to save it. Once you set the trigger, you need to select an action that will be executed
at that time. You can turn ON a pin, turn it OFF, change its value and so on. I will
select the turn ON option and I will select pin D1. Once this is done, hit OK and you
will then be able to view a summary of this action. You can then add another event to
switch OFF the pin at another time as seen here. This way you can create a schedule for
the GPIO pin. You can keep creating more events to do other things.
All you need to do now is click the run option so that it gets uploaded to the board. The
LED will switch ON at the time that you set as seen here. It will then switch OFF at the
OFF time that was set. You will also be able to view the pin status on the dashboard and
you will be able to manually control it if needed.
You can extend this to control multiple GPIO pins and devices, depending on your intentions.
It is also extremely easy to update the schedule as opposed to having to update the code.
Once you know how to control a GPIO pin, you can use to control all sorts of other things.
If you add a relay then you can use it to control lamps and other high-powered appliances.
Be careful when working with mains voltages but if you plan on doing this then do check
out these tiny AC-DC converter modules which are compact and useful. They can be used to
power the microcontroller and also power the relay while being a bit safer.
So that’s a quick introduction to using Blynk to program and control microcontrollers.
I hope you found this video useful. Don’t forget to like, share and subscribe if you
haven’t already. Thank you for watching and I will see you in the next one.