# Sample code for TPL5010

**URL:** <https://community.particle.io/t/sample-code-for-tpl5010/41813>\
**Category:** Troubleshooting\
**Created:** [May 13, 2018, 8:16pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813 "2018-05-13T20:16:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![not103](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@not103](https://community.particle.io/u/not103)\
**Post date:** [May 13, 2018, 8:16pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/1 "2018-05-13T20:16:56Z")

</div>

Does anybody has a test code for such watchdog ic? I am trying it with Arduino but it seems it’s not working well…

Any suggestion or link is appreciated 😉

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 13, 2018, 8:55pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/2 "2018-05-13T20:55:34Z")

</div>

There really isn’t so much code. I use the TPL5000, but it’s similar. Connect a GPIO pin to the DONE line. Use pinMode(pin, OUTPUT) in setup. In loop, I usually just flip it to the opposite state each time through using digitalWrite(pin, value).

---

<div class="post-metadata">

**Author:** ![chipmc](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/chipmc/32/15905_2.png) [@chipmc](https://community.particle.io/u/chipmc)\
**Post date:** [May 14, 2018, 12:53pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/3 "2018-05-14T12:53:24Z")

</div>

@not103,

I agree with @rickkas7 in that the code can be mammal but I have taught mine a few tricks.

I use a TPL5010 in my outdoor projects. I have uploaded the project details and code here:  
[Electron for Outdoor Applications](https://www.hackster.io/chipmc/particle-electron-carrier-for-outdoor-iot-applications-40ed52)  
Photon - [Smart and Safe Outdoor Watering System - New for 2018](https://www.hackster.io/chipmc/smart-and-safe-outdoor-plant-watering-system-new-for-2018-17d5c8)

The long and short of it is this:

- Pick a period that makes sense. Short enough that you reset within a reasonable time, long enough that you can get critical things done and take time to sleep if you are on a battery.
- Use an interrupt service routine to “pet” the watchdog so it can be services no matter what your code is doing.
- Don’t use blocking code like delay() unless it is very short
- In the ISR, I add a petting enabled logic. That means that I can disable petting the watchdog if I am doing something important. For example, when I am watering using the AquaMaster, I disable petting to reduce the chance that the device will lock up with the water turned on. For this to work, the watchdog interval needs to be slightly longer than the longest watering period. I also need to make sure I pet the watchdog manually just before turning on the water to ensure the timing is right.

Hope this helps,

Chip

---

<div class="post-metadata">

**Author:** ![not103](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@not103](https://community.particle.io/u/not103)\
**Post date:** [May 14, 2018, 1:17pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/4 "2018-05-14T13:17:07Z")

</div>

Basically my code take a measurement every 10 minutes at 0-10-20-30-40-50 during day and every hour during night.  
I would like to set the watchdog at 1-1.5 hours and to keep all minimal I like also the idea to simply change the state of the done pin at every loop, Ignoring the wake pin.

The purpose is to reset the board if for some reason (it happens) the Electron stop works.

Does it make sense?

---

<div class="post-metadata">

**Author:** ![chipmc](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/chipmc/32/15905_2.png) [@chipmc](https://community.particle.io/u/chipmc)\
**Post date:** [May 14, 2018, 1:42pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/5 "2018-05-14T13:42:34Z")

</div>

@not103,

Sure, if you are OK with the possibility of a 1-1.5 hour lockup, this is the simplest method. In fact, you might simply look at the [Application Watchdog](https://docs.particle.io/reference/firmware/photon/#application-watchdog) which is part of the ARM processor as adding a separate timer may be overkill for you.

Chip

---

<div class="post-metadata">

**Author:** ![not103](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@not103](https://community.particle.io/u/not103)\
**Post date:** [May 14, 2018, 1:59pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/6 "2018-05-14T13:59:48Z")

</div>

It seems for some reason the application watchdog is not enough, it can handle most of connection problems but there is something wrong that put the device in sleep forever… (or it seems it’s in such state).

I’ve found a strange coincidence of this problem and thunderstorm.

Only a manual reset or cold start resolve this state, for this reason I would try the external watchdog.

---

<div class="post-metadata">

**Author:** ![not103](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@not103](https://community.particle.io/u/not103)\
**Post date:** [May 14, 2018, 8:05pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/7 "2018-05-14T20:05:32Z")

</div>

Another question, what would be the best way to power the TPL5010?

1. direct from battery
2. from 3V3 pin on Electron
3. from VIN pin on Electron
4. fromVBAT pin on Electron

assuming I’m using SLEEP\_MODE\_DEEP

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 14, 2018, 8:30pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/8 "2018-05-14T20:30:18Z")

</div>

3V3 is fine. It’s powered all the time. 3V3 powered by VIN, USB, or battery. It’s usually the best choice.

Direct from battery (Li+) won’t work if not powered by battery. Also, it will drain the battery when charging from USB or VIN, which is different than 3V3 which bypass the battery when external power is used.

VIN is not great, as it is only powered when you’re powered by USB, not battery, unless you are powering by VIN from an external source.

VBAT is connected to 3V3 with a zero ohm resistor, but you should just use 3V3 instead.

---

<div class="post-metadata">

**Author:** ![not103](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@not103](https://community.particle.io/u/not103)\
**Post date:** [May 14, 2018, 8:52pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/9 "2018-05-14T20:52:10Z")

</div>

Clear, thanks.

This is my test scheme, is it correct? Because it seems it’s not working.  
It run well, publish and after it goes sleep till next hour, with the 22Kohm resistor it should wake it after 1 minute but nothing.

What’s wrong?

 ![TPL5010_schem](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/d/a/da28bd7b4c1d00132a2e0b38e1c28542eba8e150.jpg)

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 14, 2018, 9:03pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/10 "2018-05-14T21:03:40Z")

</div>

You don’t have to put a pull-up on RST, there’s an internal one (but won’t hurt to add both).

However, you should put an external pull down on DONE. If you let it float, I’ve found that the random floating can prevent the watchdog from triggering.

---

<div class="post-metadata">

**Author:** ![not103](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@not103](https://community.particle.io/u/not103)\
**Post date:** [May 14, 2018, 9:05pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/11 "2018-05-14T21:05:02Z")

</div>

Ok, I was suspecting the resistor on DONE what value? 10Kohm?

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 14, 2018, 9:05pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/12 "2018-05-14T21:05:28Z")

</div>

10K is what I use.

---

<div class="post-metadata">

**Author:** ![modles](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@modles](https://community.particle.io/u/modles)\
**Post date:** [May 25, 2018, 5:44pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/13 "2018-05-25T17:44:44Z")

</div>

Do you find that the TPL5000/10 always trigger the watchdog reset on the first tick of the timer?

With mine it always triggers the reset after 64seconds (my time interval), the first time, despite me putting DONE high, after that it performs as expected???

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 25, 2018, 6:33pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/14 "2018-05-25T18:33:42Z")

</div>

No, I haven’t had any trouble with the TPL5000. I have the DONE pin connected to GPIO D6. There is an external pull-down resistor, 10K.

If I don’t do anything, the TPL5000 resets the Photon after about a minute.

Normally, my firmware pets the watchdog with code like this at the bottom of loop:

```auto
watchdogState = !watchdogState;
digitalWrite(watchdogPin, watchdogState);

```

---

<div class="post-metadata">

**Author:** ![modles](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@modles](https://community.particle.io/u/modles)\
**Post date:** [May 25, 2018, 7:51pm UTC](https://community.particle.io/t/sample-code-for-tpl5010/41813/15 "2018-05-25T19:51:03Z")

</div>

ok thanks. Weird. Oh well it doesn’t matter too much, i just get a reboot first off ,but then all fine.
