Arduino and ATtiny Serial Communication

The question I want to answer in this post is: “How to debug the ATtiny?”
Short Answer: One way to debug is to directly send and view the output of the pin via SoftwareSerial in Arduino IDE.

Long Answer: The Serial.print function is not available with the Arduino IDE when it is configured as ISP. The only commands supported by Arduino IDE when it is configured as ISP to program the ATtiny are:

  • pinMode()
  • digitalWrite()
  • digitalRead()
  • analogRead()
  • analogWrite()
  • shiftOut()
  • pulseIn()
  • millis()
  • micros()
  • delay()
  • delaymicroseconds()
  • SoftwareSerial

Furthermore, the ATtiny does not have a built in hardware UART thus is unable to provide direct hardware support for serial communication. The ATtiny has an USI that can be used to facilitate I2C and SPI communication, which are commonly used to link additional chips when there are insufficient I/O pins. Therefore, the Serial object is unavailable. Luckily, there are still several ways to achieve Serial communication between the ATtiny and Arduino IDE:

  1. TinyDebugSerial.
  2. TinyISP.
  3. SoftwareSerial.

This project will show how to achieve Serial communication between the ATtiny 85 and Arduino IDE using the SoftwareSerial method. It emulates the UART by allowing serial communication on the digital pins 0 (Rx) and 1 (Tx) of the Arduino, and using software to replicate the UART functionality (hence the name Software Serial).

Compared to the TinyDebugSerial, the SoftwareSerial method occupies more memory and may require TinyTuner to tune the internal oscillator. This is because, the internal oscillator of an ATtiny from the factory can be off by as much as 10%. This inaccuracy in the clock period can skew the rate of the data sent to and from the board, thus creates strange characters in the serial monitor. Compared to the TinyISP, the SoftwareSerial does not require the purchase of an additional hardware (if Arduino is readily available). The TinyISP is a replacement for the ArduinoISP. With TinyISP a serial/usb converter is already built onto the chip to provide direct serial communication.

It is important to note that prior to using the SoftwareSerial library, the Arduino needs to already be configured as ISP, the correct chip is selected, the correct internal oscillator frequency is selected and the burn bootloader command has been executed.

Schematic:

step2_a

  • The diagram provided is only an initial circuit illustration. Follow the steps below for a complete circuit implementation.
  • Resistor 220 Ω:  between the Arduino Tx pin and the ATtiny Tx pin. This provides isolation between the serial lines. A larger value resistor will also work.

Config Serial Communication:

  1. Make sure the Arduino Tx and Rx pins are not connected. The only wires connected between from Arduino to ATtiny are from Digital pins 10, 11, 12, 13, Reset to capacitor, power and ground. Since the Arduino IDE uploads the code on the same Tx and Rx lines, the Tx 1 and Rx 0 pins can not be connected to the ATtiny while uploading the sketch.With serial, only one device can be communicating on the line at a time.
  2. Upload the Arduino code.
  3. Connect Reset directly to ground (not through a capacitor). This forces the Arduino into reset to perform serial communication with the ATtiny.
  4. Connect the Tx pin declared in the Arduino code to the Rx pin on the Arduino (Digital pin 0 Rx).
  5. Connect the Tx pin on the Arduino (Digital pin 1 Tx).
  6. Open the Arduino serial monitor. Make sure the baud rate is set correctly as per the code.
  7. Repeat steps 2-7 every time the Arduino is re-connected.

Arduino Script:

#include 

const int LED = 0;
const int Rx = -1; //unused Rx
const int Tx = 3;
const int inputPin = 2; //wire act like antenna
SoftwareSerial mySerial(Rx, Tx);
const float VCC = 4.98;
int val = 0;

void setup()
{
pinMode(Rx, INPUT);
pinMode(Tx, OUTPUT); //initialize internal pull-up resistor
pinMode(LED, OUTPUT);
pinMode(inputPin, INPUT);
mySerial.begin(9600);
}

void loop()
{
digitalWrite(LED,HIGH);
delay(300);
digitalWrite(LED,LOW);
delay(300);
val = analogRead(inputPin);
mySerial.println(val);
delay(500);
}

Testing:

The Arduino is reading the values from the ATtiny wire and displaying it in the serial monitor with 9600 baud rate.

step4_a

step4_b

Arduino serial monitor.