I'm trying to make an RF remote control using ATtiny85 running at 8MHz on internal oscillator, this cheap RF 434MHz transmitter module (like the one below), and VirtualWire lib, but had no success so far.
So, before I go any further, is it possible to use VirtualWire with ATtiny85 running at 3.6V and 8MHz with its internal oscillator? If so, how?
I've found a few scattered sources of information on the Net about it, but couldn't make much sense of it.
I have build ATMega328 versions of the receiver and transmitter using VirtualWire without any major problems. Both modules talk to each other will very little data drop out. Now, when I tried to replace the ATMega328 transmitter with a smaller version using the ATtiny85, I'm having problems.
Here are the symptoms:
The major one seem to be that when I add the VirtualWire include, my sketch seems to slow down a lot. So I figure it's a timing issue. But I'm not sure what the problem is.
I put a scope on ATtiny85 pin 6 (PB1) but I don't see anything being transmitted. I think I've messed up with the pin names.
I can make the LED blink and also can read the buttons without problems.
Here are the schematics and the board design for the remote controller:
The board is currently powered by 2 AA NiMH batteries that are currently at 2.6V. I can make the ATtiny85 blink with that voltage. I plan to power the RC with a 3.6V CR2 lithum battery.
Here are more details of my development enviroment:
- Arduino IDE 1.05
- I'm using ATtiny cores from here
- I'm programming the ATtiny85 using an Arduino UNO R3 as ISP, without problems (it's working as I can make the ATtiny85 blink an LED).
And here's my code:
#include <VirtualWire.h>
#define VCC_PIN A1
#define BTN_PIN A2
#define LED_PIN A3
#define TXD_PIN PB1
#define BLINK_UP_TIME 25
#define BUTTON_NONE 0
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define BUTTON_4 4
#define BUTTON_5 5
#define BUTTON_6 6
void setup()
{
pinMode(BTN_PIN, INPUT);
pinMode(TXD_PIN, OUTPUT);
pinMode(VCC_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// leave RF transmitter off for now
digitalWrite(VCC_PIN, LOW);
// Initialise the IO and ISR
vw_set_tx_pin(TXD_PIN);
vw_setup(2000); // Bits per sec
// let the user know we are alive: heartbeat
blink(3, 50);
delay(1000);
}
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
void loop()
{
byte button = ReadButtons();
if (button != BUTTON_NONE) {
// transmit button pressed
digitalWrite(LED_PIN, HIGH); // Flash a light to show transmitting
digitalWrite(VCC_PIN, HIGH); // turn tranmitter on
delay(10);
buf[0] = button;
vw_send(buf, 1);
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(LED_PIN, LOW); // turn off LED
digitalWrite(VCC_PIN, LOW); // turn tranmitter off
}
delay(100);
}
byte ReadButtons()
{
unsigned int buttonVoltage = analogRead(BTN_PIN);
byte button = BUTTON_NONE; // return no button pressed if the below checks don't write to btn
if (buttonVoltage < 10) { button = BUTTON_1; }
else if (buttonVoltage < 200) { button = BUTTON_2; }
else if (buttonVoltage < 400) { button = BUTTON_3; }
else if (buttonVoltage < 550) { button = BUTTON_4; }
else if (buttonVoltage < 720) { button = BUTTON_5; }
else if (buttonVoltage < 900) { button = BUTTON_6; }
else if (buttonVoltage > 1000) { button = BUTTON_NONE; }
return( button );
}
So, What's wrong with my setup?
PBx
is referring to are not defined by the ATtiny libraries, so they may just kind of incidentally work. Have you verified you can controll PORT B 1 normally (e.g. with plaindigitalWrite()
commands? Alternatively, just swap the LED and TX pins, and use the LED pin (which you know works) for the data output.PBn
is defined by AVR-Libc. It just so happens that arduino-tiny maps PBn to Dn, and eachPBn
is defined to have the value ofn
for use with_BV()
.F_CPU
, or a static variable (class instance) that, when constructed, changes something important.