I am working on communication between a Arduino and ATtiny13, and I need to get this bare-bone communication to work. There is a Arduino Nano powered by USB cable from the PC, and there is ATtiny13 powered by 3V3/GND from the Nano. Nano pin 2 and Tiny pin 2 are connected by single 20cm long wire.
Nano sends one byte of information to Tiny, and then Tiny sends the same byte back to Nano. At least that is the idea, it doesn't quite work.
This is Arduino sketch (master of sorts):
#define DATA_PIN 2
void setup() {
pinMode(DATA_PIN, OUTPUT);
digitalWrite(DATA_PIN, HIGH);
Serial.begin(9600); // Otvori Serial Monitor
}
void loop() {
byte dataToSend = 0x55; // sending some byte
byte receivedData;
sendByte(dataToSend);
receivedData = receiveByte();
pinMode(DATA_PIN, OUTPUT);
digitalWrite(DATA_PIN, HIGH);
Serial.print("Sent: 0x");
Serial.print(dataToSend, HEX);
Serial.print(", Received: 0x");
Serial.println(receivedData, HEX);
delay(1000); // small pause for user's convenience
}
void sendByte(byte data) {
pinMode(DATA_PIN, OUTPUT); // to output mode
digitalWrite(DATA_PIN, LOW);
delayMicroseconds(500);
for (int i = 0; i < 8; i++) {
digitalWrite(DATA_PIN, (data & (1 << i)) ? HIGH : LOW); // send one bit
delayMicroseconds(500); // one bit duration
}
digitalWrite(DATA_PIN, HIGH); // Idle state
}
byte receiveByte() {
pinMode(DATA_PIN, INPUT); // switch to input mode
byte received = 0;
while (digitalRead(DATA_PIN) == HIGH); // wait for LOW (start bit)
delayMicroseconds(750); // wait to half of first bit
for (int i = 0; i < 8; i++) {
if (digitalRead(DATA_PIN) == HIGH) {
received |= (1 << i); // read one bit
}
delayMicroseconds(500); // to the end of the bit
}
pinMode(DATA_PIN, OUTPUT); // back to output mode
digitalWrite(DATA_PIN, HIGH); // idle state
return received;
}
This is ATtiny13 sketch (this would be the slave):
#define DATA_PIN 2
void setup() {
pinMode(DATA_PIN, INPUT);
}
void loop() {
byte receivedData = receiveByte(); // wait for the message from arduino
sendByte(receivedData); // send back that data
}
byte receiveByte() {
pinMode(DATA_PIN, INPUT); // switch to input mode
byte received = 0;
while (digitalRead(DATA_PIN) == HIGH); // wait for LOW (start bit)
delayMicroseconds(750); // wait for the half of bit
for (int i = 0; i < 8; i++) {
if (digitalRead(DATA_PIN) == HIGH) {
received |= (1 << i); // reat the bit
}
delayMicroseconds(500); // to the end of bit
}
return received;
}
void sendByte(byte data) {
pinMode(DATA_PIN, OUTPUT); // to output mode
digitalWrite(DATA_PIN, LOW);
delayMicroseconds(500);
for (int i = 0; i < 8; i++) {
digitalWrite(DATA_PIN, (data & (1 << i)) ? HIGH : LOW); // send one bit
delayMicroseconds(500); // duration of one bit
}
pinMode(DATA_PIN, INPUT); // back to input mode
}
All that I get is this, over and over:
...
Sent: 0x55, Received: 0x0
Sent: 0x55, Received: 0x0
Sent: 0x55, Received: 0x0
...
Now, when I send some hardcoded data from Tiny to Arduino, it gets displayed on Serial monitor just fine.
The problem is somewhere in sending from Arduino to Tiny, please help!