Go Down

Topic: ATtiny13A and 433MHz Transmitter (Read 298 times) previous topic - next topic

naitsimp

May 17, 2018, 03:45 am Last Edit: May 17, 2018, 05:07 am by naitsimp
Hello,

I have a probelm with my attiny13A and a 433mhz transmitter.
It should always sleep and only wake up if a button is pressed, external interupt, send data over 433mhz and go to sleep again.
I connected a LED to check if it wakes up, it does, but it doesn't send the 433mhz Code.
The 433mhz Code works if i only upload it to the attiny.

Something is causing a problem and I can't figure out what it is for the last few days.

Does someone has an idea whats wrong with my Code?

Here is my sketch:
Code: [Select]
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>

#define PIN_TX (1<<PB3) // PB3 pin, goes to transmitter data pin

const int buttonPin = 0;
const int ledPin = 1;

void setup() {

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin, LOW);

  DDRB |= PIN_TX; // Set output direction on PIN_TX
}


void sleep() {

  GIMSK |= _BV(PCIE);                     // Enable Pin Change Interrupts
  PCMSK |= _BV(PCINT0);                   // Use PB0 as interrupt pin
  ADCSRA &= ~_BV(ADEN);                   // ADC off ADCSRA &= ~(1<<ADEN);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // replaces above statement

  sleep_enable();                         // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
  sei();                                  // Enable interrupts
  sleep_cpu();                            // sleep

  cli();                                  // Disable interrupts
  PCMSK &= ~_BV(PCINT0);                  // Turn off PB3 as interrupt pin
  sleep_disable();                        // Clear SE bit
  ADCSRA |= _BV(ADEN);                    // ADC on ADCSRA |= (1 << ADSC);
  while (ADCSRA & (1 << ADSC));           // wait for completion

  //sei();                                  // Enable interrupts
}


ISR(PCINT0_vect) {
  // This is called when the interrupt occurs, but I don't need to do anything in it
}


void loop() {

  sleep();                      // sleep function called here

  for (int i = 0; i < 3; i++) {
    send("1011101000110101");
    delay(150);
  }

  digitalWrite(ledPin, HIGH);
  delay(150);
  digitalWrite(ledPin, LOW);
}


The 433mhz code is in a second tab in the IDE:
I got it from here: https://github.com/rustam-iskenderov/ATtiny13-transmitter-433Mghz/blob/master/transmitter/transmitter.ino
Code: [Select]
#include <util/delay.h>

const short  nPulseLength = 350;

const short  nHighPulses_0 = (nPulseLength * 1);
const short nLowPulses_0 = (nPulseLength * 3);

const short nHighPulses_1 = (nPulseLength * 3);
const short nLowPulses_1 = (nPulseLength * 1);

const short nLowPulses_sync =  (nPulseLength * 31);

void send(char* sCodeWord){

  while (*sCodeWord != '\0') {
  
    PORTB |= PIN_TX; // same as digitalWrite high

    if(*sCodeWord == '0')
    {
      _delay_us(nHighPulses_0);
      PORTB &= ~PIN_TX; // same as digitalWrite low
      _delay_us(nLowPulses_0);
    }
    else    {
      _delay_us(nHighPulses_1);
      PORTB &= ~PIN_TX;
      _delay_us(nLowPulses_1);
    }

    ++sCodeWord;
  }

  PORTB |= PIN_TX;
  _delay_us(nHighPulses_0);

  PORTB &= ~PIN_TX;
  _delay_us(nLowPulses_sync);
}


If I upload the Code from the link (from github) to the attiny13a it works like it should, the 433mhz Code gets sent.

swe-dude

I cant tell you what is wrong with your code but the sleep function you are using looks a lot more complicated then it needs to be, this is what i am using on a similar project:

Code: [Select]

#include <avr/interrupt.h>
#include <avr/sleep.h>
const int buttonPin = 0;


ISR(PCINT0_vect) {
}

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif



int main(void) {
  init();
  {

    pinMode(buttonPin, INPUT_PULLUP);
    ADCSRA &= ~(1 << ADEN); //adc off
    sbi(GIMSK, PCIE); // Turn on Pin Change interrupt
    sbi(PCMSK, PCINT0); // Which pins are affected by the interrupt

    sei(); // Enable global interrupts
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);


  }



  while (1) {
    if (!digitalRead(buttonPin)) //just to avoid sending on press and release(sends only on release)
    {
      //send stuff here


    }

    sleep_mode();
  }
}


Another thought, not code related INPUT_PULLUP can waste a lot of power over time mayby try a external pullup mayby 1M or at least 500k. and if the 433 transmitter are the same cheap ones I'm using, try driving the transmitter vcc from a pin on the attiny instead this also saves alot of power, just a thought
Best of luck man.

naitsimp

Thank you very much for your sleep code and the tips to reduce the power consumption.
It took me a bit longer to get it working, but it does work now.
I can`t even meassure the power consumption with my multimeter. It is below 0.1microA when sleeping.
To send the Code I need to send it around 35 times to receive it 4 times.

I connected the Button and external Pullup like in this picture:
http://www.martyncurrey.com/wp-content/uploads/2013/12/BTN02.jpg

swe-dude

Hello I'm happy its working for you, a few thoughts:
On the picture you have a pull down not a pull up(the resistor are from gnd not vcc) it will still work but if that is how you have it connected i would change
Code: [Select]

if (!digitalRead(buttonPin))
to
if (digitalRead(buttonPin))
to make sure it send on release and not on press, if you send on press then release interupt can mess with the transmission.


Another thing you should should have a lot more stable transmit, i tested the code and i miss only 1 message in 20, (7m distance)
Make sure the antenna lengths are correct and that you have all the caps you need(0.1uf and 10uf minimum)
Best of luck

Go Up