Introduction: Demystifying ATTINY 85 Serial Comms
Background
This is a short instructable to demystify serial comms on the ATTINY85. Why is this important? Well, this powerful low cost chip can do a lot of what the Arduino can do but it doesn't have a debug facility. Therefore, for example, if you want to ensure that your temperature sensor program is working then without serial comms you are in the dark. You can set up serial comms between the ATTINY85 and your PC using external libraries and configure the pins as RX/TX to communicate with a serial programmer. However, this is very problematic and I spent a lot of time trying to figure this out, which I eventually did. Here's how:
Firstly, please refer to my previous instructable about basic programming of the ATTINY - https://www.instructables.com/id/15-Dollar-Attiny8...
The additional components that you will require are
a) Four x Dupont cables
b). A serial programmer such as a Silicon Labs CP2104. Again, these are very cheap.
Programming the chip
Load the attached ino file into Arduino IDE. Adjust the settings to that shown in the attached image. Most importantly set the internal clock to 1mhz - (see settings in image above) - Using a USBasp programmer, upload the code to the ATTINY85 - It may display" avrdude: warning: cannot set sck period. please check for usbasp firmware update." but this is advisory only.
Running the program (See attached image)
Insert the Silicon Labs CP2104 programmer into the usb port of your laptop.
Connect as follows:
CP2014 <---> Attiny85
GND GND
RXD PIN 3 (This is a adjacent to the ground pin)
Power the ATTINY85, e.g. using a 3.3 volt supply from a Breadboard Power Supply (+/- power rails to +/gnd pins on ATTINY) and then open the Arduino Serial Monitor (see attached image) . You should see number being printed.
6 Comments
3 years ago
It intrigues me why you would use an ATTiny85 when a full ATMega328 is the same price and has all its extra attributes . Why go the hard way?
Once or twice for the knowledge perhaps if you must but why use it otherwise.
Reply 5 months ago
long time have passed since you posted but here is an answer: to save space and the atmega328p is a bit more expensive in most auctions.
Yet I can understand you, the Atmega328 is more convenient to debug.
Question 3 years ago
I am working on attiny85 for serial communication at baud rate 115200 with interanal RC oscillator frequency at 16MHz it is possible to transmit a serial string or serial data @115200 baud rate but when I have receive serial data at 115200 it is reading garbage or undefined data. Please suggest any guidance to how to fix these issue
3 years ago
Hi Scanos,
Why do you need to set the ATTiny85's internal clock to 1 MHz? I have it working with the clock at 8 MHz (internal oscillator, no xtal). And I have to say that I'm impressed, with the baudrate at 115200 it still works! This is my setup:
#define RX 0
#define TX 1
#define LED 2
#include <SoftwareSerial.h>
SoftwareSerial SerialPort(RX, TX);
uint8_t counter = 0;
void setup()
{
SerialPort.begin(115200);
pinMode(LED, OUTPUT);
}
void loop()
{
SerialPort.print("hello world ");
SerialPort.println(counter++, DEC);
digitalWrite(LED, HIGH);
delay(10);
digitalWrite(LED, LOW);
delay(90);
}
Reply 3 years ago
Hi, I use a lot of ATTINY85s and as you may see from my other instructables, I have also set the internal clock to 8 MHz. However, in my experience. some ATTINYs, particularly those not made by Microchip, appear to produce garbage results at 8Mhz. Again, in my experience, setting the clock to 1 MHz usually guarantees integral data. BTW, I have read that the SoftwareSerial library is not reliable above 57600 Baud.
Reply 3 years ago
That's odd. The only reason I can think of is that in some of those ATtiny85 the frequency of the internal RC oscillator is too far off from 8MHz. That would also mean that the baudrate is too far off. But why dividing the clock by 8 would make a difference? No idea. I just checked, all my tiny's are a bit high in frequency, on average 8.05 MHz. I run them on 3.3V.
Something else, I changed from "core", I now use the ATTinycore from "Dr. Azzy".
It can be found here: https://github.com/SpenceKonde/ATTinyCore
and it is actively maintained.