0

I'm writing code to run on an ATtiny being programmed by an Arduino as ISP. The ATtiny is to send AT commands over serial link to an RN42 Bluetooth module.

Since the ATtiny has no UART I'm using SoftwareSerial on pins 0 and 1. It seemed logical to put Tx on the "Data Out"/MISO pin and Rx on the "Data In"/MOSI pin. The documentation says to declare this like SoftwareSerial mySerial(Rx, Tx); but I found it only works if you declare it the other way round like SoftwareSerial mySerial(Tx, Rx);

I've taken a screenshot of my code and the pinout, I feel like I'm missing something but when I do it like this it works and makes the Bluetooth module enter command mode. Is the documentation the wrong way round?

Code and Pinout

Matt K
  • 33
  • 8
  • Check the diagram in [this post](https://stackoverflow.com/a/15552137/17034). As you can tell, MOSI is an output pin from the point of view of the master. So one simple explanation is that the web page you found documents it being used as the slave. Hard to tell if that is what was intended, don't use a screenshot to document your question when you can also provide a URL. – Hans Passant Aug 23 '17 at 13:35
  • Yes I see your point. Although, on further thought should it matter what the pins could be used for, SoftwareSerial is bit banged so they're just used as a GPIO's? – Matt K Aug 23 '17 at 14:25

1 Answers1

1

I realised the error of my ways, I was unnecessarily setting the pinMode of the Rx and Tx pins. This threw me off as I thought setting the Rx pin to OUTPUT wouldn't work when actually it does, so I was outputting data on my Rx line and receiving it on the Tx line! The answer is to not assign direction and just let SoftwareSerial handle the pins. Pass the parameters in the order (Rx, Tx).

Here is my cleaner code that is working much better:

#include <SoftwareSerial.h>

const int Rx = 0;                           // pin 5 on ATtiny - DI/MOSI
const int Tx = 1;                           // pin 6 on ATtiny - DO/MISO
const int ButtonIn = 2;
const int OK_LED = 4;
int buttonState = 0;
SoftwareSerial serialBT(Rx, Tx);

void setup() 
{
  pinMode(ButtonIn, INPUT);
  pinMode(OK_LED, OUTPUT);
  serialBT.begin(9600);
}

void loop() 
{
  buttonState = digitalRead(ButtonIn);
  if (buttonState == 0)
  {  
    serialBT.print("$");                    // $$$ enters RN42 command mode
    serialBT.print("$");
    serialBT.print("$");
    delay(3000);

    serialBT.println("R,1");
    digitalWrite(OK_LED, HIGH);
    delay(5000);
    digitalWrite(OK_LED, LOW);
  }
}
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
Matt K
  • 33
  • 8