[null, {"document_url": "http://www.espruino.com/Small+Solar+Powered", "unformatted_src": "refimages/Small_Solar_Powered_final.jpg", "src": "https://www.espruino.com/refimages/Small_Solar_Powered_final.jpg", "formatted_filename": "Small Solar Powered final", "alt_text": "Solar Powered Espruino"}, null, {"document_url": "http://www.espruino.com/Small+Solar+Powered", "unformatted_src": "refimages/Small_Solar_Powered_torch.jpg", "src": "https://www.espruino.com/refimages/Small_Solar_Powered_torch.jpg", "formatted_filename": "Small Solar Powered torch", "alt_text": "Solar Powered Torch"}, null, {"document_url": "http://www.espruino.com/Small+Solar+Powered", "unformatted_src": "refimages/Small_Solar_Powered_torch_inside.jpg", "src": "https://www.espruino.com/refimages/Small_Solar_Powered_torch_inside.jpg", "formatted_filename": "Small Solar Powered torch inside", "alt_text": "Inside the Solar Powered Torch"}, null, {"document_url": "http://www.espruino.com/Small+Solar+Powered", "unformatted_src": "refimages/Small_Solar_Powered_torch_panel.jpg", "src": "https://www.espruino.com/refimages/Small_Solar_Powered_torch_panel.jpg", "formatted_filename": "Small Solar Powered torch panel", "alt_text": "Inside the Solar Powered Torch"}, null, {"document_url": "http://www.espruino.com/Small+Solar+Powered", "unformatted_src": "refimages/Small_Solar_Powered_panel.jpg", "src": "https://www.espruino.com/refimages/Small_Solar_Powered_panel.jpg", "formatted_filename": "Small Solar Powered panel", "alt_text": "Attaching a battery"}, {"document_url": "http://www.espruino.com/Small+Solar+Powered", "unformatted_src": "refimages/Small_Solar_Powered_battery.jpg", "src": "https://www.espruino.com/refimages/Small_Solar_Powered_battery.jpg", "formatted_filename": "Small Solar Powered battery", "alt_text": "All attached"}, null]
Small Solar Powered Espruino So you've made a program in Espruino, and now you want to put it somewhere and have it run without you having to worry about changing batteries.

The obvious solution to this is to use a solar panel and a battery (to keep Espruino working overnight). A really good, cheap source of these seems to be solar powered torches. They're advertised as having a 3.6v rechargable lithium battery inside, which would be perfect for Espruino.
 
For $2, a little device containing a battery and solar panel sounds too good to be true. At least in the devices I've looked at, it is!

Crack the device open by sticking a knife in the seam around the side and levering. It may help to bend and remove the metal loop that holds the keychain. Inside we seem to be off to a good start. There's a little PCB with 3 LEDs, a switch, and a circular plastic battery helpfully named with 'charge'.

However looking a bit closer there are some problems. Fixing it

If your torch is actually as described (with a rechargable battery and a connected panel) please let us know! If not, you're going to have to fit a proper rechargeable battery to it. Now, you should have a solar-powered battery! You can always glue the torch's top back onto the top of the battery to help protect the panel (as in the image at the top of this page).

Note: Li-Ion batteries are not supposed to be charged past 4.2v and there is no circuitry to protect against this. However the solar panel produces so little power that this is unlikely to be a problem.


Let's look at how much power is going in to and out of the battery:

So it's not looking good. We're 20uA short on power (unless you're near the equator where the solar panel will work better). We could wake up less often and for less time, and that would help. It's also not the end of the world - with a 20uA power draw, a charged 750mAh phone battery will still last for 4 years!

The issue here is the solar panel. At 150uA, the panel (with no Espruino) will still take over 6 months just to charge the battery!

We can do better with Espruino though - in Deep Sleep, the majority of the power draw on the Espruino board is from the voltage regulator. If we're running from a Li-Ion battery we don't need the regulator and we can save a lot of power. See the Power Consumption page for details on how to remove the regulator. This brings our power draw down as follows:

Great! We're now at the point where Espruino will run off the tiny solar panel - and the battery gets charged.


Now for the software. Enabling deep sleep mode is as easy as calling setDeepSleep(1);. Here we're going to flash an LED every 10 seconds, and we'll also log the battery voltage into an array. Note that we add 0.7 to the battery voltage, because when the voltage regulator was removed we added a diode instead (which lowers the voltage by 0.7v) - it's not a very accurate measurement of the voltage, but it's a good example.

Now just unplug the USB connector and you're sorted. Espruino should run forever!

As we mentioned earlier, Li-Ion and Li-Po batteries shouldn't be charged past 4.2v. Given that the solar panel produces so little power, we can ensure that the battery never gets charged past this level just by detecting when the voltage gets too high and the turning on an LED (which draws more power than the solar panel produces).

If you use the following onTimer function, that's what'll happen: