Pages: [1] 2   Go Down
Author Topic: My simplest "Arduino" circuit  (Read 2465 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is my simplest Arduino-based gadget:







It consists of an AtTiny85, one LED, one resistor and one CR2032 battery.

The objective was to have a "flashing LED" that could be used as a burglar deterrent, with a minimal power consumption.

Sketch below:

Code:
// ATtiny85 sleep mode, wake on pin change interrupt or watchdog timer
// Author: Nick Gammon
// Date: 12 October 2013

// ATMEL ATTINY 25/45/85 / ARDUINO
//
//                  +-\/-+
// Ain0 (D 5) PB5  1|    |8  Vcc
// Ain3 (D 3) PB3  2|    |7  PB2 (D 2) Ain1
// Ain2 (D 4) PB4  3|    |6  PB1 (D 1) pwm1
//            GND  4|    |5  PB0 (D 0) pwm0
//                  +----+

#include <avr/sleep.h>    // Sleep Modes
#include <avr/power.h>    // Power management
#include <avr/wdt.h>      // Watchdog timer

const byte LED = 3;  // pin 2
 
// watchdog interrupt
ISR (WDT_vect)
{
   wdt_disable();  // disable watchdog
}  // end of WDT_vect

void resetWatchdog ()
  {
  // clear various "reset" flags
  MCUSR = 0;    
  // allow changes, disable reset, clear existing interrupt
  WDTCR = bit (WDCE) | bit (WDE) | bit (WDIF);
  // set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
  WDTCR = bit (WDIE) | bit (WDP2) | bit (WDP1) | bit (WDP0);    // set WDIE, and 2 seconds delay
  // pat the dog
  wdt_reset();  
  }  // end of resetWatchdog
  
void setup ()
  {
  resetWatchdog ();  // do this first in case WDT fires
  pinMode (LED, OUTPUT);
  }  // end of setup

void loop ()
  {
  digitalWrite (LED, HIGH);
  delay (1);
  digitalWrite (LED, LOW);
  goToSleep ();
  }  // end of loop
  
void goToSleep ()
  {
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  ADCSRA = 0;            // turn off ADC
  power_all_disable ();  // power off ADC, Timer 0 and 1, serial interface
  noInterrupts ();       // timed sequence coming up
  resetWatchdog ();      // get watchdog ready
  sleep_enable ();       // ready to sleep
  interrupts ();         // interrupts are required now
  sleep_cpu ();          // sleep                
  sleep_disable ();      // precaution
  power_all_enable ();   // power everything back on
  }  // end of goToSleep

It sleeps most of the time (lazy thing!) and wakes every 2 seconds to flash the LED for one millisecond. It should use about 4 µA while asleep and when awake the LED will consume about 2 mA. Since that is only for 1 mS per 2 seconds that should average out at about another 1 µA.

If we assume that the battery has a capacity of 210 mAh then it should last for:

Code:
210 / 5 * 1000 / 24 / 365 = 4.79 years
« Last Edit: November 22, 2013, 10:20:04 pm by Nick Gammon » Logged


Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Got a calendar started to track it?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here:

Code:
// Date: 12 October 2013

Although the date/time on the sketch is 18 October 2013. Anyway, close enough. I'll have to check back again in 5 years. smiley
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A more sophisticated version would flash the LED randomly, as if it was connected to the Ethernet. Although my network switch tends to flash every couple of seconds, so it isn't too bad.
Logged


Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 99
Posts: 4399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting... I've been wondering about how one puts a project into "production" so to speak, as opposed to development in a Uno.

Nick, where's the best place to read up on that please? Big question of course is how on gets the sketch loaded? Is there an idiot's guide somewhere?
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My page on breadboard Arduinos may help:

http://www.gammon.com.au/forum/?id=11637

I used SPI (ICSP) to upload the sketch on a breadboard, and when it did what I wanted, I just removed the chip and soldered it in "dead bug" style. I chopped off the other legs (perhaps foolishly) on the socket, so reprogramming it will be a bit of a pain, as it is in upside down.

If I get desperate (eg. to change the interval to 3 seconds) I can always either solder on extra wires for programming onto those stubs, or unsolder the battery wires, get at the chip, reprogram it, and put everything back.
Logged


Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 99
Posts: 4399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Nick- I'll look at that.

Jim
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 99
Posts: 4399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks simple enough:  even I should manage that  smiley-cool

My local supplier has both the ATTINY85-20PU and ATMEGA328P-PU available. Both are 20MHz versions, is that ok?

Also, one last question Nick if I may: how big a deal is to convert your explanation to the Tiny? Will I be able to figure that out when I look at the pins?
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The 20 MHz version will be fine, considering it is running at 8 MHz.

The programming pins are pretty clearly labelled in the datasheet.

I describe the uploading process (which I used in this case) here:

http://www.gammon.com.au/forum/?id=11638&reply=1#reply1
Logged


Czech Republic
Offline Offline
Sr. Member
****
Karma: 4
Posts: 313
Absolute Beginner, yet.
Newbie in Arduino
since May 2013
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, so many years! And did you know about this?

Early CD versions of the Pink Floyd's album Pulse came with a blinking red LED on the side of the case.

http://en.wikipedia.org/wiki/Pulse_%28Pink_Floyd_album%29#LED_packaging

This was designed by EMI contractor Jon Kempner, who was awarded the platinum disc, using the now discontinued LM3909 LED flasher IC. The circuit was powered by a single AA cell; the battery life was stated to be over 6 months. Some versions were also made with 2 AA batteries and later editions of the CD set did not feature the blinking LED.
Logged

Arduino Uno R3, Arduino Mega 2560 R3, Ethermega (R3), and some Ethernet Shields (R3), Chronodots, TMP36 sensors, DS18B20 sensors, LCDs/TFTs, RS232C to learn and play...

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice and simple.
Would it be much more complicated to have Attiny85 sleep for 8 hours, for example? I am looking to have a relay switch on every 8 hours for 10 minutes.
Logged

Denmark
Offline Offline
Full Member
***
Karma: 10
Posts: 224
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice!

(And I wonder how I managed not to see this one when I was building my Cable Locker Intruder Alarm ?)



I'm deeply impressed that it is possible to run for so long on a small battery, simply by "not wasting power when it is not needed".


Ill have to make an LED-throwie in that style!
It would just hang there and blink forever!
(Or at least, it would die of corrosion before running out of power)
« Last Edit: February 13, 2014, 03:36:08 am by Peter_I » Logged

Bob Pease: "My favorite programming language is ... solder."

Offline Offline
Sr. Member
****
Karma: 6
Posts: 429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very nice example!

Made me look for a tutorial. smiley
http://donalmorrissey.blogspot.co.uk/2010/04/sleeping-arduino-part-5-wake-up-via.html
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick,
You say, that ATtiny85 will wake on pin change interrupt or watchdog timer. Which instructions enable pin change interrupt? Sorry for stupid questions.
« Last Edit: February 16, 2014, 04:23:42 pm by gvgv » Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 99
Posts: 4399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A bit off topic, but related...

If I program a chip like the atTiny using the Uno as ISP (in IDE: File > Examples > ArduinoISP), may I leave the Arduino and the target chip connected once the sketch runs in the atTiny? In real use of course the tiny will be disconnected, but I'm thinking of during code development where it's common to upload many version of code.

I'm assuming the target fires up its sketch when programmed through the Uno as ISP. But with the tiny's i/o pins being the same ones as the are connected for ISP, I'm not sure if it's wise? But my understanding of ISP is that the whole point is to be able to leave the chip in the programming circuit.

Advice please?
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Pages: [1] 2   Go Up
Jump to: