Go Down

Topic: My simplest "Arduino" circuit (Read 2958 times) previous topic - next topic

Nick Gammon

Nov 23, 2013, 03:58 am Last Edit: Nov 23, 2013, 04:20 am by Nick Gammon Reason: 1
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: [Select]

// 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: [Select]

210 / 5 * 1000 / 24 / 365 = 4.79 years
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

CrossRoads

Got a calendar started to track it?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Nick Gammon

Here:

Code: [Select]

// 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. :)
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

JimboZA

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?
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

JimboZA

Thanks Nick- I'll look at that.

Jim
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

JimboZA

Looks simple enough:  even I should manage that  8)

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?
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

Nick Gammon

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
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

K5CZ

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.
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...

gvgv

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.

Peter_I

#11
Feb 13, 2014, 08:28 am Last Edit: Feb 13, 2014, 09:36 am by Peter_I Reason: 1
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)
"Nothing is foolproof to a sufficiently talented fool"


gvgv

#13
Feb 16, 2014, 04:05 pm Last Edit: Feb 16, 2014, 10:23 pm by gvgv Reason: 1
Nick,
You say, that ATtiny85 will wake on pin change interrupt or watchdog timer. Which instructions enable pin change interrupt? Sorry for stupid questions.

JimboZA

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?
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

Go Up