Let your Arduino get enough sleep

The sensor I am building is based on an Arduino Pro Mini (Atmega328p) with ESP8266 wifi module and a sensor. It will run on batteries with a sample rate of 1 sample per hour. With such low sampling rate it is very obvious that I need to make my microcontroller and the wifi module sleep to reduce power consumption. To wake up the Atmega328p MCU from power down sleep I have to use either the Watchdog Timer (WDT) or an external interrupt. The problem with the WDT on that MCU is that the maximum sleep time it supports is 8 seconds. And to make it sleep longer you need to put the sleep statement in a loop that puts it back into sleep until it reaches a certain count value then resume working.

This is an example of such solution:

for (int i = 1; i <= count ; i++) {
    sleep_cpu ();
}

However, in 1 hour of needed sleep the MCU will wake up 450 times!

Continue reading “Let your Arduino get enough sleep”

Arduino resets when disabling Watchdog Timer!

While working on a small arduino project that will run on batteries I needed to enable deep sleep mode to reduce the energy consumption when not needed until the arduino is awaken by an external interrupt.  However, I am also using the Watchdog Timer (WDT) to make sure the system will not hang up or get into in infinite loop due to some bug or unknown situation. The maximum time that the WDT on the Atmega328p supports is 8 seconds, and if 8 seconds has passed before calling wdt_reset() the WDT will reset the microcontroller, however, my boards will sleep for way longer than 8 seconds so I have to disable the WDT before going to sleep and enable it back once waken up.

Disabling WDT should be an easy thing to do by simply calling wdt_disable(), however, calling this statement also caused the atmega328 to reset!

After looking into the content of the header file “avr/wdt.h” it seems like the Arduino IDE or AVR compiler (not sure who is responsible) is not defining the correct board/chip even though I set the correct board & processor in the Arduino IDE. And the way I solved the issue was to define the correct microcontroller before including the wdt.h:

#define __AVR_ATmega328P__
#include <avr/wdt.h>

Now the code works as intended without causing a reset.

Robo RC

When my son wanted me to buy him a toy robot I asked him if he would like to build one with me instead, he was so interested so I told him that it will be a car robot with two motorized wheels controlled from the phone over bluetooth and he needs to sketch the design then build it using Lego blocks.

IMG_20150110_115340 IMG_20150110_115358 IMG_20150110_115409IMG_20150301_184414 IMG_20150301_184527 IMG_20150301_184516 IMG_20150301_184501 IMG_20150301_184432 device-2015-03-07-210547
 

While he was busy with the sketches I started looking for parts, so I went to eBay and found these wheels


They come in two speeds, I ordered the slower one since it has more torque.
These wheel will need something to control them since the Arduino micro-controller is not strong enough to drive them with enough current so wanted to build an H-bridge using transistors (which I have already) but I found  a module based on the L298 chip  which is very cheap and has a voltage regulator too, awesome.

Next was the bluetooth module, and I went for the HC-06 which is easy to use and also cheap.

Bluetooth HC06-2And finally is of course the Arduino board, I got the Arduino Pro Mini for its small size and low cost.

arduino_miniEven though I actually used the Arduino Uno for the development since it is much easier to upload the code and fix mistakes.

I got them all from eBay with the cheapest price I can get so some took about a month to arrive and I wasn’t in a hurry:

2x Car Robot Wheels: $6.00
L298N Dual H Bridge: $3.00
HC-06 Bluetooth Module: $4.00
Arduino Pro Mini: $2.60
40pin Dupont Wires: $1.50
Total = about $17

You will also need an Android Phone or tablet, any with Android 3.0 and up should do. Lego blocks or anything to use for the car body.

Here is a video we made showing how to use the app and explaining the car we built and its main parts:

To get the Andriod app click on the Google play icon below:
Android-app-on-google-play

for the Android source code click here
and for the Arduino code click here