Arduino Template Express – Using the ATTiny Micro-Controllers

Featured

The Arduino core libraries provide a well defined structure for developing code with the different micro-contollers it supports, but not every project requires the power provided by the ATMega 328 or 2560 chips. Simple projects can take advantage of the ATTiny family of micro-controllers. The goal of this post is to demonstrate how to use Arduino Template Express (ATE) to deploy the Arduino Core libraries to the ATTiny family of micro-controllers. This post will show you how to setup a development board with the ATTiny85 micro-controller and enable it with serial communication capabilities and the ability to use the Arduino Core libraries. In order to follow this tutorial, you will need the following:

  1. Atmel Studio 6.1 or greater
  2. Arduino Template Express (ATE) 1.0.2 or greater
  3. An ISP programmer – I will use AVRISP-MKII and AVR Dragon to demonstrate deploying code with either programmer. If you have an Arduino based ATTiny programmer, that is also fine.
  4. FTDI Breakout Board – The FTDI Basic Breakout from SparkFun or the FTDI Friend + extras from Adafruit will do the job. I will use FTDI Basic Breakout from SparkFun to allow the ATTiny85 to send serial information to a terminal. If you are creative and do not have access to a FTDI Breakout board, you can remove the ATMega328 chip from an Uno board and use the serial communication capabilities already included in the board as a substitute for the FTDI Breakout board.
  5. An ATTiny85 assembled on a breadboard or from a development board such as the one provided by Tinkerlog.

Although, I will use an ATTiny85, the instructions applies to any of the ATTiny micro-controller for which ATE provides a compiled Arduino core library. Figure 1, contains a list of supported micro-controllers.

List of supported libraries

Figure 1 – List of Supported Libraries and micro-controllers

Hardware Configuration

Let’s begin by configuring the development board. Figure 2, contains the schematic diagram of the minimum configuration needed to run an ATTiny85. This configuration uses the internal 8 MHZ clock to drive the micro-controller. In order to transfer data to the FTDI breakout board, pin 2 of the micro-controller is connected to the TX pin of the FTDI breakout board. The 5V supplied by the FTDI breakout board on pin 3 provide power to the micro-controller.  Jumper JP1 is used to enable/disable resetting the micro-controller when a terminal connection is made to the FTDI breakout board.  Jumper JP1 should be open when using the AVR Dragon to debug sketches.  If connected, the capacitor C3 causes side effects while stepping through the code.  By the way, having this jumper on the original Uno board would be a welcome feature.   Note the CTS pin of the FTDI breakout board is connected to ground to prevent the FTDI chip from stopping the transmission.

Figure 2 - ATTINY85 Breadboard Diagram

Figure 2 – ATTINY85 Breadboard Diagram

Figure 3 contains a breadboard implementation of the design. Note the usage of breadboard helpers to speed up the assembly of the circuit.

ATTiny85 Breadboard Implemenation

Figure 3 – ATTiny85 Breadboard Implementation

The FTDI breakout board comes in 5V and 3.3V flavors.  Breakout boards with more advance features have a jumper that controls the voltage output and can be switched between 3.3V and 5V.  If you happen to have a 3.3V FTDI breakout board, you will need to introduce an external power supply to drive the circuit.  Figure 4 contains a circuit diagram that uses an external power supply to provide power to the micro-controller.  Note the 3.3V pin of the FTDI breakout board is not connected to the micro-controller.

Figure 4 - ATTINY85 Breadboard diagram with dedicated power source

Figure 4 – ATTINY85 Breadboard diagram with dedicated power source

The circuits illustrated in figure 2 and 4 are functionally equivalent, the only difference is how the micro-controller is powered. It is important to note that in order for the ISP programmer to work, the micro-controller needs to be connected to an independent power source. Both, the 5V provided by the FTDI breakout board or an external power supply will satisfy this requirement.  Before proceeding connect the ISP programmer to the ISP header (J1) and configure the micro-controller fuses. Table 1 contains a list of the values of the different fuses that need to be programmed.
[table “22” not found /]

If you need information abut AVR fuses check Lady Ada tutorial on her site. Figure 5 illustrates the Atmel Studio screen from where the fuses are programmed. See (Tools/Device Programming) or (Ctrl+Shift+P).

Figure 5 - ATTINY85 Fuses as seen by Atmel Studio

Figure 5 – ATTINY85 Fuses as seen by Atmel Studio

At this point we have a development board assembled in a breadboard. The following step is to register the board in Arduino Template Express so the wizard can pick it up when new projects are created.

How ATE Supports Multiple Boards

ATE uses the concept of a development board as an abstraction of the environment for which a developer creates code. Figure 6, illustrates an entity diagram of a development board.

ATE Entity Diagram

Figure 6 – ATE Entity Diagram

Each development board has a series of properties that identify the board.  A programmer is is a logical construct that includes the port used to program the board and the application responsible for transferring the sketch created by a developer.   ATE comes configured with 17 defined boards, 8 of which can be activated during installation. An activated board shows in the list of available boards when the wizard runs to create a new project. ATE supports an unlimited number of boards that can be manually added or activated after installation. In order to enable the board ArduinoTiny85 follow the following instructions:

  1. Open the file boards.xml with your favorite editor. I would recommend Notepad++ if you do not have a favorite editor already.
  2. Find the board “ArduinoTiny85”, around line 247
  3. Change the property <EnableForProjects> to true, it is false by default. This is the flag that tells the ATE wizard to include the board as an option.
  4. Change the programmer to AVRDRAGONISP-AT.  Since I am using AVR Dragon and will use the ISP header and the atprogram.exe provided by Atmel.  If you are using another programmer, find a suitable entry or create one.  Figure 7 illustrates a modified record.
  5. Configure the DataPort field to reflect the Com Port that is created when you connect the FTDI Breakout Board.  Check the Device Manager list of Com Ports if necessary.
  6. Last, locate the programmer entry AVRDRAGONISP-AT and make sure the ComPort property is set to avrdragon and the protocol to ISP.  ATE 1.0.2 beta was shipped with the wrong port/protocol combination.  This will be changed in the final release.
Editing boards.xml

Figure 7 – Editing boards.xml

At this point we have a development board configured and registered.  Let’s proceed to create a hello world sketch.

Creating Hello World

Start by launching Atmel Studio, selecting new project, and select Arduino Sketch, see figure 8.  Once you press OK the ATE Wizard will start.

atmel-new-project

Figure 8 – New Project Dialog

 Figure 9 illustrate what the wizard will show you.  The boards will vary based on what you have installed, but the ArduinoTiny85 should be in the list.  Select ArduinoTiny85 and also select 8 Mhz from the supported speed drop down.  Press OK and a new project will be created tailored to the Attiny85.

ate-wizard-arduinotiny85

Figure 9 – ATE Wizard with Attiny85 Board

Compile the program and you will get two errors and 4 warnings.  The errors are related to the fact the Arduino Serial library is not supported by the ATTiny85, that’s OK, let’s get rid off all Serial references and concentrate first on making the LED blink.  We’ll introduce a Serial library replacement later.  Comment all lines referencing Serial in main.cpp and Utility\buildinfo.cpp.  Do not delete the lines, just comment them.  At this point you should be able to compile the code and receive 4 warnings, but the code will compile successfully.  Make sure your board has power and the programmer is connected to the ISP header.  Press Deploy in Atmel Studio and see the LED start flashing.  If this the LED does not flash, retrace your steps to verify everything is connected properly.  It is important to note the code is Arduino friendly and you can move to any Ardunio board for which the core libraries are compiled.  This means you are capitalizing on your investment to learn and use the Arduino core library.  Now the LED is flashing and the code can be successfully deployed, let’s take care of the Serial connection.

Enabling Serial Connection

The TinyDebugSerial library provided as open source by Rowdy Dog Software is an implementation compatible with the Serial library provided by Arduino.  It only uses one pin to transmit and does not provide data receive capabilities.  Download TinyDebugSerial.cpp, TinyDebugSerial.h, TinyDebugSerial9600.cpp and core_build_options.h.  Include the “TinyDebugSerial.h” in main.cpp and “..\TinyDebugSerial.h” in BuildInfo.cpp.  Remember to remove the comments on the lines of code referencing Serial.  Recompile and deploy the code.  Open a terminal and connect to the proper COM Port and you will see output coming from the Attiny85.  If you do not want to deal with all of this, a fully functional sketch is included in the link below.  Download, compile and deploy.  If you have configured the ArduinoAttiny85 properly in your board, the deployment script will pick all your local parameters and deploy successfully.

Download Source Code: Sketch85

Arduino Template Express – Creating a Library

In previous posts I discussed How to Install Arduino Template Express and How to Create a Sketch.  In this post let’s discuss how to create a library using Arduino Template Express and what are the advantages and disadvantages of partitioning your code into libraries.  Let’s go!

Divide and Conquer

In order to understand the benefits of libraries, let’s begin by creating the same hello world sketch we created in the How to Create a Sketch post.  Follow those instructions and come back when you have a working sketch in place. Let’s take a look at the main.cpp file to discuss what is going on in the sketch.  See Listing 1.

#ifndef SKETCH_H_
#include "Sketch.h"
#endif
/* Running counter */
static unsigned long counter = 0;
//#pragma message "Sketch Running At: " XSTR(F_CPU) " HZ"
void setup()
{
/* Setup pin connected to board LED for output */
pinMode(LED, OUTPUT);
/* Initialize serial interface */
Serial.begin(SERIAL_SPEED);
/* Show build information via serial interface */
#if defined(ENABLE_TEXT_MESSAGE)
ShowBuildInfo();
#endif
}
void loop()
{
digitalWrite(LED, HIGH);
delay(DELAY_LENGTH);
digitalWrite(LED, LOW);
delay(DELAY_LENGTH);
Serial.print(MESSAGE);
Serial.println( ++counter );
}

Listing 1 – Hello World Sketch

This is a monolithic sketch.  Everything it does is packed into one single development unit.  The sketch uses the Serial library and functions from the core Arduino library, but the functionality of flashing the LED and sending notifications via the UART are packaged in a single program.  As the code stands it is truly a one developer job and contains the following structural (not functional) deficiencies.

  • Unit Testing this code is very difficult.  At best you will be able to verify the LED goes on an off, but testing for LED initial conditions and controlling the timer is very difficult.
  • Having multiple developers work in this program is not possible.  Everything is packed into a single unit of work (main.cpp).
  • Reusing any code of value you develop in this sketch is not possible.  At best you will be able to use clipboard inheritance (copy and paste for the object oriented challenged).
  • Replacing the on board LED by an external LED driven from a II2C/SPI chip requires a great deal of changes because main.cpp is operating too close to the metal by invoking digitatWrite directly.

Let’s visualize how a library can help with our design by introducing a layer of abstraction.  If this was production code, I would make sure there is also a layer of abstraction for the serial communication and for the timer.  For the purpose of this example I will concentrate on the LED.  The sketch main job is manipulating the state (ON|OFF) of a pin by calling digitalWrite().  Let’s introduce the concept of a Led class with two functions on and off.  Figure 1 illustrates the class diagram of the ILed interface and possible implementations.

class-diagram-led

Figure 1

Programming to the ILed interface allows swapping different implementation transparently at compile or run time.  Instead of calling digitalWrite directly the sketch will invoke ILed.on() or ILed.off() to turn the LED on or off.  What are the advantages and disadvantages of introducing this level of abstraction?  I am glad you asked.  Let’s start with the advantages.

  • Programming to an interface promotes code reuse and in this case allows the design of the sketch to evolve to support an Led directly attached to the micro controller or attached to an external component with which the micro controller can communicate.  Besides SPI or II2C you could turn on and off an LED via a XBee,  WiFi, or radio interface.  The possibilities are multiple as long as you implement the ILed interface.
  • Unit Testing an ILed implementation is possible and independent of the main code.  Once an implementation is tested it can be shared and reuse in binary form with a high degree of confidence.  This promotes good design practices such as the Open/Closed principle.
  • Multiple developers can work now on this sketch.  One in the main.cpp sketch and the others in the implementation of the Led, SPILed, II2CLed classes.  The developer responsible for main.cpp will integrate to the different implementation as they become available.  The size of this sketch does not warrant such a division of labor, but in a the context of a bigger project, this option is a plus.
  • The code becomes more cohesive, a desirable trait, this benefits a few “ilities” such as maintainability, reliability, reusability, and understandability.  See Cohesion Metrics for Predicting Maintainability of Service-Oriented Software and Software Design Metrics for Predicting Maintainability of Service-Oriented Software.

Not all is good news, the introduction of ILed in the code has the following disadvantages:

  • After introducing ILed and the Led implementation, the size of the sketch is bigger.  The introduction of abstract classes (the interface of the class diagram) carries a virtual table that takes more memory than the direct digitalWrite approach.  In fact according to avr-size, the primary memory usage of the release version of the sketch goes from 3,176 bytes (9.7%) to 3,676 bytes (11.2%), while the data usage goes from 267 bytes (13%) to 296 bytes (14.5%).  This increment in size is not related to using a library, but to the introduction of ILed into the design.
  • The solution is more complex.  There are more moving parts and more code to develop in order to provide an abstraction layer for the LED.

Let’s refactor the code to introduce a library.  To add a library project to the solution, right-click on the solution and select add/project.  The new project dialog appears.  See Figure 2.  Select Arduino Library Wizard, provide the name Led and select the default location to save it.  Follow the wizard and select the same development board you previously selected for the sketch.  This will create a new library project named Led in your existing solution.

add-new-project-dlg-with-lib-selected

Figure 2 – Adding new library

You can download the full solution with the sketch and the library at the end of the post.  All paths for the sketch to find the library have been added using relative paths.  This is a manual step that I expected Atmel Studio to take care of when I added the library as a reference to the sketch, but unfortunately it did not happen.  See Listing 2 for the new sketch code.

#include

/* Running counter */
static unsigned long counter = 0;

/* Micro-controller connected LED */
Led led;

/***************************************************
Return LED as ILed to isolate caller from what
type of ILed is used
****************************************************/
ILed* getLed()
{
	return &led;
}

 void setup()
 {
	 /* Initialize LED */
	 led.Init(LED);

	 /* Initialize serial interface */
	 Serial.begin(SERIAL_SPEED);

	 /* Show build information via serial interface */
#if defined(ENABLE_TEXT_MESSAGE)
	ShowBuildInfo();
#endif

 }

 void loop()
 {
	 getLed()->On();
	 delay(DELAY_LENGTH);
	 getLed()->Off();
	 delay(DELAY_LENGTH);
	 Serial.print(MESSAGE);
	 Serial.println( ++counter );
 }
 

Listing 2 – Hello World Sketch with ILed

Line 35 and 37 take care of turning the LED on and off.  Reading the code makes it clear you are dealing with an LED.  I could have created a global ILed pointer and initialize it during setup(). The getLed() function provides an opportunity for putting logic that may switch at run time the type of LED the sketch controls.  The source code for the solution with both projects can be downloaded from here.

For Arduino developers making the transition to Atmel Studio, I hope this post has given you an introduction to how to use libraries in your projects. Coming from the Arduino IDE, an environment that promotes code reuse as source code, hides the true advantages of using libraries to decouple functional areas of an application.

Arduino Template Express – Creating a Sketch

In this post we’ll go over creating a sketch with Arduino Template Express.  I assume you have followed the instructions of how to install ATE from my previous post.  Launch Atmel Studio and select File\New\Project or Ctrl-Shift-N.

atmel-new-project

Select Arduino Sketch Wizard, give it a proper name and location and hit OK.  The wizard will be launched with the list of development boards you have configured during installation  or after installation by modifying the boards.xml file.

ate-wizard

I will use the Uno board for this example.  Pick an appropriate board from your setup and hit OK.  Atmel Studio will render a new project with the following layout.

ate-solution-explorer

This sketch flashes the on board LED and sends a running counter via the serial port.  Compile the sketch and  take a look at the output window.

ate-output

The sketch is compiled and as part of the compilation the template keeps track of how many successful and failed compilations have taken place.  It also keeps track of how many successful/failed deployments occur.  Let’s deploy this puppy.  Select Tool\Deploy or press deploy in the tool bar if you have it configured.  The output window should display the outcome of the deployment.  If you launch a terminal window and connect it to your development board data port, you will see a running counter every time the LED flashes.

Additional Goodies Included Provided by ATE

  • Take a look at Utility\BuildDate.cpp.  This file gets rebuilt every time the sketch is compiled.  It generates the time stamp of the last build and you can store it in ram, EEMEM or PROGMEM.  You can optionally use this information by calling GetBuildDate().
  • Utility\BuildInfo.cpp has ShowBuildInfo() to output build information to the Serial port.
  • Utility\BuildNumber.cpp keeps track of how many successful deployments has been made.  You can optionally use this information to keep track of your deployments.

That’s all for now.  In my next post we will go over creating libraries with ATE.

Arduino Template Express – Installation

The Arduino ecosystem consist of an IDE, development boards, development libraries and a vibrant and engaging community involved in the support and expansion of all of the components that make the ecosystem.  I acknowledge the pedagogical benefits of the Arduino IDE but if you are already used to Atmel Studio, the Arduino IDE feels like developing with training wheels.  The Arduino Template Express (ATE) enables Atmel Studio to create sketches using Arduino libraries and development boards.  AVR developers can take the best of the Arduino and continue using Atmel Studio and all the benefits it provides.  This post will walk you through the installation of ATE.

Arduino Template Express Configuration

In order to install ATE you must have already installed Atmel Studio 6.1 and Arduino 1.0.4 or higher.  Both of these programs must be installed and running successfully.  Download ATE from the Arduino Template Express site at CodePlex.  Before running setup make sure you close all instances of Atmel Studio that may be running, run atesetup.exe and agree to let the program run with administrative rights.  The following screens will guide you to the setup process.  As of the writing of this post version 1.0.2 (Beta 3) is available for download.

ate-screen1

Press Next to get to the next screen

ate-screen2

Press “I Agree” to accept the license and move to the next screen.

ate-screen3

The atesetup.exe program will install pre-compiled Arduino libraries, project templates and a wizard to drive the initial configuration of sketches.  Click Next to continue.

ate-screen4

This screen let’s you select the directories where Atmel Studio accepts new templates and the location where you want to store the application.  It is advisable to install it in your documents\Arduino Template Express folder.  The installer will try to pre-populate the directories for you.  Click Next to continue

ate-screen5

The following screen will attempt selects the location of AVRDUDE and its configuration file also the path for atprogram.exe distributed with Atmel Studio.  Note that as of Atmel Studio 6.1 the path of atprogram.exe has changes and now it is under the atbackend directory.  The installer will try to find these programs for you, but if does not, you will need to provide their paths before continuing.  Press Next to continue.

ate-screen6

The Board Selection screen allows you to select up to 8 boards you already own.  Do not worry, if you have more than 8 boards.  After the installation is completed you can add as many boards as you want to the boards.xml file.  In my case I have 3 development boards, an Uno, Mega2560 and Leonardo.  Note the Leonardo board requires two different ports, one for data and the other for programming.  Once you have defined all your boards, press Next to continue.

ate-screen7

The External Tool configuration configures the option to deploy sketches from within Atmel Studio. The option is configured in the Tools menu and it is recommended that you configure the Deploy option as part of one of the tool bars in order to enable one click deployment.  Your screen may be a little different depending on the number of external tools you may have already installed in Atmel Studio.  Click Next to continue.

ate-screen8

The About to Install screen gives you a list of what is going to be installed.  Click Install to continue and after a little while the end of the installation screen will show giving you the option to launch the readme file or go to the web site to get additional information.  I suggest you take a quick look at the readme file.

ate-screen9

That’s all.  Hit close and the installation is completed.  If you forget where is the program installed, open a command window and type set | find “ATE_HOME” to find the path where the ATE was installed.  Next post I will walk you through the generation of a sketch that blinks the on board LED and uses the serial port.