Attiny85: no serial output? - Page 1
Forum section: electronics / beginners

author: phennessey
author_rank: Contributor
timestamp: January 19, 2020, 12:43:48 am
content: I’m seriously confused.I have the Sparkfun Tiny AVR usb programmer for my attiny85. I’m testing a rotary encoder, and I want to see its output on my computer. No serial output method I could find would work, despite these sources saying they would.But then, the Sparkfun AVR information page says the attiny85 doesn’t support serial output. Wait, what?Who’s got it wrong? Anyone have experience with this? I have the chip in my breadboard, with all 8 lines connected to the programmer, and i can reprogram the chip while it’s on the breadboard. I set pins 3 and 4 to RX/TX. I even switch the chip to 8mhz mode (as some have suggested).Still nothing. I’m at my wits end. All I want to do is test a rotary encoder so I can troubleshoot it (I’ll need an RC filter to help with debouncing, I just want to be sure that’s the problem and not my code.)So. If you’ve successfully gotten serial output from this chip and this programmer, please let me know!I’ll post more info and images soon.

author: ataradov
author_rank: Super Contributor
timestamp: January 19, 2020, 01:01:48 am
content: ATtiny85 does not have a dedicated UART peripheral. It has something called USI (Universal Serial Interface), which can be used to accelerate multiple serial protocols.Or alternatively you could just bit-bang the UART.I don't know about support for any of this in Arduino-like software though.

author: phennessey
author_rank: Contributor
timestamp: January 19, 2020, 01:46:57 am
content: No idea how to “bit bang” or how to even interface with this. I need very specific step-by-step instructions. I am, after all, a beginner.

author: james_s
author_rank: Super Contributor
timestamp: January 19, 2020, 01:51:04 am
content: Start with a simpler project, or use a mega328 based arduino. You'll learn more by going through a series of tutorials from start to finish than you will asking for someone to do all the work for you and lay it out in neat tidy steps. Bit banging is a very basic concept that you will encounter often. Search engines are your friend.

author: ataradov
author_rank: Super Contributor
timestamp: January 19, 2020, 01:51:16 am
content: Well, you perhaps picked the wrong device for a beginner. ATtiny85 is very cramped and hard to debug. But the limited memory size pretty much lets you just write the program and reasonably expect it to work right away if you know what you are doing.I'm sure there are tutorials on this somewhere on the internet.

author: phennessey
author_rank: Contributor
timestamp: January 19, 2020, 02:14:00 am
content: I never asked anyone here for instructions. I merely stated that i needed them. Tutorials are definitely right up my alley.

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 19, 2020, 02:14:36 am
content: The Arduino environment has two libraries you can use to implement serial output: SoftwareSerial (was also known as NewSoftSerial), and AltSoftSerial.If you only need to send data from ATtiny85 to your computer, the SoftwareSerial will suffice. (It is basically okay for whenever you know which side is sending and the other receiving, and data only flows in one direction at a time.)It should be included with your Arduino installation automatically.If you need bidirectional data transfers, you should use AltSoftSerial, as it allows full duplex (simultaneous send and receive) communications. (AltSoftSerialis maintained by Paul Stoffregen, the Teensy developer and half of PJRC.com.)Pick a baud rate, and set it for both the serial dongle, and in your Arduino code. Remember that TX on one end is connected to RX on the other; i.e. the wires "cross".The example sketch you can try with the ATTiny85 is as follows:Code:#include <SoftwareSerial.h>SoftwareSerial mySerial(3, 4); // rx, txvoid setup(){mySerial.begin(9600); // 9600 baud}void loop(){mySerial.println("Ping!"); // Write "Ping!" and newline to mySerial,delay(1000); // and wait 1000ms = 1 second.}This is set to 9600 baud, but you can try other rates as well.

author: Renate
author_rank: Super Contributor
timestamp: January 19, 2020, 03:35:08 am
content: Yeah, the ATTiny85 is a strange beast.I guess that they figured with so few pins you wouldn't use a crystal and without a crystal you wouldn't use serial data.If you are just transmitting serial (especially fixed messages) you can do it easily using the USI as an SPI equivalent.If you want to transmit "Hello", that's five characters of 10 bits (including start and stop), 50 bits total.7 bytes of the proper SPI data will be the exact same thing. (You'll have an extra 6 bits of high at the end).Ok, you have to sit down with a piece of paper to figure out the bits (or write a program).I used this technique on a 4 button box that barks out canned phrases as a remote control.

author: james_s
author_rank: Super Contributor
timestamp: January 19, 2020, 04:27:29 am
content: The attiny85 was never part of the Arduino vision, support was added later and it comes with caveats given the limitations of the chip. The 8 pin attiny series was designed for simple embedded projects that need only a few pins, it's surprising how much you can implement with so few pins if you are clever. There is no onboard UART because it is not typically needed, few commercial products use RS232 anymore for anything other than debugging, if you're designing a product around the attiny and you need serial debugging, you can use a larger AVR for development and then move over to the tiny when you're finished.

author: phennessey
author_rank: Contributor
timestamp: January 19, 2020, 05:25:03 am
content: I did exactly this, but I’m not sure it’s working right. I’ll post photos of my setup so you can offer more feedback. I only want to read from the chip. I don’t need to send data to it.

author: sleemanj
author_rank: Super Contributor
timestamp: January 19, 2020, 05:33:12 am
content: If I was better at marketing, everybody would know about my Arduino core (fork) which is especially tuned up for these very tiny tinys....https://github.com/sleemanj/optiboot/blob/master/dists/README.mdwith my core, Serial.print() should work out of the box, TX/RX pins as below

author: phennessey
author_rank: Contributor
timestamp: January 22, 2020, 12:58:10 am
content: How do you edit the core?

author: Psi
author_rank: Super Contributor
timestamp: January 22, 2020, 01:30:09 am
content: Quote from: phennessey on January 19, 2020, 05:25:03 amCan you post your code?The example Nominal Animal posted should work for you.Some traps for young players.- Tx and Rx connections can end up reversed because some manufactures label them correctly and others label them wrong.- Baud rate maybe wrong due to clock speed being wrong. (or at least not what the code expects it to be)

author: sleemanj
author_rank: Super Contributor
timestamp: January 22, 2020, 04:50:18 am
content: Quote from: phennessey on January 22, 2020, 12:58:10 amDo you mean how to install a third party core? In that case, File > Preferences, "Additional Boards Manager URLs", click the window icon on that line and paste the third party core's json url. Close preferences, Tools > Board > Board Manager find the core you want, install. Choose Board from Tools > Board after installing.Do you mean after you have installed a third party core and you want to change it for some reason? File > Preferences, look down at the bottom of the window where it says "More preferences can be edited...." click the location of preferences.txt and it should open up the folder containing that, inside there is a packages folder, open the packages folder and you can drill-down into the installed core files. But, be aware that if you update the installed core, your changes would be nuked.

author: hsn93
author_rank: Regular Contributor
timestamp: January 22, 2020, 10:50:30 am
content: Quote from: phennessey on January 19, 2020, 02:14:00 amfabacademy has one simple code for bit banginghttps://fabacademy.org/2019/docs/FabAcademy-Tutorials/week08_embedded_programming/attiny_c.htmlthere is source codes here:http://academy.cba.mit.edu/classes/embedded_programming/index.html

author: westfw
author_rank: Super Contributor
timestamp: January 25, 2020, 09:37:33 am
content: Sleemanj: how’d you wind up with all that “core” code in an optiboot fork?

author: sleemanj
author_rank: Super Contributor
timestamp: January 25, 2020, 08:19:44 pm
content: Quote from: westfw on January 25, 2020, 09:37:33 amOstensibly because the original purpose was principally as a distribution of my optiboot fork. As is common with this sort of thing, it grew a bit.

author: phennessey
author_rank: Contributor
timestamp: January 27, 2020, 04:17:07 am
content: Here's my setup:Rotary encoder with A/B pins connected to pins 2/3 on the attiny85All attiny pins connected to Tiny AVR Programmer with jumpersAttiny clock set to 8mhzHere's some sample code I tried -- not to interface with the encoder, just to get the chip to send a simple serial signal so I could confirm whether it even could work at all, so I found this code in another forum:Code:#include <SoftwareSerial.h>SoftwareSerial Serial(3, 4);void setup() {Serial.begin(9600);}void loop() {Serial.println("Hello World!"); //Print this once every second.delay(1000);}When I open the serial monitor, I see no activity. I tried different pins, different baud rates, different MHz settings (1 and 8, no higher!). Still...nothing.I have another project (called RGB_trig) where I use the rotary encoder (successfully, I might add!) to adjust an RGB LED. When I try to combine this serial setup with the code (even just the #include at the top) and include a serial.println to be called when the rotary encoder triggers, I get this error:Code:libraries/SoftwareSerial/SoftwareSerial.cpp.o (symbol from plugin): In function `SoftwareSerial::read()':(.text+0x0): multiple definition of `__vector_2'sketch/RGB_trig.ino.cpp.o (symbol from plugin):(.text+0x0): first defined herecollect2: error: ld returned 1 exit statusMultiple libraries were found for "SoftwareSerial.h"Used: /Applications/Utilities/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerialexit status 1Error compiling for board ATtiny25/45/85.Why can't it compile all of a sudden? I don't have multiple libraries of SoftwareSerial.h. I checked that directory, and it's just one file. Is this because I have an interrupt set up for the encoder? I use an interrupt so that I don't have to rely on a loop to adjust the LED brightness. Let me know if you all need to see my code for this.Thoughts? All I want to do is trigger a serial print output when the switch is triggered so I can study its output and determine if it needs to be debounced.

author: sleemanj
author_rank: Super Contributor
timestamp: January 27, 2020, 05:02:32 am
content: Quote from: phennessey on January 27, 2020, 04:17:07 amDo you actually have a USB Serial adapter connected?I think you may be perhaps thinking your programmer can do that, which is unlikely.You should disconnect your programmer, and connect a USB Serial adapter (UART, not RS232 of course) in order to talk/listen to your chip over serial after programming.QuoteYes both SoftwareSerial and your own code are trying to use the same interrupt, which is no good.

author: phennessey
author_rank: Contributor
timestamp: January 27, 2020, 05:06:52 am
content: Crap.I thought this thing was more capable...guess not. Looks like I’ll be getting a separate serial adapter!

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2020, 05:15:29 am
content: Separate serial adapter will not help here. Your MCU still can't output UART. You need to make a thing that is similar to SoftwareSerial, but does not use interrupts. This is pretty easy for TX-only.

author: phennessey
author_rank: Contributor
timestamp: January 27, 2020, 05:35:30 am
content: Quote from: ataradov on January 27, 2020, 05:15:29 amHow do I do that?

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2020, 05:42:14 am
content: I don't have any readily available code. The basic idea is to set a GPIO pin high and low according to the UART protocol and your data. Use blocking loops to generate bit-long delays.Or better yet, figure out how to use USI and use that.

author: sleemanj
author_rank: Super Contributor
timestamp: January 27, 2020, 06:28:18 am
content: Quote from: ataradov on January 27, 2020, 05:15:29 amYes it will. That appears to be the poster's primary problem (ignoring the interrupt conflict) as even without the conflict they are unable to get any serial output when using SoftwareSerial - because they don't have a USB-Uart connected, only an ISP.QuoteOr, they could as I posted above, use my core. Which provides "Serial" ready to roll on the pins indicated above and uses the Analog Comparator for it's interrupts, which should work out of the box with their code.Or, they could rewrite their encoder handling so it does not use pin change interrupts (which SoftwareSerial does), either use the External Interrupt pin (85 only has one) or not use interrupts.@phennesseyInstall my core, as above.Remove this#include <SoftwareSerial.h>SoftwareSerial Serial(3, 4);you don't need it with my core. Put your RX/TX on the pins indicated in my image above, connected to your USB-Serial (Uart) device, move your encoder to some other pins if you have that connected to those, adjust code to suit.

author: phennessey
author_rank: Contributor
timestamp: January 28, 2020, 03:56:56 am
content: I'm not sure what kind of cable I actually need. Would a USB to TTL Serial Cable work?I need to debug the encoder while its plugged into the Attiny85. Is it really impossible to do that while it uses pin interrupts?The programmer I'm using clearly indicates serial in and out (MOSI, MISO). Why can't these work for me?https://cdn.sparkfun.com/assets/c/c/3/1/8/52715ae8757b7f30048b4567.pngWhat do you think about the information in this article?https://www.hackster.io/porrey/easy-serial-on-the-attiny-2676e6

author: sleemanj
author_rank: Super Contributor
timestamp: January 28, 2020, 05:36:22 am
content: > The programmer I'm using clearly indicates serial in and out (MOSI, MISO).That is not serial, that is SPI.> Is it really impossible to do that while it uses pin interrupts?I already described ways around your problems in the previous post.

author: sleemanj
author_rank: Super Contributor
timestamp: January 28, 2020, 05:38:27 am
content: > I'm not sure what kind of cable I actually need. Would a USB to TTL Serial Cable work?USB to TTL adapter. Get one with CH340 or CP2102. Cost a dollar or two from China.

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 28, 2020, 07:00:31 am
content: Quote from: phennessey on January 28, 2020, 03:56:56 amThere are a number of serial protocols used by these Arduino-compatible microcontrollers. The most common ones are UART, SPI, I2C, and CAN. They all transmit bits serially, one after another, but the exact implementation details differ; they are not interchangeable. (Some microcontrollers do have hardware that can support more than one using the same pins -- not at the same time, but to allow wider configurability.)(UART andRS-232are closely related, typically only the physical connector and voltage levels differ. TTL voltage levels sleemanj mentioned are most commonly used, with "zero" being less than 0.8 V, and "one" being 2.0 V or higher, when a 5 V supply is used.)Because of various reasons, when someone says "serial" when talking about microcontrollers, they usually mean the UART type serial connection. This is particularly true within the Arduino environment: TheSerial Monitorwindow only supports UART connection to the microcontroller.Many AVRs, including ATtiny85, can be reprogrammed using SPI. Your programming dongle connects to the AVR using five pins: VCC, GND, MOSI (master out slave in; from programmer to AVR), MISO (master in slave out; from AVR to programmer), and MCLK (SPI clock).Yes, you can use that USB-SPI dongle to talk to your computer, but the Arduino environment cannot not show that communication in the serial monitor window.So, what you need to do, after programming the ATtiny85, is to disconnect it from the programmer, then connect it to a USB-UART dongle. They indeed cost only about 1-2 USD/EUR, shipped, from eBay/BangGood. They are simple devices, and the ones I've got, Just Work.

author: phennessey
author_rank: Contributor
timestamp: January 29, 2020, 03:54:41 am
content: Quote from: Nominal Animal on January 28, 2020, 07:00:31 amTHIS. This right here, people. This is the information I needed (emphasis added).Thank you all so much for your help and insight. You've given me everything I think I need to figure this out. I will post results here once I get it working!