ATTiny13A serial output/monitoring - Page 1
Forum section: electronics / general

author: CyberBob
author_rank: Contributor
timestamp: August 03, 2020, 05:39:23 pm
content: I do a lot of experimenting with microcontrollers and I'm trying to use an ATTiny13A microcontroller. I want it to collect data and show it in a serial monitor (I do this all the time with other microcontrollers but these connect via USB). I have a FTDI USB to TTL serial adapter as I have seen that some people have used this with the ATTiny13A. I can't seem to get it to work. I use the Arduino IDE to program the microcontrollers and monitor the serial ports. Can anyone help?

author: sleemanj
author_rank: Super Contributor
timestamp: August 04, 2020, 12:34:30 am
content: Usemy fork of ATTinyCoreand then you can use Serial.whatever as you would normally, to a degree.You will find some specific ATTiny13 examples in it, after choosing the ATTiny13 from Tools > Board, go to File > Examples > ATTinyCore > Tiny13 > CommunicationJson Url for Board Manager URLs

author: sleemanj
author_rank: Super Contributor
timestamp: August 04, 2020, 12:39:50 am
content: Also to note that in my core using the Tiny13 the baud rate is locked based on processor speed selected9.6MHz will be 57600 Baud4.8MHz will be 9600 Baud1.2MHz will be 9600 Baudthe baud rate given to Serial.begin is ignored.

author: westfw
author_rank: Super Contributor
timestamp: August 05, 2020, 07:55:22 am
content: The reason you're having difficulty is that the ATtiny13 doesn't have a UART peripheral, so you have to use some variant of SoftwareSerial.Some of the cores for tiny13 will include that as "Serial" (with various limitations.) printing ... pretty much anything ... will take up a significant percentage of the Flash and RAM space available.

author: Rick Law
author_rank: Super Contributor
timestamp: August 12, 2020, 06:45:43 pm
content: Besides the mere 1K flash, the other constrain is the 64 byte ram. If you are using something like C to program it, you have to watch your stack carefully. When you call a function, you really have to trace the nesting (one function calling another function which calls another function...) 64 bytes can be used up very quickly with nested calls.I first upgraded to a ATTINY25 (2K flash, 128 byte ram), but eventually I went to top of that line: the ATTINY85 (8K flash, 512 byte ram). That extra resources saved me a lot of time.The chips are not the same size, but one can bend the ATTINY85's pin just a tiny bit and it fits on the solder pads sized for the ATTINY13.

author: westfw
author_rank: Super Contributor
timestamp: August 13, 2020, 01:43:57 am
content: If you're looking at new projects, you should probably look at the new Tiny-0 and Tiny-1 chips like the ATtiny402. 4x the memory of a Tiny13, USART, SPI, and I2C peripherals, and ... cheaper!(alas, no 8k chip to go up against the tiny85, yet.)

author: CyberBob
author_rank: Contributor
timestamp: August 13, 2020, 11:30:24 am
content: Thank you allSorry for not responding sooner but the notifications of your posts ended up in my Junk folder. I'll work my way through your suggestions and post an updateThanks again

author: CyberBob
author_rank: Contributor
timestamp: August 13, 2020, 03:44:15 pm
content: HiI tried the FTDI USB to TTL serial adapter, wired as follows: VCC to VCC, GND to GND, RX to pin 6 and TX to pin 5.If I removed the TX wire from pin 5 and put the RX pin into pin 5 I got something showing in the serial monitor, though just lots of symbols. I tried connecting the DTR attched to pin 1 but nothing happened. I am using the Arduino serial monitor. I must be missing something (apart from between my ears).I have the ATTiny 13A set up on a small breadboard attached to an Arduino Uno which acts as an ISP programmer. I have no problems uploading programs to the Tiny. I have another Arduino (well a few, actually) so I tried connecting this to the Tiny after I had uploaded the program (Menu example). I connected VCC to VCC, GND to GND, RX to pin 6 and TX to pin 5. I opened the Arduino serial monitor and didn't get anything. Is there an easy way of doing it this way (preferred option) and if so, have I got the wiring correct?Thanks for your help and patience

author: Rick Law
author_rank: Super Contributor
timestamp: August 13, 2020, 07:16:08 pm
content: At this point, my approach would be: put a scope to the ATTINY's Tx line to see what is coming out. Keep writing 'U' from the ATTINY, that would send 01010101 out plus a stop bit. (I am not highly experienced, so there is probably a ton of other better approaches.)If nothing is coming out from ATTINY's Tx line, that means (a) you are not writing anything to the Tx line correctly, or (b) your chip probably is sick. If 'U' is coming out, I would try another letter. I like '3' which should show 00110011, but of course you can choose any other on the ASCII table. If both 'U' and '3' are coming out, chances are, your software and hardware are transmitting just fine. The issue would be baud and proper connection.One confusing thing is, Rx of one connect to Tx of the other. The ATTINY's Tx line is what I would expect things you wrote from the ATTINY to come out, and that should got to the Rx of the FTDI. So make sure you got that correctly. My FTDI adapter would tolerate a reversed connection at least for a short time - I got them mix up every now and again. You can always say a prayer, cross your fingers, then swap Rx and Tx and see if you made that mistake.

author: sleemanj
author_rank: Super Contributor
timestamp: August 13, 2020, 11:28:40 pm
content: Quote from: CyberBob on August 13, 2020, 03:44:15 pmIncorrect baud rate or the internal oscillator is too far out of cal (the internal oscillator is very unreliable).As per my post, if you are using my core the baud is locked to the rates given, ensure you have selected the correct one in your arduino serial terminal. My core also has a helper example to help you find an OSCCAL value to calibrate the oscillator if necessary.With serial, RX on one end goes to TX on the other, ie, cross over.

author: CyberBob
author_rank: Contributor
timestamp: August 14, 2020, 03:11:08 pm
content: Thank you for your responses.I uploaded the OscillatorCallibration program from the Arduino IDEThese are my setting for the ATTiny 13ATools > Board : ATtiny13Tools > BOD : 2.7VTools > Clock : 9.6 MHzTools > Timing : Micros disabledI left the ATTiny 13A connected to the Arduino ISP and I opened the Arduino serial monitor at 115200 baud rate and started sending "x". After the second "x" sent I got the following15:53:39.449 -> ⸮⸮⸮I repeatedly sent "x" for a while and the output continued to be "⸮⸮⸮"Here is the wiring for my Arduino ISP:ATtiny13A Arduinopin 1 pin 10pin 5 pin 11pin 6 pin 12pin 7 pin 13pin 8 5vpin 4 Ground (GND)I continued to send "x" for at least 5 minutes but didn't get and readable text in the serial monitor.

author: CyberBob
author_rank: Contributor
timestamp: August 14, 2020, 04:11:38 pm
content: Connecting the ATTiny (pin6) RX and (pin5) TX to RX and TX respectively on a separate a Arduino Uno. I opened the serial monitor and set a baud rate of 115200. The following kept printing out continually without any interaction from me.⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮Eventaully the program stopped sending "⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮" and there was no response when I tried to send "x" (a total of fifty times!).Swapping the RX and TX connections(ie connecting pin6 of the ATTiny to TX of the Arduino and connecting pin5 to RX of the Arduino, nothing was displayed in the serial monitor. I sent "x" many times without any response.

author: CyberBob
author_rank: Contributor
timestamp: August 16, 2020, 12:43:34 pm
content: Can someone please help?Thanks

author: Rick Law
author_rank: Super Contributor
timestamp: August 16, 2020, 09:57:52 pm
content: Quote from: CyberBob on August 16, 2020, 12:43:34 pmYou said you had it set to 115200, butsleemanjnote that his core locks the Tiny13 baud rate based on clock rate.Unless you are not using his core, you should try setting your baud on both ends to match the baud that corresponds to your clock rate.Hope this helps.Quote from: sleemanj on August 04, 2020, 12:39:50 am

author: sleemanj
author_rank: Super Contributor
timestamp: August 16, 2020, 10:44:46 pm
content: Going by the options poster is not using my core115200 on a 9.6Mhz processor is probably pretty iffy, the uart calchttps://trolsoft.ru/en/uart-calcsuggests it'll be at 4.2% error (assuming an accurate clock)You should use an alternate baud rate, anything up to 33.6, 56k at a stretch.(NB: obviously above is for hardware uart which t13 don't have, software uart can only be as good or worse)

author: westfw
author_rank: Super Contributor
timestamp: August 16, 2020, 11:58:56 pm
content: QuoteSoftware UARTs can be more accurate than the HW UART, because they can be timed down to the single-clock level, rather than clock/8 or clock/16 for the HW USART. But only for certain speeds...

author: CyberBob
author_rank: Contributor
timestamp: August 17, 2020, 10:19:39 am
content: Thanks again for your responses. I'm new to the terminology here; sleemanj. when you mention your "core", do you mean your wiringhttps://raw.githubusercontent.com/sleemanj/ArduinoOrientedChipPinoutCreator/master/13.png?Is my ISP wired as required as I have successfully uploaded other sketches onto the ATTiny13. Here is the ISP wiring that I use:ATtiny13A Arduinopin 1 pin 10pin 5 pin 11pin 6 pin 12pin 7 pin 13pin 8 5vpin 4 Ground (GND)If this is correct, is my wiring for Serial correct, ie:ATTiny13A Arduinopin 1 pin 5pin 6 (RX) pin 1 (TX)pin 5 (TX) pin 0 (RX)Do I need to disconnect all of the ISP connections whilst monitoring the serial port.I have tried this at various baud rates with no success. I suppose it all depends on using your "core" which I need clarification on. Is your "core" the wiring or something else (excuse my ignorance).Thank you all againKeep safeRob

author: sleemanj
author_rank: Super Contributor
timestamp: August 17, 2020, 09:41:30 pm
content: No, my Arduino core (fork of ATTinyCore)https://github.com/sleemanj/optiboot/blob/master/dists/README.md

author: sleemanj
author_rank: Super Contributor
timestamp: August 17, 2020, 11:34:57 pm
content: Maybe I think it would be best to spell it out in steps for you1. Copy this URLhttps://raw.githubusercontent.com/sleemanj/optiboot/master/dists/package_gogo_diy_attiny_index.json2. Open the Arduino IDE and chooe File > Preferences locate the "Additional Boards Manager URLs" at the bottom of the window3. Click the window icon next to that field to edit it4. Paste the URL from 1 into this field on a new line and click OK, and OK to close the preferences5. Go to Tools > Board > Boards Manager6. Wait for the update download to finish7. In "Filter your search" type Tiny13 and choose to install the "DIY ATtiny by James Sleeman"8. Close the boards manager9. Choose Tools > Board > DIY ATtiny > ATtiny1310. Choose Tools > Burn Bootloader to set the fuses correctly if you are not certain they are correct11. Choose File > Examples > ATTinyCore > Tiny13 > Communication > ASCII Table12. Upload sketch13.Disconnect programmerand then connect the serial adapter - RX of the ATTiny connects to TX of your serial adapter, and TX of the ATTiny connects to RX of the adapter14. Open serial monitor and select 57600 baud

author: CyberBob
author_rank: Contributor
timestamp: August 18, 2020, 10:43:46 am
content: James, your a star!!I followed your step by step idiots guide and it worked. I think the problems I was having were with the original bootloader. Anyway the ASCII example worked. I noticed that when I tried it again an hour later, nothing showed in the serial monitor (strange). I put a simple program together just to test serial printing and that worked fine:void setup() {// put your setup code here, to run once:Serial.begin(57600);}void loop() {// put your main code here, to run repeatedly:Serial.println("Hello");delay(1000);}Now I'll be experimenting with some sensors that I have to see if I can serially print some analog data.I had a quick look at the examples (File > Examples > ATTinyCore > Tiny13 ) but the links to the wiring didn't work, I got the following error: Invalid upstream response (403)Most importantly you have ended my three weeks of frustration by solving my serial problem. Hopefully experimenting with analog and digital read and write as well as outputting to the serial monitor won't be as traumatic.Finally, is the ATTiny13 capable of outputting to an 0.96" i2C OLED ? If I could get that to work it would open up a whole new world of possibilities.Thank you so much for you patience and help.Take care and be safeRob

author: sleemanj
author_rank: Super Contributor
timestamp: August 18, 2020, 10:37:51 pm
content: I'm not aware of any Software I2C library ("Wire" library) that is compact enough to usefully fit in a Tiny13.Some years ago I did have a crack at getting "SoftWire" to fit and got it to the point it would fit, but it wouldn't have left any space to do anything with it.

author: westfw
author_rank: Super Contributor
timestamp: August 19, 2020, 09:03:11 am
content: Not to mention that the oled displays are usually bit mapped, and require some sort of font table to display text. That’s frequently a significant hit even on much bigger chips. (Many arduino folk with 32k flash find that oled+Ethernet or gps pretty much fills things up!)

author: CyberBob
author_rank: Contributor
timestamp: August 19, 2020, 11:05:51 am
content: Yes, as I thought not enough space for a library. Anyway I do have enough to be getting on with now thanks to you both.Thanks for your patience and help.; it is greatly appreciatedTake care and be safeRob

author: newbrain
author_rank: Super Contributor
timestamp: August 19, 2020, 01:55:33 pm
content: Quote from: westfw on August 19, 2020, 09:03:11 amMaybe they should ask for some coding help toFarbrausch, theyreally can cram a lot in 64k(not a serious comment, sorry).

author: CyberBob
author_rank: Contributor
timestamp: August 19, 2020, 03:12:53 pm
content: I knew it wouldn't be long.....I've adapted a couple of the core examples and they work well. I was trying out the "communication" examples and had no problem with the SerialDigital Clock. However, I ran the ASCIITable sketch and it worked first time but when I tried it again, nothing would print to the serial.With other two examples, ReadASCIIString and MenuDemo, I get the serial output just scrolling through and there is no response to me sending anything to the ATTiny. The hardware and my wiring haven't changed so it is a bit of a mystery to me.Any pointers would (again) be greatly appreciatedThanksRob

author: sleemanj
author_rank: Super Contributor
timestamp: August 19, 2020, 09:41:06 pm
content: No reason if it worked once it shouldn't work again, except if your timing is off due to thermal/voltage changeEnsure you are disconnecting programmer

author: Rick Law
author_rank: Super Contributor
timestamp: August 20, 2020, 02:33:01 am
content: Besides whatsleemanjalready said... Also make sure all your other wiring is connected well. Often during testing, temporary connections are made just for testing. They may not be holding as well as it should. That is my frequently encountered problem: temporary connections failing.

author: CyberBob
author_rank: Contributor
timestamp: August 21, 2020, 12:08:01 pm
content: Thank you for your responses.I program the Tiny on a dedicated programmer (small breadboard sitting an a shield, attached to an Arduino Uno). I then physically remove the ATtiny and put in onto a dedicated serial adapter (breadboard connected to a second Arduino Uno. I've replaced the wiring and I still have problems with the examples just causing scrolling in the serial monitor. Using my programmer and serial adapter as above, I have had lots success with using sensors and displaying data in the serial monitor.Talking of sensors, I am trying to use a HC-SR04 ultrasonic sensor with the ATTiny but there isn't enough room for the program; I get the following:Sketch uses 1798 bytes (175%) of program storage space. Maximum is 1024 bytes.Here's the sketch:const uint8_t echoPin = 3;const uint8_t trigPin = 4;long duration, distance;void setup() {Serial.begin (57600);pinMode(trigPin, OUTPUT);pinMode(echoPin, INPUT);}void loop() {digitalWrite(trigPin, LOW);delayMicroseconds(2);digitalWrite(trigPin, HIGH);delayMicroseconds(10);digitalWrite(trigPin, LOW);duration = pulseIn(echoPin, HIGH);distance = duration/58.2;Serial.println(distance);delay(500);}I don't suppose I'll ever be able to strip the code down enough to fit on the ATTiny 13Take care and be safeRob

author: Rick Law
author_rank: Super Contributor
timestamp: August 21, 2020, 06:38:30 pm
content: Quote from: CyberBob on August 21, 2020, 12:08:01 pmYou could try converting it to assembler, including library function calls.At 175%, the possibility of stripping down enough is low. Changing your own code is not going to make enough of a difference. Your big gain is going to be any library functions that is coded with too many options that you did not need (including even test tests for it) and you go strip that down.When you compile, have the output assembler listing turned on. So you can see exactly what codes converts to and from there you can get a gauge of where is the big gain.Alternatively, make an empty program with just one line of code. You can than see which line is the "expansive" line.The line "distance = duration/58.2;" for example likely added a huge amount of code right there calling in the floating point library. Do they really need to belong(vs int16)? That would reduce some, so forth.distance = duration/58.2; // done infloatingdistance = (duration*10)/582; // done inint (or long), but no roundingdistance = (((duration*100)/582)+5)/10; // done inint (or long), with an imperfect 1 digit roundingdistance = (((duration*1000)/582)+50)/100; // done inint (or long), with an imperfect 2 digit roundingYou will have to do that kind of analysis to each line of code. By the way, you also have to pay close attention to your ram usage. At 32 bytes, it get used up just like that.I got tired of doing that - I am a hobbyist and was aiming to have fun, not aiming to have frustrations. So for me, I switch to higher resource ones the Tiny 25, then 45, and eventually just "standardized" on Tiny 85 so I don't need to track so many different parts or utility libraries.EDIT: fixed parenthesis mismatch.

author: sleemanj
author_rank: Super Contributor
timestamp: August 21, 2020, 10:00:51 pm
content: Yep float math is a non-starter on tinys, the required code is hundreds of bytes. Integer approximations is the best you can do.Your continued serial problems sound like timing issues to me, either oscillator out, fuse incorrect, or mismatched baud rate between the tiny and whatever is on the other end.

author: Rick Law
author_rank: Super Contributor
timestamp: August 22, 2020, 12:32:55 am
content: The other probable low hanging fruit:serial.print(distance)..print()can covert values with formatting (precision specification), so it is likely it has codes you don't need.It would be particularly good if yourdurationanddistancecan beint16. If you must keepdurationalong, at least makedistanceanintif at all you can.You are usingserial.print(distance)to convert yourdistanceinto alphanumeric before writing. Youmaybe able to save a few bytes by doingitoa()converting the integer to alpha yourself and do aserial.write()to print it out. This avoidsprint()dragging in any unnecessary codes. Youmayeven be able to safe a bit more by doing theserial.putc()in a loop yourself.Lastly, standard Arduino startup & libraryshoulddefaultpinmode()to input. So you don't need apinmode()call if you are using it for input. You could eliminate the linepinMode(echoPin, INPUT);in yoursetup(). But do test that, you know, some versions of libraries/setup-codes works better than others. Trust but verify.Good luck, let us know how it goes.

author: sleemanj
author_rank: Super Contributor
timestamp: August 22, 2020, 12:33:51 am
content: I'd add also that in this use-case, dividing by 58 is totally fine. You are simply within the margin of error anyway there, your answer is still just +/- 1cm difference from the actual distanceLosing the floating point gets you inside the space available very easily.

author: CyberBob
author_rank: Contributor
timestamp: August 22, 2020, 07:00:33 am
content: Again, thank you for your responses.I had a brief play with the program :Changing distance from long to intSketch uses 1790 bytes (174%)Removing pinMode(echoPin, INPUT);Sketch uses 1780 bytes (173%)Changing distance = duration/58.2; todistance = duration/58;Sketch uses 1208 bytes (117%)...nearly there!As far as the scrolling serial output, this happens with all three ATTiny13s that I have. (Make that two as one has just lost a pin). I'll try burning the bootloader again and uploading the examples.Thanks againKeep safeRob.

author: sleemanj
author_rank: Super Contributor
timestamp: August 22, 2020, 07:24:50 am
content: Err, using my core with the only change to your code posted above being changing 58.2 to 58, as I say fits just fine with plenty of room to spare.Sketch uses 912 bytes (89%) of program storage space. Maximum is 1024 bytes.If you needed more space for something else, you could disable millis to save almost another hundred bytes.Other cores are not so optimised as mine.

author: CyberBob
author_rank: Contributor
timestamp: August 22, 2020, 03:58:57 pm
content: I thought that I was using your core.I have these in my additional board manager URLs under preferenceshttps://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.jsonhttp://arduino.esp8266.com/stable/package_esp8266com_index.jsonhttps://mcudude.github.io/MicroCore/package_MCUdude_MicroCore_index.jsonhttps://raw.githubusercontent.com/sleemanj/optiboot/master/dists/package_gogo_diy_atmega8_series_index.jsonSo when I burn the bootloader, will it use your core?ThanksRob

author: CyberBob
author_rank: Contributor
timestamp: August 22, 2020, 04:18:13 pm
content: Panic over!I went through the process of reinstalling your core DIY ATtiny to the latest available release in Board Manager. Now it the program fits on the ATtiny!Sketch uses 912 bytes (89%) of program storage space.Thanks you so much (again)Be safeRob

author: Rick Law
author_rank: Super Contributor
timestamp: August 22, 2020, 07:08:48 pm
content: Quote from: CyberBob on August 22, 2020, 04:18:13 pmGood going!!!!By the way, at 912bytes you actually have the room to do more exact math withintinstead of abandoning the 0.2 in 58.2 (reminder below). Just watch out for overflow.As you work with the TINY's, you would get the habit of using the least possible. No floating/long long/long unless absolutely needed, and do library calls with care...Quote from: Rick Law on August 21, 2020, 06:38:30 pm

author: CyberBob
author_rank: Contributor
timestamp: August 23, 2020, 12:03:51 pm
content: Thanks RickI'll be following your advice on this and other projects, especially abandoning the 0.2 in 58.2Thanks againKeep safeRob