Serial interface for ATTiny that allows you to print different data types to an arduino's serial monitor. Doesn't require any extra serial cables/devices. This uses SoftwareSerial so it's not all too lightweight.
These instructions assume you are already able to upload code to your ATTiny using the Arduino IDE. For a good tutorial on how to do that check out this.
The library has to be downloaded as three separate libraries.
- Download this zip file (also available in the releases tab).
- Unzip this file. Inside are three more zip files.
- For each of these zip files, do the following:
- Using Arduino IDE 1.0.x:
- Sketch > Import Library... > Add Library... > select the zip file > Open
- Using Arduino IDE 1.5+:
- Sketch > Include Library > Add ZIP Library... > select the zip file > Open
- Using Arduino IDE 1.0.x:
- For each of these zip files, do the following:
After doing this, when you go to your list of libraries (Sketch> Include Library), you should see the following libraries included:
ArduinoSerialIn
SerialTypes
TinySerialOut
- The code uploaded to your Tiny will allow you to write out data
- You'll also have to upload some code to your Arduino to be able to print out incoming data from the Tiny to the Serial Monitor
- Navigate to: File > Examples > ArduinoSerialIn > ArduinoSerialInCode.
- Upload this code to your Arduino.
If all you are doing with this arduino is serial monitoring, you won't have to change this code.
- Upload code to your Tiny
- To make sure Serial is working and to see examples of how to use it, you can upload one of the following to your tiny:
- File > Examples > TinySerialOut > ExplicitWrites
- File > Examples > TinySerialOut > ImplicitWrites
- To make sure Serial is working and to see examples of how to use it, you can upload one of the following to your tiny:
For more details on using these functions see TinySerialOut Details below.
Once you've uploaded your code to your Tiny, and the provided code to your arduino, you'll need to connect the two.
You'll want to hook up your designated TX pin on your Tiny to the Arduino's RX Pin with a 330 ohm resistor in series. Then hook up the Tiny's Voltage pin to 5V and GND to GND.
The schematic shown uses the same TX pin on the Tiny as used in the included examples:
- This library uses one way communication (Tiny -> Arduino), which is why it only needs one pin on the Tiny for serial communication.
Once you've uploaded the correct code to your arduino as described in Setting Up the Arduino, and you've uploaded code to your Tiny that contains some serial output, and wired everything together:
- Power on the Arduino
- Make sure that you have the port (Tools > Port) corresponding to the Arduino selected
- Wait a second or two. if you open up the Serial Monitor straight away, you may be met with no output. If you then try to close it and open it again, most likely you'll get bad values, and you'll have to power off and power back on your arduino.
- Open up the Serial Monitor and read incoming data from the Tiny!
For example, here's what you should see on the Serial Monitor if you've uploaded the "ImplicitWrites" example to your Tiny:
To write serial data from your Tiny include TinySerialOut.h
in the code that you upload to your Tiny:
- Create a new TinySerialOut object.
- Pass in to the constructor the digital pin on the Tiny you are using for serial output.
- Note that this is not the hardware pin, the number you should use in code should correspond to the light-blue numbers below:
The TinySerialOut object contains two groups of functions to use.
The first allows you explicitly state what type of value you are sending:
writeBool
writeChar
writeInt8
writeUnsignedInt8 //same as arduino's byte
writeInt16 //same as arduino's int
writeUnsignedInt16
writeInt32 //same as arduino's long
writeUnsignedInt32
writeFloat
writeArray
writeString //a char array, not the Arduino String object
The second just uses an overloaded write(...)
function, which allows for many different data types, namely:
int16_t (int)
int32_t (long)
char array (will be output as a string)
bool
char
The Syntax for writeArray is a bit different than the other functions. It is:
writeArray(void * array, SerialTypes::Type type, uint32_t length, bool addFormatting)
- The first parameter is the array
- The second is the type of the element that makes up the array. This is passed in as a value in terms of the SerialType
Type
enum, whose members include:
BOOL
CHAR
INT8
UINT8
INT16
UINT16
INT32
UINT32
FLOAT
-
The Third parameter is the length of the array in # of elements (not total byte size)
-
The fourth parameter is whether or not you want formatting.
- A formatted array will be printed out as: "[elem1, elem2, elem3]"
- An unformatted array will be printed out as: "elem1elem2elem3"
For example:
int16_t array[] = {1,2,3,4,5};
writeArray(array, SerialTypes::INT16, 5, true);
output: "[1,2,3,4,5]"
The writeString method makes use of the writeArray method. Calling:
char message[] = "hello";
SerialOut.writeString(message, sizeof(message));
is the exact same as:
char message[] = "hello";
SerialOut.writeArray(message, SerialType::CHAR, sizeof(message), false);
which is also the same as:
char message[] = "hello";
SerialOut.write(message, sizeof(message));
sizeof
only works in this instance because chars are one byte.
You may also find it useful to define a macro such as:
//assuming you named your TinySerialOut object "SerialOut"
#define STRING_WRITE(str) (SerialOut.write(str, sizeof(str)))
So that in your code you can do something like:
STRING_WRITE("hello");
The library automatically adds in an 100 ms delay after each message. I found this alleviates
timing issues that occur when sending multiple messages quickly (in a loop for instance). If you'd like to get rid of this you can open up the TinySerialOut.h
file and comment out:
#define USE_DELAY
You can also try changing the length of this delay by changing the value defined at:
#define DELAY 100
Depending on your program, you may run into some issues if you change these values.
Most likely your compiler will optimize away any uncalled functions. But just in case, you can remove them yourself as well. This may or may not save some space on the code uploaded to your Tiny.
Provided in the TinySerialOut.h
file are a series of defines that can be commented out to explicitly remove certain functions from compilation.
For example if you know you won't be outputting any boolean values, you can comment out:
#define USE_BOOL`
and both the writeBool(bool out)
function, as well as the write(bool out)
function will be disabled.
You can also comment out:
#define USE_SHORTHAND_FUNCTIONS
which will remove all the write(...)
functions.