0. Microcontrollers and Arduino.

0.1

Over the las few years, projects such as Arduino have smoothed the access to electronics and consequently enabled many hobbyists, students and many other people that didn’t have the prior need of gaining an insight into the technical intricacies in order to build an “intelligent device” that interacts with the environment, a device that will require the use of a microcontroller.

Arduino is a board based on an 8-bit microcontroller from Atmel plus all the electronic components required by the microcontroller to operate and communicate with its external environment. It is supported by a programming environment and library set which offer a microcontroller specific hardware abstraction, so that the developer doesn’t require to know the specifics of the microcontroller to program a particular task.

Arduino Leonardo

Arduino Leonardo

Arduino is an excellent tool for those with virtually any experience in electronics who want to make some “intelligent stuff”, and/or to get started in electronics in a quick and easy way. Once someone gets to know what Arduino has to offer and want to go a step beyond that because it wants to get the most of the microcontroller or if learning electronics is the main focus, probably the right choice would be to go bare metal, interface with the microcontroller directly and leave the level of abstraction provided by the Arduino libraries behind.

Instead, the route of going with an 8-bit microcontroller from Atmel from its own integrated development environment, and with a c compiler, doesn’t turn out to be much more complex than the Arduino IDE for someone who wants to learn electronics, and the advantages over using Arduino boards and its libraries are numerous.

If we learn how to program an Atmel microcontroller without Arduino libraries, we will have the ability to customize and write more efficient code for our task, making the most of the limited resources of our microcontroller. And also we wouldn’t need a bootloader to eat up memory space.

We could use a professional programming environment that Atmel offers absolutely free of charge for any of their microcontroller families, and most importantly, we will be learning how to use that family. That way we will have the freedom to choose what microcontroller is better suited to our application without the need to stick with an Arduino board which could be suboptimal to our project, having this way a drastic cut in the cost and providing a custom designed solution.

As an supporting example of everything mentioned so far, we are going to compare a “Hello World” embedded example developed in Arduino versus a “Hello World” in another 8 bit microcontroller but using a professional approach. We are going to have a look at the code needed in each case to toggle a digital output pin and we will evaluate the result produced in the Arduino and Atmel’s development environment.

As a 8 bit microcontroller we are going to use what is in ecTiny841, a prototyping board with a Attiny841. An Atmel 8 bit microcontroller with 14 GPIOs and a decent number of peripherals, considering also its affordability the micocontroller becomes very attractive as a potential option for many projects.

ecTiny841

ecTiny841

We will start with the Arduino Leonardo, the code required to toggle endlessly one of its digital pins at the maximum rate is given below, we can open the Arduino IDE and type the following code:

int outPin = 7;
 
void setup() {
  // Configure the microcontroller pin as an output
  pinMode(outPin, OUTPUT);
}
 
void loop() {
  //Set to 1 and reset to 0 the output pin
  digitalWrite(outPin, HIGH);
  digitalWrite(outPin, LOW);
}

In order to toggle the pin as fast as the Arduino can cope we just need to indicate what pin will be used for the purpose, configure it as an output and switch it on and back on in an endless loop given the libraries provided by the Arduino library.

Let’s observe the result in a oscilloscope connected to the relevant pin in the Arduino Leonardo running the former code, the pin transitions from ON (5V) back to OFF (0V) at a rate of 98.000 times every second.

Arduino Leonardo. Hello world! output pin. Frequency 98 KHz.

Arduino Leonardo. Hello world! output pin. Frequency 98 KHz.

This time we are going to run the equivalent in the Atmel microcontroller running in our ecTiny841. We are going to write the following lines in Atmel’s IDE so that we replicate the desired behavior by: configuring the selected pin as an output and then we toggle it in an endless loop as fast at the microcontroller could possibly execute it.

#include <avr/io.h>
#define F_CPU 16000000UL
 
#define outPin PORTA0
 
int main(void)
{
    // Configure the microcontroller pin as an output
    DDRA = 0x01; 
 
    while(1)
    {	
        //Set to 1 and reset to 0 the output pin
       	PORTA |= (1<<outPin);  
    	PORTA &= ~(1<<outPin); 		
    }
}

As it can be appreciated, the code is very straight forward as it was with the Arduino and with a few instructions we can achieve the same task in this case. The big difference this time is that we didn’t have to use any third party libraries to configure and turn the pin on and off, we have instead interface with the relevant microcontroller register dedicated to the output pin all written in C.

As we did before, if we connect the oscilloscope to the output pin we will get a graphical representation of the solution:

ecTiny841. Hello world! output pin. Frequency 2.66 MHz.

ecTiny841. Hello world! output pin. Frequency 2.66 MHz.

As it happened with Arduino the result is as expected, having the pin switching between high (5V) and low (0) back and forth, nonetheless the difference in rate is significant this time it is switching 2.66 Million of times per second (f = 2.66 MHz) vs 98 thousand times every second (f = 98 Khz).

Both microcontrollers have a crystal of 16 Mhz. Therefore the improvement that we could get in performance given the choice of using an Atmel microcontroller with no Arduino libraries to toggle a pin will be 27 times faster than having choosen Arduino for that task, and not only the code executes faster but it also uses less memory.

What will make a fundamental difference in how an embedded solution gets developed is that instead of following the Arduino route with its own libraries we will need to learn how the microcontroller works getting down to its own registers.

Learning how to use the 8-bit microcontroller from Atmel is easy, not much more difficult than an Arduino specially for someone genuinely willing to learn about electronics, and who wants to learn to make the most of every limited resource that the microcontroller has to offer to unveil its all potential for your own projects, not to mention the satisfaction of doing so…

If you are interested in learning how to use an Atmel microcontroller your can read further and check the rest of tutorials in this page, don’t forget to have a browse and consider the low cost custom build hardware that we have designed at eleCrab to seamlessly complete the different tutorials.

Related links to this post: