@byron Happy to see you are still with us. I am so embarrassed, today, I actually read your post that started this that I did not see before. It, as you know, changed a major design point in that you wanted a solution to turn the power off, not just sleep. I was assuming the interrupt was being dealt with by the PICO. DUH, totally different scenario. I think I might go that route as well if I can get a cost-effective PICO+Camera package or even an ESP32+Camera package. I will assume subject to testing that turning off the power on the ESP32 etc will work as well as the PICO.
Sooooo what I am building up to is may I use the design that you and @davee came up with, I don't want to step on any more toes, I think I am over my quota.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@byron I forgot the other part. Yes AFAIK the C code was affected with regard to deep sleep instability. Yes, I know the PICO needs to tidy up the RTC after waking up, in my case, it will probably be a bit more complex due to different wake cycles at different times of the day by date (sunrise/sunset for instance)
Just an FYI, I am pretty sure the concept of 'power down' is not really something the RTC does AFAIK, it is just the absence of I2C signalling that allows the battery to drop to micro Amp territory. At least that is my understanding from everything I have read, if you know of a reason that is not true, please share with me your source as that may have implications for my design.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@byron My understanding is that the C code is not stable.
For the Power Off project, how do you turn off the PICO?
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Hi Ron @zander,
Power Off project, how do you turn off the PICO?
In my C test code, just 1 simple call
rtc.clearAlarm(1);
which (by reputation, as I haven't personally audited the code) resets the Alarm bit in the appropriate DS3231 register, and hence automatically sets the INT/SQW pin back to high.
Hence, the FET is turned off, and the power is removed from the Pico.
I presume @byron's Micropython code will do the same.
Best wishes,
Dave
@davee I will try to test that if I am feeling well enough, but I seriously doubt it. That pin when it signals an interrupt is LOW for 100ms IIRC.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Hi Ron @zander ,
That pin when it signals an interrupt is LOW for 100ms IIRC.
No ... when the time alarm triggers, it stays triggered until an I2C message resets it ....
From https://www.analog.com/media/en/technical-documentation/data-sheets/ds3231.pdf
The description in the "Pin Description" page 9 is misleading, as it is incomplete, but
Well hidden on Page 14 of the data sheet ...
Bit 1: Alarm 2 Flag (A2F). A logic 1 in the alarm 2 flag bit
indicates that the time matched the alarm 2 registers. If the
A2IE bit is logic 1 and the INTCN bit is set to logic 1, the
INT/SQW pin is also asserted. A2F is cleared when written
to logic 0. This bit can only be written to logic 0. Attempting
to write to logic 1 leaves the value unchanged.Bit 0: Alarm 1 Flag (A1F). A logic 1 in the alarm 1 flag bit
indicates that the time matched the alarm 1 registers. If the
A1IE bit is logic 1 and the INTCN bit is set to logic 1, the
INT/SQW pin is also asserted. A1F is cleared when written
to logic 0. This bit can only be written to logic 0. Attempting
to write to logic 1 leaves the value unchanged
And I have had my Pico running with the described code ... it takes (very) roughly 5 seconds to go from DS3231 alarm tripping and power on through to power off.
Hope you are feeling well, regardless of whether you test it!
Best wishes, Dave
@davee Ok, I must have read some fake news. That makes for a solid and controllable way to shut the PICO down. In all the examples I looked at, they cleared the alarm almost always first thing after detecting the interrupt, but with how you are using it, it becomes the equivalent of the exit statement. Cool! I might set this up with one of my new genuine NANOs. I still need to figure out the exact steps to loading a sketch on a PICO. I thought I did and even told others, but somewhere between my confusion and maybe a broken PICO, it's a random chance to get a sketch uploaded.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Hi Ron @zander,
Good luck with your trials.
I admit to finding dowloading code to the Pico somewhat dependent on luck .. most of the time it is fine, but occasionally it wants to be a 'mass storage device', and I have yet to see the button on the board make any difference - I used Ubuntu as the Arduino IDE host for the trial. Usually, it was a case of checking/resetting the board selection and port settings in the Arduino IDE ... there are two 'apparent' port options, but only one that works, and it has habit of jumping to the other one, or maybe neither.
Maybe 3 times I had to swap to Windows, download a trivial program to the Pico, then return to Ubuntu to download the code I wanted.
I am sure there is a better way, but as this is the first time I had taken the Pico out of its bag since I rashly added it to an an order a long time ago, and I only used this time to (nearly) match @byron's intentions, so I am avoiding the sidetrack of trying to make it 100% reliable. It is 'good enough' at the moment.
Best wishes,
Dave
may I use the design that you and @davee came up with
The design is wholly Daves', the only claim to fame for me is I tested the circuit which works as described. So if you send a suitable fee to Dave he may let you use it. 😎 (and I can confirm the alarm trigger of the int/sqw pin grounds the gate pin on the FET thus switching the power on, and it remains grounded keeping the power flowing to the pico until an 'alarm clear' instruction is sent to the DS3231 via i2c, which then removes power from the pico)
For programming the pico in C probably the best way is to follow the instructions in the C / C++ SDK and download the necessary to compile your C code, and not to use the Arduino environment. For a C editor then Visual Studio Code is a good one to pick if you don't have a preferred one already. Here is a youtube link to someone showing how to set it all up, but its all in the SDK manual.
In essence you compile your C code to a .uf2 file which can then be copied to the pico. You can bring up the pico's file system into your computers file system by holding down the boot button whilst power is applied to the pico. A bit cack handed if plugging the power into the pico via its usb port, but easy if you have a usb hub with power on off switches by each port. This is the same as loading a micropython .uf2 file, which is, of course, just a compiled C program the same as the C programs you may produce.
@byron At one time I was quite adept at uploading to a PICO, but I have now forgotten. I find the Visual Studio IDE to be a bit busy so I think I will stick with Arduino. I have uploaded a few C sketches with the Arduino IDE and I have never used the make based C SDK stuff although I do have it and will likely need it to do some things. I think I have 3 or 4 different RP2040 board descriptions for the Arduino IDE so some work and some don't as they are for specific boards that have RP2040 chips but on a different board. When I get more involved with the PICO I will get it sorted. I will go hat in hand to sir Dave for his most generous and forgiving approval for me to 'borrow' his design even though I found a similar design by accident last night. Better to 'steal' from friends than strangers LOL!
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Hi Ron @zander,
The mini-project was inspired by @byron's question.
Of course you can use anything I create, albeit at your own risk, and whilst I normally try to credit/reference external sources, I make no claims or offer any assurance that others do not have patents, trademarks and goodness knows what other prior claims. 😊
The circuit was my 'creation', albeit it is really nothing more than a demonstration of how a p-channel works.
Having established the nature of the INT/SQW output, both in terms of electrical (open drain) output and its 'alarm fires when times match -- reset via the I2C bus' operation mode, the principle of making the software do 90% of the work, seemed obvious. Given a world population of say 8 billion, I would expect lots of others to have followed a similar path!
Whilst you may well get away with using "input pullup" resistors of the microcontroller for the I2C bus, the saving of maybe 2 cents/pence is anything but smart ... the waveforms can and should be 'flat topped' square waves, but using the "input pullups" they are more like shark's teeth ... and one day they might just bite!
If you build your own board, don't forget the decoupling capacitors and clip for the coin battery.
-------------
I used Arduino 2.0.x for all my development, but that is simply because it used least effort.
Enjoy. I hope you persuade it to do what you want.
Best wishes, Dave
@davee Thanks, Dave. After all the dust settled and I could focus and learn your circuit, I realized I had seen something like it before. I am sure you will get a chuckle out of this. If you recall, @will had found a latching circuit that allowed the turning on and staying on until the MCU/MPU turned off the power. As you no doubt remember, the flaw in that design was the use of a physical switch. In a fit of freshly learned MOSFET education, I quickly realized the switch could be replaced by a P channel MOSFET which I had just learned about. I am sure you know where I am going. Yes, I planned to use the INT pin of the DS3231 to trigger the MOSFET, and because I thought the INT pin was a pulse was content with the knowledge the latching circuit held power on. I am not at all sure I would have simplified that circuit if I knew the INT pin stayed LOW, but I am very glad you did. It's been a while since I was the new guy arguing with the pro, and I don't like it one bit! I am thankful that you were totally professional and patient despite my foolish rants. I really hope I learn from this, but at 81, it's hard to change. Maybe anytime I start to do that, if everyone would just say, Ron, remember POSC, I will hopefully recall this valuable lesson.
I just installed the PC-based Oscilloscope so I can observe the I2C signals and the INT as well I assume. I had to buy a mini windows PC to use with the scope, so you can see the depth of my commitment to doing things right, none of those things run on a Mac.
I did just buy some coin battery holders, but can't find them. They will show up. I am only vaguely familiar with the idea of decoupling capacitors, can you tell me the type, size, etc and where in the circuit they go?
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
My breadboarded circuit has been powering up and down and blinking away for a few days now, and I'm putting it aside for now as I've other things to get on with, but its a very nice little project to have available when I need it, and it was a very good education for me. Thanks to @davee I learn those little FET's are very useful things to know about.
As I tidy up my test programs to save in a safe place for later use I thought I may as well put up this diagram that I made for myself from Dave's circuit diagram as a check on what I was going to put on the breadboard and to double check I was not missing anything. For a test I left out the switches, so be careful not to power the pico via its usb and the battery pack at the same time if going the same route. (I realise I don't actually know what would result, but I don't imaging it would be good 😎)
I created two programs, they are in micropython, but they are short and sweet and as its python they are readily read as sudo code for creating arduino code. No doubt in place of the ds3231 library I used an arduino lib will be readily found. (and who knows, an intrepid reader my actually like to program in micropython)
First up is the ds3231 alarm set up program, to be run when the pico powered and attached to an IDE via its usb port:
# Uses the ds3231_gen_test.py library # Author: Peter Hinch # Copyright Peter Hinch 2023 Released under the MIT license. # Program to set up an alarm on the DS3231 from machine import I2C, Pin from ds3231_gen import DS3231, EVERY_MINUTE import time # Set up i2c i2c = I2C(0,scl=Pin(1, Pin.OPEN_DRAIN, value=1), sda=Pin(0, Pin.OPEN_DRAIN, value=1)) # instantiate DS3231 class rtc = DS3231(i2c) # (1) set DS3231 RTC with current time # assumes pico has the current time (can get current time if being programmed with Thonny) dt = time.localtime() rtc.set_time(dt) print('DS3231 RTC time set to ',dt) # (2) use DS3231 alarm1 which can trigger on seconds passed the minute alarm = rtc.alarm1 # (3) the seconds passed the minute when alarm should trigger alarm_at = 0 # set the alarm, then ensure alarm is cleared ready for the next alarm trigger point. alarm.set(EVERY_MINUTE, sec=alarm_at) alarm.clear() print('alarm1 set to ',alarm_at, ' seconds passed the minute')
Next is the program set to auto run when the pico is powered up when an alarm is triggered to run a short blinky function to show when the pico is alive as triggered by the alarm setting in the previous script.
# Uses the ds3231_gen_test.py library # Author: Peter Hinch # Copyright Peter Hinch 2023 Released under the MIT license. from machine import I2C, Pin from ds3231_gen import DS3231, EVERY_MINUTE import time import uasyncio as asyncio """ program to run to test davee power trigger on alarm circuit progam to set up DS3231 is ds3231_setup.py this program will be triggerd every x seconds passed the minute as set, but will then increment the seconds passed the minute to 13, 30, 45, 0 for each subsequent alarm The program will blink the on board LED each time the alarm wakes the pico. """ # set the LED pin for the picoW LED = Pin("LED", Pin.OUT) # pico i2c pins and i2c bus i2c = I2C(0,scl=Pin(1, Pin.OPEN_DRAIN, value=1), sda=Pin(0, Pin.OPEN_DRAIN, value=1)) # instanciate the DS3231 class (as found in ds3231_gen.py) rtc = DS3231(i2c) # alarms to use (alarm1 or alarm2 or both) alarm = rtc.alarm1 # return the local time in H:M:S from the RTC on the DS3231 def str_time(): t = rtc.get_time() return '{:02d}:{:02d}:{:02d}'.format(t[3], t[4], t[5]) # return the current minutes passed the hour / future use def get_rtc_minutes(): t = rtc.get_time() return t[4] # return the current seconds passed the minute def get_rtc_seconds(): t = rtc.get_time() return t[5] # function to blink the LED (2 blinks) def blink(): for _ in range(4): LED.toggle() time.sleep(1) # function to return the next alarm sequence (seonds passed the hour) def next_alarm_seconds(): secs = get_rtc_seconds() print('current rtc seconds are ', secs) if secs >= 45: return 0 elif secs >=30: return 45 elif secs >= 15: return 30 else: return 15 # Notes for full test: # program for minutes passed the hour EVERY_HOUR (not seconds passed the minute) # - connect to wifi # - connect to MQTT broker # - publish MQTT message: # - the temperature reading from the DS3231 # - the current DS3231 time # For a mini test run a blink function. blink() # Reset the alarm time for the next 15 minute section new_secs = next_alarm_seconds() alarm.set(EVERY_MINUTE, sec=new_secs) # Clear the alarm to remove power from pico alarm.clear()
For full understanding then you must read the full detailed description as provided by Dave, this post is just a short example of what I did to as a precursor to getting a breadboard test wired up by creating a simple diagram to check against Dave's schematic.
If you use the Chinese DS3231 board (as modified to remove unwanted resistors) then it plugs into the breadboard, the pico plugs in, if you manage to get the specified FET soldered onto a carrier board, then it also plugs in. Then a few jumper wires later and in half an hour you should have a very nice power on circuit to play with. (ok you may get some magic smoke, don't blame me for your crossed wires)
@byron FYI @davee Very good. Glad it is working for you. I am trying to use my new scope to look at the signals, and so far, I don't see what I expect, but since I am new to all this, I assume I am doing something wrong. Designing a board (it will not be a PCB, I don't know how) is a big challenge. Poor sight and fat fingers that can't feel things so good either makes for a challenge, plus my general spatial relationships problem. I will do what I always do, waste a pad of paper or more doing drawing after drawing until it is optimal. I do have one advantage, though, I have both Chronodot's and raw DS3231 chips so that saves some time and space. On the plus side, I also have a handful of very tiny DPDT switches I ordered by mistake that will now have a use.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@davee I am trying to display the INT pin on my scope as a scope exercise before moving on to the I2C. I haven't used a scope in over 60 years. I have a 5 sec delay from when I detect the interrupt to when I reset the pin so I should see a 5 sec pulse but it appears to be a saw tooth of very brief duration. I have no idea which of the myriad settings need to be set. I did display a 5V 1kHz square wave with no difficuty using the test output on the scope. Any thoughts?
EDIT I now see the line across the scope drop to 0 for the proper time. Now trying SDA/SCL and don't see much but some noise.
EDIT2 After changing some random stuff, I get this image, it was a sawtooth before I used the 4k7 pullup.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.