NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » a Tiny project

April 21, 2014
by Noter
Noter's Avatar

My wife wants to record shows our dvr in the afternoon but the problem is the satellite box goes to sleep during the day so when the recorder comes on it just records an hour to two of a message saying "press select to ..." whatever. Last year I tried to make a remote control that would keep the dvr awake all day but without success. Since then I've learned a lot more about PWM as well as how to use my logic analyzer and now I have a working prototype!

ATtiny10

As seen in the photo there is not much to the hardware in this project. I have a small breadboard stuck on top of a 3xAAA battery holder so when the batteries are fresh the voltage is around 4.5v. Add an infrared LED, regular red LED, ATtiny10 MCU, and that's it. Initially I got the IR signal working on an ATmega328p but considering there was not much to do in the code it was a perfect candidate for the tiny10. I used to think these tiny chips were a waste of time but now I always keep an eye out for apps for them. They cost about 40 cents so they are quite inexpensive.

I laid out a small pcb and ordered a few via OshPark so I can finish this project and get my little prototype back. I'll use a battery box that has a switch with the batteries enclosed and stick the pcb on top with some double sided foam tape. I figure the batteries will last for years before they run down but really that's just a guess.

The code compiles to 712 bytes of flash which fits nicely on the ATtiny10. :-)

// IR-KeepAlive.c
// Send IR remote codes to Dish receiver every hour
// so it won't go to sleep.
//
// Sends Select code followed by Cancel.

#include <stdlib.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <util/delay.h>
#include <libNoter/pin_io_macros.h>

#define SELECT 0xBFFF
#define CANCEL 0xB7FF

#define IR_LED          B,0   // OCR0A
#define STATUS_LED      B,1   // OCR0B

#define PERIOD_SECONDS 3600  // ~ once an hour
volatile uint16_t elapsed_seconds = PERIOD_SECONDS;

ISR(WDT_vect, ISR_BLOCK){
  elapsed_seconds += 8;
}

void pwm_on(){
  // config timer0 for ~ 56 kHZ as measured with scope
  ICR0 = 138;
  OCR0A = 69;
  TCNT0 = 0;
  GTCCR = _BV(PSR);
  TCCR0A = _BV(COM0A0) | _BV(COM0A1) | _BV(WGM01);
  TCCR0B = _BV(CS00) | _BV(WGM03) | _BV(WGM02);
}

void pwm_off(){
  TCCR0B = 0;
  TCCR0A = 0;
  CLEAR(IR_LED);
}

void send_mark(){
  pwm_on();
  _delay_us(420);
  pwm_off();
  _delay_us(6000);
}

void send_1(){
  pwm_on();
  _delay_us(420);
  pwm_off();
  _delay_us(2500);
}

void send_0(){
  pwm_on();
  _delay_us(420);
  pwm_off();
  _delay_us(1500);
}

void blink(){
  // blink and pwm_on/off are mutually exclusive
  // because they both user timer0
  ICR0 = 500;
  OCR0B = 1;
  TCNT0 = 0;
  GTCCR = _BV(PSR);
  TCCR0A = _BV(COM0B1) | _BV(WGM01);
  TCCR0B = _BV(CS00) | _BV(WGM03) | _BV(WGM02);
  _delay_ms(100);
  TCCR0B = 0;
  TCCR0A = 0;
  CLEAR(STATUS_LED);
  _delay_ms(100);
}

void send_code(uint16_t code){
  uint8_t i;
  uint8_t i1;
  uint16_t t_code;
  for(i=0;i<4;i++){
    t_code = code;
    send_mark();
    for(i1=0;i1<16;i1++){
      if(t_code & 0x8000){
        send_1();
      } else {
        send_0();
      }
      t_code = t_code << 1;
    }
  }
  send_mark();
}

//---------------------------------------------------------------------------------------
void main() {
  // bump clock to 8MHz
  CCP = 0xD8;   // enable protected change
  CLKPSR = 0;   // set 8MHz prescaler = 1

  // power reduction
  ACSR = _BV(ACD);                                                // shutdown analog comparator
  DIDR0 = (_BV(ADC0D) | _BV(ADC1D)  | _BV(ADC2D) | _BV(ADC3D));   // disable digital inputs
  power_adc_disable();                                            // disable adc

  // setup ports
  OUTPUT(IR_LED);
  OUTPUT(STATUS_LED);

  // startup watchdog timer
  CCP = 0xD8;                     // enable protected change
  WDTCSR = _BV(WDP3) | _BV(WDP0); // 8 second timeout
  WDTCSR |= _BV(WDIE);            // interrupt on timeout
  sei();                          // enable global interrupts

  // blink status led on startup
  blink();
  blink();

  for(;;){
    if(elapsed_seconds >= PERIOD_SECONDS){
      // send codes
      cli();
      send_code(SELECT);
      _delay_ms(100);
      send_code(CANCEL);
      elapsed_seconds = 0;
      blink();
      sei();
    }
    // powerdown sleep until watchdog wakeup
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    sleep_cpu();
    sleep_disable();
  }    
}
April 22, 2014
by Rick_S
Rick_S's Avatar

Neat project and a perfect candidate for that little bugger. As small as that circuit board looks to be, you might be able to fit it inside a switched battery holder. Some of the ones I had, had a hollow section above the batteries with the switch in it. There was enough room that I was able to fit a small regulating circuit inside after the switch.

April 22, 2014
by Noter
Noter's Avatar

Great idea Rick! I think it will fit in the AAA box but if not I can switch to the slightly larger AA box. My pcb is .5 x .43 inches and to get 3 of them from OshPark costs $1.05 with shipping included. Usually takes about 2 weeks to receive boards in the mail. Can't beat OshPark for small orders.

pcb

April 22, 2014
by JKITSON
JKITSON's Avatar

Neat..

I found some parts of your code that may help me on a project. Thanks Noter. Great project..

Jim

April 22, 2014
by esoderberg
esoderberg's Avatar

Noter,

Nice project. I hadn't looked close at Oshpark, but that is a great deal for really small boards.

Eric

April 23, 2014
by Rick_S
Rick_S's Avatar

I agree, for real small boards that you only want a few of it is a great deal. You could fit a lot on a 1" x 2" board for only $10 shipped and I could see myself taking advantage of that.

Granted, much bigger than that and I'd still probably have to fall back on the PCB service from iTeadstudio. For $10 + Shipping there, you can get 10pcs 2" x 2" (5cm x 5cm) and it's only $20 + Shipping for 10pcs 4" x 4" (10cm x 10cm).

You'll have to let us see the final project in the enclosure.

Rick

April 23, 2014
by Noter
Noter's Avatar

ITeadStudio is definitely a better deal especially if you want more. Their site says you can go 5up on sub-boards but if you send your design to them for advanced approval you can have as many as will fit. In this case 12 of these little boards would fit on their 5cm x 5cm board giving a total of 120 pcbs for $10 + Shipping. Can't beat that anywhere.

Although I probably won't build more than one of these I am working on another little project to make my own G-35 GE type Christmas lights. Have to move up to a tiny85 for that one but it will still be one per RGB LED and lots of small pcbs. I plan to use Oshpark for a few prototypes along the way and then order the batch from ITeadStudio.

Noticed today that the security certificate for nerdkits has expired. Makes me wonder if the end is near. Anyway if the site is still up I'll post pics of the final enclosure in a couple of weeks.

April 23, 2014
by Noter
Noter's Avatar

And if you do get a bunch of little boards from ITeadStudio separating them becomes an issue. With some of the Windows money I saved by upgrading my household PCs to linux I bought a little table saw for ~$130 and it works great.

saw

Proxxon 37006 KS 115 Bench Circular Saw

May 01, 2014
by Noter
Noter's Avatar

Even with the small pcb it was too tight in the AAA battery holder so had to go with the AA case. Also had to oversize the led holes a bit so I could make the angle to get it inserted. Then gave it a squirt of hot glue to hold it all in place before installing the batteries and cover. Works good, I'm happy with it and more importantly so is my wife. :-)

keepalive1

keepalive2

May 01, 2014
by esoderberg
esoderberg's Avatar

Noter,

Even though most of the work was probably in the programming and electronics, the nice packaging really makes the project look good. Funny to me how looking like it could be "retail" is a mark of quality for a custom job even if avoiding retail was part of the goal.

Eric

May 01, 2014
by Rick_S
Rick_S's Avatar

That's very similar to the case I was talking about. It definitely looks good all enclosed.

May 02, 2014
by sask55
sask55's Avatar

Yes I agree. Your tiny project really dose look good in that case both from the outside view and the board placement inside the case. It appears that you have a reasonably rugged, very transportable, good looking creation.

May 02, 2014
by JKITSON
JKITSON's Avatar

Very nice complete project. Good work. Jim

May 03, 2014
by Noter
Noter's Avatar

Thanks everyone for the nice feedback. I'm thinking these enclosed battery holders can be inexpensive project boxes even without using batteries. Might have to trim their insides a bit but two bucks for a little project box is a pretty good deal.

May 05, 2014
by Pew446
Pew446's Avatar

Wow! Really cool project, neat idea. This is what I love about the electronics hobbyist communities. Where most people would either give up recording or go buy a new DVR, you made a really inexpensive solution that works well and in the end, everyone is happy. I like that you added a case to it, looks really nice.

Thanks for the reference to Oshpark and the little proxxon saw, btw. Very helpful!

Post a Reply

Please log in to post a reply.

Did you know that you can make a spooky Halloween Jack-O-Lantern with a microcontroller? Learn more...