If you had one superhero sense, what would it
be? Now Superman can shoot lasers out of his eyes, but can he turn on your lighting
automatically? Dr Horrible can create a freeze ray and turn things to ice, but
can he control his thermostat using Wi-Fi microcontrollers? And I'd say something about
Deadpool, but he's a Welsh fan, so he's perfect. This video is designed to introduce you to
some unbelievably powerful yet super cheap components that you can master to give you
superhero powers. If you want the powers of heat, motion, light, sound, air, moisture, load and
much more, then you really need to watch this. I want to give you a short, sharp introduction to
all of these sensors so that you get that tingle of excitement of what you could achieve. I'm not going
to be overly laborious, and this isn't meant to be a spoon-feed tutorial for each because there's masses
of tutorials and and guides already out there on all of these. These components are constantly
getting upgraded, so what I want to do is give you a sense of how accessible this stuff is now.
I want to give you the component names to then google. This is actually half the secret; once you
know that such a thing exists as a temperature sensor and it's called a BME280 (where are you my
little lovely?), then it's actually pretty easy to just google BME280 and get a detailed guide and
videos on how to do everything on whichever chip that you're using. I'll start off fairly slowly to
talk you through the process, and by the end I'll just be flying through at a high level as by then
you'll be able to think for yourself. I'll assume a few things: one that you have home assistance
set up. If you don't know what this is then watch my other videos. You really need to get it set
up. Secondly, you should have an ESP32 chip and at least one of these sensors because, well, that's
what this whole video is about. Other than that you just need to pay attention. Quiet down there at
the back. Now there are many variations of ESP32 chips. Some have cameras, others have LED screens on
them, and you can use these when you get a bit more experienced, but I'm going to use a basic one to
keep things simple. Let's start by getting the ESP chip running. Two things: first plug it in. That's it!
You've completed the first step. I'm being flippant, but I want to stress throughout this how easy this
stuff has become. This little chip is packed with useful components, it's Wi-Fi enabled, bluetooth
enabled, ultra-low power consumption, and it has a surprisingly nifty little processor. What this
means is that you've just created a standalone device that you can put anywhere in your home. If
you think what room you have a plug or USB socket in, well it's kind of everywhere isn't it? And
even if you wanted to attach a battery or solar panel, then the ultra-low power consumption makes
these very effective, and they can last a long time. So what we have now is a little microcontroller
that's running happily away, but it's doing nothing - the rough equivalent of a PC that you've built
and turned on but there's no operating system. So the next step is to install ESPHome. This again
is super simple. Visit this page (I've put this link in the description as well), then click ESPHome
and follow the simple instructions. This installs ESPHome as an add-on to Home Assistant. This is
a central hub for all your ESP chips. It doesn't just handle installing code the first time but it
enables Wi-Fi connections which then allow over-the-air updates. So you can have 20 devices in your house,
and if you change some of the code you can just click to install instantly to them all. You can
also write code and debug it, validate code, check the logs...really everything that you need. We've
now got a super powerful hub for our devices and a chip running away mindlessly. What we want to do
now is connect this chip to the hub and install a basic operating system to it. Once you've got your
cable connected you can just plug it in and click to 'add device' in ESPHome and it should take you
through the steps. Clicking connect should pop up the USB port selector and you can select your
device there, and then you're away! It steps you through the process of installing the initial
OS on the device, like naming your device. Once this is done your device will be connected to
your home Wi-Fi automatically, connected to Home Assistant and ready to rock forevermore. You can
disconnect it at this point if you want and all the updates you make will be done over the air.
So we have now, in a matter of minutes, set up ESPHome to act as a central hub for endless
devices and connected your first ESP32 and installed the basic software. We're basically done!
The next step is is about connecting up sensors, so let's go through eight of the most common to
show just how easy it is. Let's start heating things up with a temperature sensor. Wouldn't it
be cool - pardon the pun - if you could measure the temperature fluctuations around your house every
second of the day? Imagine being able to trigger devices to turn on and off based on temperature
thresholds. Well that's super cheap and super easy. Let's start with the venerable DHT22. If you google
each of the sensor names in this video you'll see just how cheap they often are. It'll shed new light
on the mark-up you pay in all these commercial devices. The DHT22 is a very simple temperature
sensor and a great place to start it has three pins power, ground and data. The first two just
power the device and the last is how the sensor readings are sent to ESPHome and Home Assistant.
Now to wire this up, you just need to connect each pin to the right place on the ESP32. It's pretty
simple. The power goes to the power, the ground goes to the ground and the data goes to one of
the data pins. There are loads of pins on each chip. This gives you the ability to create even
more complex sensors, like if you wanted a single chip that monitored temperature, light levels, noise
and more, well you just connect up to different pins for each data feed. But we'll keep
things simple for now. I've picked GPIO23 for my data - you need to remember that for your code later.
Almost all ESP32s have the pins written on the board, but you can also look up your model online
to see all the diagrams and guides. So coding time! This is where the penny may drop on just how
awesome ESPHome really is. It's already handled the vast bulk of the code for you, so you don't need
to worry about Wi-Fi configuration or bluetooth management, energy settings, encryption and all that
stuff. You just need to code the sensor and that is as simple as you could imagine. We write 'sensor'
to let it know that the sensor is attached, then we confirm the platform as DHT for this sensor as
it's a DHT22. All this is available on the ESPHome site, and they do a great job of giving sample
code for everything. Then we put in the pin that it's connected to. As I said earlier, I'm using
GPIO23. We put in the model number so it knows how to interact with it. This is all the code you need
to set up your sensor. Now, to create sensor values that get tracked in Home Assistant we just type
what what they are, so temperature with a name of 'Outdoor Temperature', and we can also add humidity
which this sensor tracks in the same way. Now, I'm keeping this code super simple, but one more
thing we'll add is the update interval. This is handy as it naturally will affect the amount
of data that flows into Home Assistant and the energy use of the device. That's it! You've created
your first sensor. You now just click to install it and it'll do all the hard work for you. If
in future you wanted to change the frequency, name, sensors or anything, you can just edit it
from here and broadcast the new code over the air. Cool eh? Let's quickly jump into Home Assistant
and you'll see under 'Integrations' the new sensor is already appearing. Clicking on it and you can
see the values coming through. How exciting! And finally, let's jump into a dashboard page, create
a graph, and bang! We've got a detailed sensor that tracks the temperature and humidity constantly. And
how easy was that? We can use this sensor easily to trigger anything we want - change light colours, play
sounds, turn on air conditioning or simply gather the data to build your knowledge of temperature
fluctuations. Final point: I'm deliberately picking the cheap components for each of these demos to make
it as accessible as possible. There are definitely better sensors out there with improved accuracy.
The temperature accuracy of the DHT22 is fine for general sensing, and the humidity is actually
a proxy sensor by inverting the temperature. For most use cases, I'd actually recommend the BME280
as its much higher accuracy is really handy. There's also the newer BME680 which is even
better, but naturally the cost goes up a little. each time, so just pick what suits your use case.
You now have the power to install a temperature sensor. But, my little acolyte, I can see you crave
more. So let's get moving with motion sensors. I'll assume you know now how to set up the ESP32 chip,
and we can just jump directly to the wiring and the coding. This is the HC-SR04. They're again very
cheap, and it works by sending ultrasonic waves out and then measuring the response times.
Because there's two of them, they can work much like two eyes do and judge distances by the
difference in response times. The wiring is very simple, coding is very similar - again we just have
a trigger pin to send out the pulse and an echo pin to read the waves bouncing back. Tther than
that it's simple, eh? You'll already know what the update interval does. This is worth considering
carefully for your use case. If you want a motion sensor for a room, you might actually want it to be
quite slow so it's not hammering back readings all the time and triggering on and off. If you want
something like my hands-free light and music volume controller, well then you might want it to be much
faster to allow a smooth control of light levels. This is where mmWave sensors come in. They're
very similar, but their sensitivity is exceptional - sub-millimetre, as the name suggests. So they will
detect you in the room as long as you're breathing. And if you're not, well it might be a fitting end
for your smart home to fade the lighting and music in time for you leaving this mortal coil. [Robotic voice: Katie has been deactivated. Oh this is quite dramatic]. Let's now grant you the power of light with lighting
sensors. For this we'll use a BH1750, which is a little sensor that happily detects light levels.
You're becoming an ESP pro now, so you know what to do. The code is very similar. Just define the name
that you want for your sensor, the update interval, and that's it! There's an option value for the
address that you can set if there are any issues. By default it sets it to 0x23, but if you have any
problems just add this and set to 0x5C and you'll be sorted. Again all this is covered in brilliant
detail on the ESPHome site. Save, compile and publish your code. And we now have a light sensor
that you can use to do all sorts of cool things. Again, there's loads of even better sensors - the
APDS9960 recognises colours, so you can have it trigger a different action based on the colour of
the room. I'm not done with you yet. I want to hear you squeal with excitement...so I can measure it
with my sound sensor. Let's use the KY-038, a nifty little sensor that measures sound. I don't even
need to tell you now what to do. You know the drill, and the code is very familiar. Only thing to call
out is the attenuation variable. Without getting too nerdy, because the ADC or the Analog-to-Digital
converter on the ESP chip is sensitive to the input voltage, you need to cater for this. As you've
got a 3.3 volt input, you just need to enter 11 DB, and that's it. And as you getting a little more
comfortable I've added some filters to the code. These basically let it smooth out noise levels. If
you imagine a normal sound wave of, say, you talking it's very noisy waves as it's literally noise. And
let's say if you were in a dungeon, then the human ear would consider it noisy, but this sensor would
more accurately describe it as quiet punctuated by the sound of whips and screams....until final....silence.
So to make it more useful, you want to average it out a little to give you a general level of noise.
That's it! Now you have a sensor in your house that monitors the sound levels. You can also use it as
a simple burglar detection for everyone...except ninjas. Now, I can hear you panting with anticipation, but
is the air you're breathing clean or dirty? Now let's think about an air particulate sensor - things
like the HM3301. This has a little fan that sucks in air, blows across a sensor and detects PM1
2.5 and 10. The code is super simple, the wiring is super simple, and you're sorted. I will paste the
code in the description for each of these. You've now got a sensor that detects dust levels to let
you know when you need to hoover or if you suffer from hay fever. if you have a workshop, it can alert
you to dust levels being dangerous. There's so many cool uses of these sensors, but most importantly it
gives you the data about your environment so you can understand it better. Okay, hopefully that's got
the juices flowing, so why don't we measure them. A moisture sensor. Imagine every plant in your house
being able to scream at you when it needs watering or a dashboard that shows all their moisture
levels, the temperature in the rooms and the humidity levels. To do this you just need one of
these dudes, called a capacitive soil moisture sensor. There are loads of types, but I'd go with
a capacitive sensor as they're less prone to corrosion than the resistive types, and naturally
as they're in soil and water it'll be getting a little bit exposed to that. The code is simple -
you just need to name the sensor and pins, as always, and set the attenuation for your device.
You'll notice two filters: first the calibration. As the device doesn't know what you're going to
use it for, you need to set the values for what wet and dry means. If this was measuring whether
your pond was drying up then wet would be very wet as it would be completely immersed in water.
If it was measuring soil moisture, well, then wet would be a much lower value. You can naturally set
these values in home assistant too, but it's handy to control them from the device itself sometimes.
You can test the values by checking the logs in ESPHome as you use the device, or just use Home
Assistant to read state values for the sensor. I've also added a median filter as another
example of how you can filter out noise. This basically takes a median size of three readings
and will send the new median every time it takes a reading, thus smoothing the results. And that's it!
You can now measure moisture super easily. But let's talk about one more example, where you start
to get components like b-parasite. This is where you start to see that some options combine things.
This nifty little open-source approach monitors moisture temperature, humidity and light levels.
And it all gets powered by a little battery that can last for years, so you could stick a bunch of
these in your garden beds and have unbelievable details of how your plants are doing all in one.
Okay the HX711. This is a load sensor. The code is super simple and just needs a bit of calibration.
So you'll need a precise weight, say put a 1 kg weight on it, and if it reads 810 then put that
number into the filter. This can be used for lots of things. Obviously a scale is handy. Again you
can get even more precise devices based on your use case, but you could easily have a device that
monitors the amount of coffee beans left in your pot or sense when someone steps on your doormat,
whether someone is on a chair or trigger when a precious jewel is lifted from its place, and so
on. MPR121 capacitive touch sensor. These detect your touch, just like your phone screen. Differing
models have different ranges, but it's pretty cool to create a button on a material that senses
through it, a device that triggers when you tap the material in different places. And finally you'll
very quickly see how you can start to put these sensors together into combined all-in-one units. As
you add more, inevitably it gets more complicated, but it's all achievable. Companies like Apollo
Automation are great examples - love their stuff! So if you want a quick off-the-shelf solution
with an absolute battery of sensors already available, go for it. You may think that this is
all now easy, and in many ways it is, but there are three tips I'd like to share to save you hours
of headbanging. Things will of course not work some sometimes, but it's amazing how many of them will
be down to one of these three reasons. Number one: connect directly to the device running ESP home if
you're struggling. This takes out lots of potential issues with your home network setup, device
authentication and more. Number two: check your USB cable. I know you're sure it's a data cable, and
it's definitely working. But you'll be amazed at how often this is the cause of problems. It's
that chaos of USB standards again. Top tip: get one of these. Just plug the cable in two places and
bang! It tells you exactly what cable you've got. Number three: look at the data sheet for your exact
microcontroller and sensor. Just google the name of it. Again, you'll be amazed at how often something
changes with a new model, and that's the source of your problems, and you just need to change the
data pin you're using or something similar. So that's it. You now have the power, but promise me
you'll only use your powers for good. I hope this video has helped demystify some of these chips and
sensors and you're able to fly off into the world to make great things. I'd like to thank the real
superheroes of this video who are soaring past my head: my Patreons and YouTube members. If you'd like
to see more videos then think about joining. Links in the description as always. Oh, and many thanks
to all of the wonderful developers of ESPHome who do such a great job in helping make the world
of microelectronics accessible to everyone. Hwyl Fawr. [Music] uh