Tutorial:A guide to putting your Arduino to sleep

Please put some money in the tip jar by clicking on the donate button to support me so I tin can go on creating contend like this. P.S. delight donate more $1 as PayPal takes minimum $0.30 per transaction

sleeping arduino.png

Sometimes nosotros are in a situation that requires us to put an Arduino in a identify where plugging it in to the power grid is non an option. This happens ofttimes when nosotros try to log information in a remote site, or but demand to take your Arduino active at a specific interval/action.

In these cases putting your Arduino to sleep is the perfect matter to exercise.  Their attending is only required for a brusk corporeality of time eastward.grand. log data in a specific interval, or put out an alert when a predetermined event happens. In this tutorial we are going to experiment with putting your Arduino to sleep and see how to turn your Arduino back on.

This tutorial familiarizes you with the concept and has a pocket-size exercise to see what information technology takes to put an Arduino to sleep. In the side by side couple of web log posts (in 2 weeks or then)  I will show post a couple a projects that will show y'all how to wake your Arduino using a sensor, or a Existent Fourth dimension Clock module (RTC).

MATERIALS NEEDED IN THIS TUTORIAL

What lath to apply?

In this tutorial nosotros will be using the Arduino Uno only because information technology is an easier board to epitome on. In a real live project I would use an Arduino Pro Mini for this. The Arduino Uno and the Arduino Pro Mini have very similar characteristics, the Arduino pro mini has a lot less hardware to ability (e.chiliad. the USB portion, extra leds, and some other stuff) thus using a lot less ability. This is the reason why the Arduino Pro mini is a ameliorate choice.

To give an example a Uno uses betwixt 30-forty mA when awake and about 19 mA when asleep. The Pro Mini uses 25mA when awake and 0.57 mA when asleep. As every mA matters when hooking it up to a battery yous tin can meet that in that location is no competition and the Arduino Pro Mini is the winner.

Note: As a beginner Maker the Arduino Pro Mini might be a fleck intimidating, but there is no reason for it. Yes you need to solder the headers onto the board, and you need a FTDI cable to upload your sketch, but other than that there are no major differences.

Sleep mode

When you look at the documentation of the ATmega328p (click this link for a copy of this certificate) processor used for both Arduino Uno and the Arduino Pro mini you discover there are many unlike slumber modes available. But in a real world scenario in that location is really merely one mode that is useful; The Power down style (SLEEP_MODE_PWR_DOWN).

When you put your Arduino to sleep it turns off all unnecessary components, reducing the power consumption of the MCU (Microcontroller Unit of measurement).  In this fashion the but manner yous can wake information technology up is the use of an external influence (e.m. we requite it a nudge to wake up). Nosotros volition examine how to do this a scrap later on in this tutorial.

Interrupts

Before we go into the code to put an Arduino to sleep we need to understand the interrupt concept. The best fashion to describe it is ; You are working on something you lot really need to concentrate on. You wear headphones blasting your music loud to drown out your surrowndings . You are so concentrated on this that the outside world is lost to y'all. The only style to get your attending is past giving you a nudge. After you receive this nudge you pay attention to what the intermission is about, and after dealing with it yous put the music dorsum on and continue with your task.

Note: I am not going to get to deep into what interrupts are good for, but if you want to larn more about this concept check out my tutorial (Using Interrupts to meliorate the functionality of your project) on this topic

Most true Arduino'south take a couple of pins that exercise just that. The Uno and the Pro Mini have 2 pins (d2 and d3) that accept the adequacy to interrupt what the Arduino is doing. With this we can nudge the Arduino back to a waking country.

Image 1 contains the code snippet that loads the library that contains everything we demand to put your Arduino to sleep, Nosotros also declare the variable interruptPin for digital pin ii. We will later use this for making pivot 2 an input pin. Adjacent we will expect at the Setup() function.

Image_2 Click To Enlarge

Image_2 Click To Overstate

Image 2 has the code for the Setup() function. It is all direct forwards. We declare digital pin 13 as an output pin (LED_BUILTIN is a build in variable for digital pin 13 where an onboard led is connected to).  Nosotros are using the LED as an indecator for when the Arduino is asleep (when LED is on Arduino is awake, when off the Arduino is asleep).

On line eighteen we fix digital pivot 2 every bit an input pin. Y'all discover we use INPUT_PULLUP instead of INPUT. By doing this nosotros use the build-in pull-up resistor to prevent the pin from flopping between Loftier and Depression when nothing is attached to it (same affair you lot would do with a push button).

Adjacent nosotros are going to the master loop() function;

Image_3

Image_3

On line 23 we put in a delay of five seconds before we call the Going_To_Sleep() role on line 24. This is but so you can see that the onboard LED is on to testify your Arduino is awake, and the moment we phone call this the Going_To_Sleep() function the LED goes off to indecate the Arduino is asleep.

Next lets look at the Going_To_Sleep() function itself;

On line 28 nosotros call the sleep_enable() part which is part of the avr/sleep.h library. Information technology enables united states to put the Arduino to slumber, without calling it we can't put the Arduino to slumber. On line 29 we attach an interrupt to pin two as I explained in the Interrupt department.

Syntax

attachInterrupt (interrupt, ISR, mode);

As you notice we utilise a 0 to indicate that nosotros are using pin two. This is because the Arduino Uno/Pro Mini have 2 interrupts. Interrupt 0 is connected to digital pin 2, and Interrupt i is connected to digital pivot 2.  The ISR is the function name that is called when the interrupt is called. In our case it is called wakeUp .  The manner is what needs to happen to the digital pivot to phone call the interrupt. In our case the pin needs to be pulled Low (to GND).

On line 30 we set up the set of sleep mode we want. In our case information technology goes and shuts downwardly everything it can. On line 31 we turn off the LED, and on line 32 we wait a second to give the board the time to turn the led off. Next we actually put the Arduino to sleep with the sleep_cpu() function.

The code halts here until the interrupt is chosen. After waking up the Arduino will outset execute the code in the wakeUp() role and then will continue with line 34 printing the wakeup message on the serial monitor, and on line 35 turning the LED back on.

In the wakeUp()  function we print a line to the serial monitor to permit y'all now the interrupt has been called. The adjacent ii lines are very important to make sure we exercise not get an unintentional loop where the sketch can become stuck, and making your project neglect.

On line 40 we disable the sleep role, and on line 41 we detach the interrupt from pin 2. As I said we do this to forbid a possible endless loop situation.

Exercise 1

Step 1)

Now it is time to upload the sketch. Simply before doing that put a jumper wire in d2. Just exit it unplugged on the other stop for at present. Load your sketch and wait 5 seconds for the LED to turn off and the Arduino to go to sleep.

Step 2)

After the LED turns off insert the other end of the jumper wire in a GND pin on your Arduino Uno. This will pull pivot two Low triggering the interrupt, thus awaking the sleeping Arduino. After the LED comes back on y'all can remove the jumper wire out of GND and v seconds later on the Arduino goes back to sleep.

IN Closing

Now you know the principles of what it takes to put your Arduino to sleep. Every bit y'all see it is a very simple procedure. Finding a good mechanism to control this project is sometimes a bid more problematic. For this reason I volition post a couple example projects on how to wake upward an Arduino using a sencor (e.thou. motion sensor), and a Real Time Clock module (RTC) that will be designed and so you can use them and integrate that into your ain project.

If you lot similar this tutorial and would similar to run into more than of the same blazon, please subscribe to my newsletter using the form below or like my Facebook page. This way you lot get notified when a new post is available.  If you have questions or suggestions please e-mail me or exit it in the comments below. Accept a slap-up 24-hour interval and see you next time.