Creating battery-powered sensors in a smart home
At first, I tried to make a device that works online, powered by 5 volts, but it quickly became clear that it was much more convenient to use battery-powered devices, of course there were quite a few devices on the market that could be bought, or they were very expensive, and I had to study what could be used to make these devices alternatively, in particular basically, I ended up using the attiny 85 microcontroller, there is a special version, which allows you to work with low consumption in sleep mode, this is the 20u version and on this version the sleep consumption of this microcontroller is only 4 microamps, so in the operating mode of this microcontroller there are about 4 milliamps and this is of course, for example, if I use a CR2450 battery, I chose it because it is bigger, it has a higher rated current, which it can give out, and this microcontroller can work in standby mode for 17 years, and if it works in continuous mode, it will certainly work for about 6 days, based on the fact that the CR 2450 battery has a capacity of 610 milliamps per hour.
In order for this microcontroller to constantly fall asleep, a special Watchdog mode is used, this is a timer that allows you to wake up the microcontroller with some frequency, for example, on this microcontroller it is possible to adjust it to different ranges from 100 milliseconds to 8 seconds, and it turns out that the microcontroller, when it has performed some action, puts itself into sleep mode, and next is this timer, it works in parallel, it is powered separately and, accordingly, it consumes very little energy, actually these four microamps, and then when this condition is triggered, after these 8 seconds, it wakes up the microcontroller. It wakes up quickly enough, basically turns on at the same place where it stopped, and then the program continues to run while saving all the data that was accumulated on it at the time.
To ensure the operation of this sensor for a long time, for this microcontroller, of course, it is necessary that it sleeps as long as possible and works as little as possible, this allows it to be done in such a way that the operation cycle itself should be very short. Of course, you can do this, for example, if you use some sensors to poll, for example, once a minute, this is done in such a way that when the timer wakes up, for example, every 8 seconds, it checks if 60 seconds have passed in total, and if they have passed, then you can perform some action, if they have not passed, then the microcontroller immediately falls asleep again and practically does not it takes time, but some sensors that I use, such as temperature sensors, they work using the i2c protocol, this protocol is serial exchange, and it takes some time to read the value from this sensor, that is, to send special sets of pulses, the sensor processes and returns the value quite quickly, but it can even last up to 50 milliseconds and significantly affects battery consumption.
Or, for example, if I use the sensor as a water meter, then the reed switch on the meter itself can be triggered quite often, and if, for example, the microcontroller wakes up only once every 8 seconds, it may skip this action and, accordingly, not calculate this flow rate, so there are different meters that count by liter, there are those that count by 10 liters, but basically, of course, everyone puts them that count 10 liters per pulse cycle, that is, one turn on and off, that is, it turns out that turning on or off is 5 liters, that is, if there is a consumption, then of course it may turn out that in 8 seconds these 5 liters were spent and the microcontroller did not see a change in the status of this reed switch, you have to make sure that the microcontroller has to wake up more often, it can be several milliseconds, say 100 or 200, or 500, or it can be several seconds, for example one or two, or four, I picked it up experimentally, you need to understand, for example, what the expense is if, for example, all the taps are turned on at the same time, then have time to detect a change in status at that moment, but the more often he wakes up, the more time he gets in total and the more battery consumption he gets.
Of course, I'm trying to keep a ratio of about 1000, in this mode it allows you to work for about 500 days, in general, I made this method of measuring the operation of the microcontroller to understand how much it is in active mode, how much in sleep mode, then I just, for example, have a millis timer on the microcontroller, a command that allows you to see how long the controller is in principle, he worked in active mode, if he falls asleep, then this timer stops, and it is clear that you need to understand in some other way how time goes when the microcontroller is asleep, and in this case, I just know which timer I have set, and accordingly, if I set it for 8 seconds, then I add 8 seconds to the value of the parameter that saves sleep time, and thus I understand how much I was in total and in what mode.
Of course, if you use a different battery, for example, in general, this microcontroller allows a fairly large range of battery operation, it can work from five and a half volts maximum, up to about 2.8 volts, then it works unstable, but it also depends on what frequency is used on the microcontroller, I used 8 megahertz, this is practically the factory setting and of course, I usually use three-volt batteries, but in principle you can also use 5 Volts, for example, it can be powered by some kind of USB charger, or for example it can be three AA finger batteries, then they will give four and a half volts together, but this is at face value, of course, the new batteries give more and there is of course a possibility that it will even go beyond 5 and a half volts, also for some sensors I use power from a lithium-ion battery, there is a base voltage of 3.8 volts, but when it was just charged, they usually show 4.2 volts, which is quite normal for a microcontroller, and of course it can work for a very long time from such a battery, and no large currents, for example, which are required to transmit information, have practically no effect on the voltage.
Even on the microcontroller, it is possible to measure the supply voltage, there is a built-in such function, if, for example, I measure when powered by a battery, then at this moment, of course, the controller is on and the voltage is usually greatly underestimated, unlike if it is measured, say, in the sleep mode of the microcontroller, or just without load, to measure the voltage on the battery, but let's say from powered by thumb or pinky batteries, or from a lithium-ion battery, there is practically no such subsidence, and based on this information, of course, you can also transfer the smart home system, and monitor the battery status.
Of course, you need to be careful with lithium-ion batteries, because they are very bad at over-discharging, but such a controller is basically incapable of actually charging this battery, unless it works in the always-on mode.
This is the simplest microcontroller, of course, there is also the Attiny 13, which has smaller characteristics, and less built-in memory and performance, but I have never used it practically, because there is absolutely no point, the Attiny 85 is not much more expensive, about 100 rubles, sensors for it, for example, a temperature sensor costs about 200 rubles, this if it's a good sensor, if it's the simplest sensor, then it can cost about 50 rubles, these temperature and humidity sensors go at the same time, which is quite convenient in principle.
In general, the cost of the final device is quite low, that is, about 500 rubles, but of course you can find branded devices such as Xiaomi or Tuya, which will also work either on batteries or even on CR2032, and about the same time, of course they have a pretty beautiful case already and they are generally much better made, but the advantage of a homemade device is that you can connect, for example, another light sensor and measure the illumination in parallel with a light sensor, this is a simple photoresistor for 2 rubles that you can buy, and of course, the functionality of the sensor is greatly expanded.
Automation in a smart home should be useful and create additional comfort, but sometimes everything goes wrong… The reasons for this, and how to configure everything, see on my channel.