When is an Arduino not an Arduino?

17 views
Skip to first unread message

kal9001

unread,
Oct 17, 2014, 6:58:11 PM10/17/14
to barns...@googlegroups.com
So I figure most people in our merry little band have an idea what the Arduino is. But to clarify for those who may not be so sure, It is a combination of three things:
A hardware platform which uses Atmel AVR microcontrollers alongside a hardware USB interface controller which allows the thing to easily connect to a PC's UB port.
A The Arduino Bootloader firmware on the Microcontroller that allows the chip to be easily programmed via the USB interface.
And the ArduinoIDE software platform that makes creating and editing code much easier than it would be.

The Arduino has seen major success over the years with many MANY people making a vast array of wonderful things. However most people seem unaware that the Microcontroller at the heart of the Arduino can be used on its own without the Arduino Firmware or the Software.

It has been my desire for a while now to progress into being able to program the AVR microcontrollers without using Arduino. The great thing about the way it was developed is that you can actually write generic AVR C/C++ code right into the ArduinoIDE alongside the usuall Arduino commands and functions.
This allows a sort of Hybrid code to be developed that shares the ease of Arduino along with the efficiency of C/C++

One example of this that is probably the most well known, The Arduino digitalWrite() function is slow....Very slow infact, The reason is because the digitalWrite() actually does a lot of checking and calculation in the background before it actually writes the pin. these overheads are there to allow the same language to be portable across the wide range of different Arduino products many of which use different Microcontrollers on them.
However if you know what chip you are using, And are willing to put a little effort into reading datasheets and are willing to dabble in lower level code then you can perform the same function as a digitalWrite() on up to 8 pins in a fraction of the time it takes with digitalWrite().

Most if not all Arduino functions sacrifice speed for portability and efficiency for ease for the beginner.

So how do I create a hybrid program? Simple, Just write it straight in there! because the ArduinoIDE uses the same gcc compiler provided my Atmel for their own products the generic code is interpreted automatically. An example of this hybrid code is below:

//LED sequence for ArduinoMEGA with ATMEGA2560 chip
void setup() {
DDRB
= 0xff; //sets all of portB as outputs (Port b = digital pins 53, 52, 51, 50, 10, 11, 12, 13)
DDRA
= 0xff; //sets all of portA as outputs (port a = digital pins 22, 23, 24, 25, 26, 27, 28, 29)
}
void loop() {
for (int i = 0 ; i <= 7 ; i++)
 
{
  PORTB
|= (1<<i); //turns on pin i in port B
  PORTA
&= ~(1<<i); //turns off pin i in port A
  delay
(128);
 
}
for (int i = 0 ; i <= 7 ; i++ )
 
{
  PORTB
&= ~(1<<i); //turns on pin i in port A
  PORTA
|= (1<<i);  // turns off pin i in port B
  delay
(128);
 
}
}

You can see the DDRx = 0xff is the same saying pinMode, but the AVR C way allows all eight pins on the port to be changed to outputs in a single operation,
The PORTx bits are no more compact then you could make them with Arduino using a very similar loop,  but they are significantly faster in execution. In this particular example the speed isn't so much of an issue as we are adding a 128ms delay anyway. However it is quite likely that some day you will want to interface with an external controller of some kind which will usually need a sequence of control and data signals to operate and the overheads of the Arduino function will greatly reduce the speed at which you can communicate.

To understand this further I suggest you take some time to read and understand http://www.micahcarrick.com/tutorials/avr-microcontroller-tutorial/avr-c-programming.html 
I'm learning more and more myself and some time very soon I hope to not have to use Arduino at all and just be able to program the microcontroller directly.

It is important also to realise that AVR C isnt specific to a particular chip, Infact any 8-bit AVR chip uses the same language. However you do need to familiarise yourself with the pin to port mapping for each chip, And also be familiar with the hardware peripherals on each chip you intend to play with.

Further updates will be provided as I develop my own skills, Comments, suggestions and questions are always welcome :)

Chris Garrett

unread,
Oct 17, 2014, 7:01:04 PM10/17/14
to barns...@googlegroups.com

On Fri, Oct 17, 2014 at 4:58 PM, kal9001 <kal...@gmail.com> wrote:
I'm learning more and more myself and some time very soon I hope to not have to use Arduino at all and just be able to program the microcontroller directly.

This is a good guide

I'm a big fan of the Attiny85 :)


--

Chris Garrett
http://www.chrisg.com   |   http://twitter.com/chrisgarrett

The contents of this email is:
[ ] blogable [x] ask first [ ] private

kal9001

unread,
Oct 17, 2014, 7:05:23 PM10/17/14
to barns...@googlegroups.com
 Yea I have ten Tiny85s, also have five 328Ps on the way in the post, as well as a few ArduinoMega boards :)

Chris Garrett

unread,
Oct 17, 2014, 7:11:14 PM10/17/14
to barns...@googlegroups.com

On Fri, Oct 17, 2014 at 5:05 PM, kal9001 <kal...@gmail.com> wrote:
Yea I have ten Tiny85s, also have five 328Ps on the way in the post, as well as a few ArduinoMega boards :)

Unfortunately the price has gone crazy. I bought 25 for $22 this time last year, now they are a pound a piece :(

kal9001

unread,
Oct 17, 2014, 7:28:29 PM10/17/14
to barns...@googlegroups.com
You mean the Tiny85 of the 328Ps?
The Tinys are £0.85p each from Farnells. I got mine for £0.76 each if you order 10+.
If you mean the 328Ps then they are about two quid each from the same site I've got mine for half that from overseas.
But really one or two quid is not really much considering what they are capable of. Its just learning to use them thats both the challenge and the appeal! lol 

Chris Garrett

unread,
Oct 17, 2014, 7:41:18 PM10/17/14
to barns...@googlegroups.com


On Friday, October 17, 2014, kal9001 <kal...@gmail.com> wrote:
You mean the Tiny85 of the 328Ps?

Attiny85s - if I just need to blink some LEDs or make some beeps I'd use that over a 555 any day

October 2013 I got 25 attiny85 for $22 (canadian).

 Atmega328 were $2 each roughly. 

For the price now of atmega I like to get ebay pro minis unless I really need the compactness or I'm making a pcb :)

kal9001

unread,
Oct 17, 2014, 7:55:43 PM10/17/14
to barns...@googlegroups.com
Well I thought the 328P was a good choice as it offers a good I/O selection in a DIP that can be easily breadboarded. Shame they don't do the ATmega in a DIP package but I suspect even if they did the one IC would take up an entire breadboard on its own lol.
The Tiny85's will do great for....well.....Tiny stuff :) I have already used one to replace a 555 timer I used on a voltage doubler. The wiring is greatly simplified, Power, Ground and output and software can dial in the exact frequency/duty cycle you need without having to have a ton of different caps and resistors to get it just right >:-@. With the added benefit of being able to output more than one different signal and being able to change the signals on the fly over time or due to some input, All in the same 8P DIP......I don't know why they still even sell 555s lol..

kal9001

unread,
Oct 18, 2014, 8:33:19 AM10/18/14
to barns...@googlegroups.com
Slight update, I modified the code to use 24 LEDs instead of 16 but using an extra register. The delay has been reduced slightly too.
A video can be found Here.

void setup() {
DDRB
= 0xff;

DDRA
= 0xff;
DDRL
= 0xff;

}
void loop() {
for (int i = 0 ; i <= 7 ; i++)
 
{
  PORTB
|= (1<<i);

  PORTL
&= ~(1<<i);
  delay
(96);

 
}
for (int i = 0 ; i <= 7 ; i++ )
 
{

  PORTA
|= (1<<i);
  PORTB
&= ~(1<<i);
  delay
(96);

 
}
for (int i = 0 ; i <= 7 ; i++ )
 
{

  PORTL
|= (1<<i);
  PORTA
&= ~(1<<i);
  delay
(96);
 
}
}

Chris Garrett

unread,
Oct 18, 2014, 12:09:45 PM10/18/14
to barns...@googlegroups.com
On Fri, Oct 17, 2014 at 5:55 PM, kal9001 <kal...@gmail.com> wrote:
Well I thought the 328P was a good choice as it offers a good I/O selection in a DIP that can be easily breadboarded.

It is, I think it's the best bang for buck in the AVR line. There are others like the 168, etc but the price difference is not worth it. There are also bigger as you have seen. 328 is the sweet spot IMO :)
 
 
..I don't know why they still even sell 555s lol..

No idea, I guess for education and electronics tough guys who always want to do things the hard way ;)

Here's my attiny85 running 4 RGB individually addressable LEDs. Because these are "chip on LED" type daisy chainable LEDs I could run dozens of them off one tiny (if enough current). Great for Halloween and Christmas decorations! Of course the LEDs are not cheap so you only save space not money over a pro mini ;) 


kal9001 .

unread,
Oct 18, 2014, 4:54:07 PM10/18/14
to barns...@googlegroups.com
Looking at that picture before I read your comment was a little confusing! never heard of chip on led type LED's to be honest, I guess they make sense now you've said it.
The Ive not done a great deal with my Tinys yet just a few small projects. I have more ideas than I can shake a stick at but its just getting my backside in gear and actually building them up. I'm just trying to get my head around Atmel Studio at the moment, Going from the ArduinoIDE to that is a massive jump.  
Reply all
Reply to author
Forward
0 new messages