Programming ATtiny85 from Arduino Nano - The Porting Process
In the previous post in this series, we prepared the ATtiny85 by installing the bootloader. Existing sketches require some work to adjust them for use on ATtiny85. In ICT it is common practice to adapt applications written for a certain platform, so they can run on another platform. Often you will see an application with versions for Linux, Microsoft Windows and Apple macOS. The process of making an application suitable for a different platform is called porting. On a smaller level, Arduino sketches, created on e.g. Uno might not automatically run on an ATtiny85. You also need to apply a porting process and create a sketch version suitable for the ATtiny platform.
DISCLAIMER
THIS BLOG POST IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THIS HOWTO OR SOFTWARE OR THE USE OR OTHER DEALINGS IN THIS BLOG POST.
Differences
Size
There are some obvious differences between the commonly used Arduino boards and the ATtiny85. The first thing to notice is the size difference. This affects many features, e.g.:
- Memory
- Pins
- UART
- Libraries
Memory
The memory of the ATtiny85 is smaller compared to other boards. This means that large sketches might not fit. Compilation will halt if you exceed the available space. Minimize the amount of source code used to achieve your goal.
Pins
With 8 pins, the ATtiny85 has a lot less connection points. This limits the amount of pins you can address in your sketch.
UART
Arduino boards have at least one serial port and often use pin 0 and 1 for serial communication. UART is not available on ATtiny chips. Instead of Serial you have to use SoftwareSerial, which comes with its own limitations. This makes it difficult to monitor what is going on inside the program at runtime. You could print debug messages with SoftwareSerial to dedicated RX/TX pins. And then use a separte Uno board, connected to these pins to read the messages and display them via Arduino GUI's serial monitor. But that means having to sacrifice 2 pins of ATtiny for monitoring.
Libraries
For connected sensors on output devices, you may need a dedicated ATtiny version of the library. E.g. for LCD screens, you can use LiquidCrystal_I2C on Arduino boards. This won't run on ATtiny chips. You need an alternative. e.g. the TinyLiquidCrystal_I2C library.
Examples
Pin Example
Commonly you need to assign different pin numbers for the ATtiny platform.
The LED_BULLETIN pin (13) used in Blink.ino is not available on ATtiny85. Obviously it only has 8 physical pins, so pin 13 doesn't exist. And there are no on-board LEDs either. If you want to use Blink.ino on an ATtiny85, you need to adapt the pin numbers to address an external LED on an existing pin. To make it more complicated, the pin numbers you address in your sketch are mapped pin numbers that are different from the physical pin numbers. In the ATtiny85 datasheet you can find the pinout and the mapped numbers.
You can address pins from group PB in your sketch. In this example, the mapped pin 3 (PB3) is used in the sketch. An updated Blink.ino might look like this:
#define LED_PIN 3 // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 3 as an output. pinMode(LED_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }We will use this code in the next post and upload it to the ATtiny85.
The wire to the LED needs to be connected to the physical pin 2.
LCD and Sensor Example
If you want to connect a DHT11 temperature and humidity sensor and a LCD screen to an ATtiny85, you need to use libraries suited for the ATtiny platform. Instead of using the libraries LiquidCrystal_I2C and DHT, you now need to include ATtiny versions:
#include <TinyLiquidCrystal_I2C.h> #include "TinyDHT.h" DHT dht(DHTPIN, DHTTYPE); TinyLiquidCrystal_I2C lcd = TinyLiquidCrystal_I2C(0x27,16,2);
Not all functions may be available in the tiny versions of the libraries. E.g. TinyDHT doesn´t have the computeHeatIndex function.