![]() When the button connected to GPIO4 is pressed (the bottom button), the RTC’s datetime is set to 2 PM on.Initialize the RTC with an IRQ to trigger on ALARM0 to wake the ESP8266 from deep sleep.Initialize two buttons on GPIO4 and GPIO14 as inputs with pull up resistors.Initialize the LED on GPIO5 as an output.Below is an itemized list of what the code does: Press the restart button on the NodeMCU and then immediately plug the wire going from GPIO16 into the pin labeled RST on the breadboard before doing anything else. After understanding the code, copy it into the Thonny editor, and save it to your device as “main.py”. This code demonstrates how the RTC continues to run, even in deep sleep mode. I encourage you to go through the following code line-by-line so that you understand what is happening. A signal from GPIO16 to the RST button is what restarts the ESP8266. For this reason, the jumper from GPIO16 needs to be connected to the RST pin on the NodeMCU after pressing the restart button an initial time to run the code. ![]() Waking the ESP8266 in this way is equivalent to pressing the restart button on the NodeMCU. This simply imports the machine module so that deepsleep can be called. To prepare your device for deep sleep mode, upload the following code to your device as “main.py” and press the restart button on the NodeMCU to run it. This results in power consumption being drastically reduced. In deep sleep mode, the only part of the ESP8266 that uses power is the RTC. In this section, we will go over the mode that uses the least power, “deep sleep” mode. The ESP8266 has several power saving modes that could be useful for an application like this. In the future, you may have an application for your ESP8266 that requires it to be run off of a battery for a long time. We will come back to this in a future guide on I2C. A common one, the DS3231, uses and I2C bus to communicate. ![]() We will go over using ntptime in an upcoming guide.Īlternatively an external RTC could be used. The RTC can be used in combination with the ntptime module to periodically pull an accurate time from a time server to maintain a more accurate time. In my personal use, I have noticed that after running the RTC for a day, it will end up being several minutes off from the actual time. The RTC built-in to the ESP8266 along with the MicroPython implementation is far from a perfect timekeeping method. You do not need to worry about setting the exact day of the week. If you followed along and set the RTC to you should also see that the 4th element, the day of the week, has changed from 0 (Monday) to 2 (Wednesday). ![]() You should see that the time has changed depending on how long you waited before calling the function. Press the restart button on the NodeMCU to run this code. Day of the week: 0 (Automatically calculated from other data.)Įnter the following code into the Thonny editor and save it to your NodeMCU as “main.py.The following elements for the tuple parameter indicate how to set the time to 2:00:00 PM on the 19th of February, 2020: The second line sets the time using a tuple containing elements in this order: year, month, day, day of the week, hour, minute, second, millisecond. The first line creates a RTC object and sets it to a variable. Not including the import, initializing the RTC and setting the time manually takes two lines of MicroPython code, shown below. In this section we will go over how to set the RTC’s time, check the time, and some of the limitations of using the built-in RTC with MicroPython. It achieves this through the use of a crystal oscillator. Two buttons, an LED, and a jumper wire connected to GPIO16 (D0).Ī RTC, or Real Time Clock, is a circuit that keeps track of real world time (years, months, days, hours, seconds, etc). Lastly, connect one end of a jumper wire to GPIO16 (D0) and connect the other end to an empty breadboard row. Connect the other side of both of these buttons to the GND rail. Connect one button to GPIO4 (D2) and the second button to GPIO14 (D5). Connect an LED to GPIO5 in series with the resistor going to the GND rail. Timers and Interrupts with a NodeMCU and MicroPythonĬomputer running Windows, Linux, or MacOSXĬonnect one of the 3V3 pins on the NodeMCU to a breadboard rail and one of the GND pins on the NodeMCU to another rail.Basic GPIO Input and Output with a NodeMCU and MicroPython.
0 Comments
Leave a Reply. |