We are now going to look at the different options that are available when it comes to
controlling and reading GPIO pins using ESPHome. By knowing how to do this, you will be able
to interface basic sensors, modules and obtain their status. In addition to this, all of
these entities can then be used within node-RED to create automations. So let’s start by
reading inputs.
As you are already aware, ESPHome communicates with something called nodes, that are nothing
but WiFi-enabled boards like these ones. We then connect modules, sensors, switches and so
on, to these boards. Binary sensors give you a logic 1 or 0 and the simplest way for us
to simulate them is by using a tactile switch like this one. All we need to do is display
its status on the dashboard. I have already gone ahead and created a new node which uses
the WeMos D1 mini board. We have covered this in a previous video so watch that if
you are not familiar with this process.
If this is the first time you are using a tactile switch, then keep in mind that it
has four terminals like so. These two are connected internally and let’s call them
A. The other two are also connected internally and let’s call them B. Under normal conditions,
terminals A & B are both disconnected, however, when the switch is pressed, the terminals
A & B will be connected. To interface it with the board, we simply connect terminal A to
pin D2 and terminal B to ground. If we read the input in D2, then we will be able to read
the status of the switch. If I press the switch, then the two terminals will get connected,
which means that D2 will be held LOW. However, if I release the switch then there is no guarantee
that D2 will be HIGH and that is the reason why we add a pull-up resistor which holds
the pin D2 HIGH when the switch is not pressed. By doing this, we create two definite switch
states that can be detected.
ESPHome has a built-in component called GPIO binary sensor which can be used to read the
switch status. This is an example configuration that can be used. You will notice that there
is an additional property called device class which is mainly used for binary sensors. You
can click this link to learn more about binary sensors and then this link to learn about
the device class property. You can use either of these values to suit that device that you
are integrating. For example, if we use the window option, then the status will be open
when the input is HIGH, and it will be closed when the input is LOW.
You have other options to choose from here or you can simply not specify anything and
you will be able to view the input status as ON/OFF instead. I am going to copy this
example configuration and paste it in the node configuration file. Now, if I hit the
upload button, the node will automatically get updated over the air. Once done, I can
simply add it to the dashboard to view its status. We know that when the switch is not
pressed, the input will be HIGH because of the pullup resistor. And this is reflected
on the dashboard. Now, if I press the switch, the input will go LOW and the sensor status
will change to OFF. In this way, we can obtain the status of a particular input pin.
If you do not want to use an external pull up resistor, then you can enable internal
pullups by modifying the configuration as shown in this example. Most microcontrollers
have internal pullup resistors that can be used for these purposes. Before we add the
internal pull up resistors, let’s remove the external one and test it. When I press
the switch, it says the window is closed, but when I release the switch it still says
it is closed – which is incorrect. So let’s update to the configuration to give us something
like this and finally, we can upload the new configuration. The window status is now correctly
being represented, both when I press the switch and then when I release it.
Now, what if we want the opposite effect? Meaning we want the status to be Open when
the switch is pressed, which also means that the input is LOW? This can be done by adding
the inverted property in the configuration, like so. If we upload this to the board then
you will see that the status is closed by default. However, when I press the switch,
the input goes LOW, and the status reports Open, which is what we wanted to do.
What we have done so far is good enough if you want to simply read the status of an input.
However, if you interface mechanical switches like the ones we used then there is one more
thing that you should take care of.
This is the voltage at the input pin when I release the switch. You will notice that
it does not rapidly transition from LOW to HIGH, but instead, oscillates a bit before
taking the final value. The amount of oscillation will depend on a number of factors including
the force that was applied while pressing the switch. Keep a note of the time interval
as this oscillation happens extremely quickly in the order of microseconds, but microcontrollers
can still detect these changes depending on the application.
The oscillation is the result of the mechanical construction of the switch and it can lead
to the wrong values being read, depending on the program. One way to eliminate this
issue is by adding some sort of filter which is normally called debouncing.
In our case, ESPHome does provide a built-in property that we can use. By using this property, the
microcontroller effectively waits to see if the signal is HIGH for at least 10ms, if it
is so, then it will change the status otherwise it will reject the change as being noise.
Now that we know how to read inputs, we need to learn how to control outputs. I am going
to add this LED to the board using a current limiting resistor.
This is a reference diagram that you can use. In summary, if pin D1 is HIGH, then the LED
will start glowing, else, it will remain OFF. ESPHome makes it easy to control this GPIO
pin and we can use the GPIO Switch component to do this. Simply copy this example configuration
entity, paste it in the node configuration file, update the pin and name like so and
finally, hit the upload button. Wait for it to complete uploading and then add it to the
dashboard. Now, you can control the LED by toggling this switch.
By default, the LED will remain in the OFF state every time the board is powered up and
you will have to manually control it using the switch on the dashboard. There may be
cases where you would want the board to remember the last state that was set and that can also
be easily done by adding a couple of lines to the configuration file.
You have four options to select from. You have the choice of always setting it ON or
OFF or you can attempt to restore the last state. If for some reason the state cannot
be restored, then you can fallback to either switching it ON or OFF. I am going to set
the restore mode to RESTORE_DEFAULT_OFF like so.
Before we proceed, there is one more thing we need to keep in mind. The switch state
and other variables are stored in memory called RAM. This is the reason why when you switch
OFF power, it does not remember the last state. In order to preserve the state, we need to
store it in long term memory which will not be erased when power is removed. The Wemos
D1 mini board has FLASH memory that can be used for this purpose.
All we need to do is add this line to the configuration file and set the property to
true, like so. Now, if we upload the configuration, then the board will remember the state of
the output pin. First, I’m going to set the output HIGH such that the LED switches
ON. Now, if I unplug the board and plug it back in, you will see that the LED starts
glowing and the status on the dashboard reflects this. Now, if I switch OFF the LED and power
cycle it, you will see that it retains the last state and remains OFF.
When you are using FLASH memory, you need to be aware that it has a limited number of
life cycles which is usually around 100,000 writes. This means that if you constantly
write data to it then you might damage it, which will prevent you from writing to it
further.
What we have here is an output pin that can be controlled using this toggle switch. When
the switch is ON, the output is HIGH. When the switch is OFF, the output is LOW. What
if you wanted it the other way around – the output being LOW when the switch is ON?
This can also be easily done by using the inverted property like so. By default, the
output will be HIGH, which means that the LED will be ON. When I toggle the switch to
the ON position, the output pin will go LOW, which means that he LED will switch OFF. In
this way you can create an inverted switch which can also be considered to have an active
LOW output.
Now that we’ve covered the basic types of output signals there’s one more thing we
need to talk about. Say you have a garage door that needs to be opened. You would perhaps
send a logic HIGH signal to it, wait for some time and then switch it OFF.
This can be done by using the momentary switch configuration. It uses something called a
template and you can learn more about it by clicking this link. Think of a template as
a mini script and you will be able to understand it better when we look at the example configuration.
I’m simply going to copy this example over and change the pin to D1. We have given the
output pin an ID which we will use in the template. Within the template we have a name,
icon and we then define a turn on action. What this means is that, when we turn ON the
switch using the dashboard, we will execute all these commands that are contained within
the template. What we are doing here is switching ON a pin with the ID relay, waiting for 500ms
and then switching it OFF. If you assign different IDs to multiple GPIO pins, then you can control
all of them with a simple switch. I’m going to update the delay to 1000ms and we will
then upload it to test it.
You will have to add the entity to the dashboard again as we have created a template. Once
done, you can toggle the switch to watch the LED switch ON, wait for a second, and then
switch OFF. In this way, you can create a custom output to help you interface different
modules.
The wemos D1 mini board operates at 3.3V and most of the devices around us work at much
higher voltages. These days, many of the modules work at 3.3V but some do still work at 5V.
For those, you can level shifting boards like these. There are numerous guides available
that help you get started with them. In summary, you simply need to connect a 3.3V and 5V power
supply as shown here. This section becomes your low voltage section, which is also denoted
by the symbols LV. The other side becomes your high voltage section or HV. If you apply
a 3.3V signal to one terminal, then you will obtain a 5V signal at the corresponding terminal.
This can then be connected to any 5V device or module. Similarly, by connecting a 5V signal
to either of these pins, you will obtain the 3.3V version of that signal. This can be then
be connected to an input pin so that you can read its status.
If you want to interface a desk lamp like this or any other device that works at a higher
voltage, then you would need to use something like a relay. If you are just starting out
with electronics then I would not recommend working with mains voltages as you can seriously
injure yourself or even cause a fire. Here’s a very common relay and you have a total of
5 terminals. This is what the inside of a relay looks like – we have an electromagnet
and a couple of contacts. These two outer terminals are connected to the coil and they
form an electromagnet. This middle terminal is free to get attracted by the magnet. When
the magnet is active, then it will be pulled towards it and it will come in contact with
the bottom terminal. However, when the magnet is switched OFF, the central terminal will
get released and it will come in contact with the top terminal.
Electrically, a relay can be represented this way. When the magnet is OFF, the common terminal
will be connected to the terminal marked NC which stands for normally closed. When
the magnet is switched ON, then the common terminal will get connected to the terminal
marked NO which stands for normally open.
I will not talk about interfacing a relay here as I do not want to encourage you to
work with mains voltages if you are inexperienced. You also get off the shelf modules that can
be used which is much safer. Here’s an example of one such module and I have ordered a few
so that we can integrate it with home assistant.
I do hope this video has given you a good understanding of using GPIO pins to control
outputs and read inputs with home assistant. We will continue interfacing more devices
in future videos so do subscribe to stay updated. Thank you for watching and I will see you
in the next one.