---

Thursday, October 4, 2012

Arduino Leonardo vs. Arduino Uno

I finally got chance to have a really good play with a Leonardo a few weeks ago. I was checking that the Arduino-based sections of 'Hacking Electronics' worked with the Leonardo as well as the 'standard' Uno.
  

The new book will contain about 25 sketches - well Arduino is just too useful not to include in the book. So, it was with some trepidation that I uploaded the first sketch. Some 24 sketches later, and I was a convert. In the end, everything worked, and with the application of a bit of common sense, sketches can be written to run unmodified on both boards.

Uploading.

The first thing that stumped me, was that, excited by the prospect of my Leonard being able to impersonate a keyboard, I uploaded a sketch, that would spew out some gibberish.


This worked fine of course. A very nice clean and simple class for pretending to be a keyboard - this is great, I thought. It wrote gibberish all over my sketch, but thats fine, I could unplug it and tidy it up. So, I decided to try out my next idea (a password entering gadget).



Press the button and it writes your password - pretty insecure really.

But the problem was, as soon as the Arduino IDE got focus, ready to press the Upload button for my new sketch, the old sketch started spewing nonsense all over it, and of course the upload couldn't be made to happen.

Oops I thought - and for one moment I wondered if I had bricked my Leonardo.

The answer of course, is to hold down the reset button (to shut it up) until the 'uploading sketch' message appears in the IDE then release it.

Phew, I was back in business.



Analog Inputs as Digital Outputs.

Most of the sketches I had trouble with were those where I was using analog pins A0 to A5 as digital outputs, by referring to them, using the old Arduino trick of adding 14 to their 'A number'. So, to use A0 as a digital output, you can just do:

pinMode(14, OUTPUT);

This did not work.

However, if you do:

pinMode(A0, OUTPUT);

It works just fine. What is more, it is actually clearer to see what is going on.

Note, that when referring to the analog inputs for their intended purpose, I did not put 'A' on the front of the pin numbers, I just did the following to read from A0.

int x = analogRead(0);



Shields.

The other area where I had a problem was in using a Leonardo with an old pre-R3 Ethernet shield. A bit of research indicated that an R3 Ethernet Shield should work just fine with it. I didn't have one to try.

The only other shield that I tried with it was the Freetronics LCD shield. This worked just fine.


Conclusion.

I really like the Leonardo, I love the USB keyboard and mouse emulation and the neat PCB layout. I also like that it is cheaper than an Uno, and has the following advantages:


  • More PWM pins
  • More digital pins full stop
  • Separate I2C pins (A4 and A5 are not dual purpose on Leonardo)
  • A second hardware serial port
So, what did I dislike?

Not much really - in theory, an advantage of the Uno is that if you destroy the ATmega328, you can replace it with a new one. But even with my usual lack of forethought, I think I have only had to do that once, and I spend a lot of time messing with Arduino.

The keyboard-gone-mad programming problem was understandable and really just my fault.

I suppose the only real down side that I see is if you have a lot of old shields that may have problems working. I also suspect there will be compatibility problems with some libraries.

Personally, I really like it, and will be using it again. If asked to recommend for someone just starting out with Arduino, I would love to be able to recommend a Leonardo, but I suspect sooner or later, they would run into a compatibility problem. So, I guess I would tell them to get one of each.

For a full list of the Leonardo's features go here.

Oh, and here is the password writing sketch. Obviously totally insecure - just a bit of fun.

char* password = "mysecretpassword\n";

const int buttonPin = 2; 

void setup() 
{
  pinMode(buttonPin, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() 
{
  if (! digitalRead(buttonPin))
  {
    Keyboard.print(password);
    delay(2000);
  }
}

About the Author
These are my books. Click on the image below to find out more about them.




                                                 

6 comments:

Unknown said...

Simon a great post but re your comment "I suppose the only real down side that I see is if you have a lot of old shields that may have problems working." If I may do a blatant plug?... Our LeoShield adaptor will allow you to use most older shields with the Leonardo.
http://gorillabuilderz.com.au/shop/leoshield/

Simon Monk said...


I wasn't aware of this. What a great idea!

Juan said...

Thank you so much for this. My LED cube worked with my Uno but did not work with my Leonardo. Once you explained the Analog pins as outputs difference, I was able to make the changes and it works fine. Thanks again!

Juan said...

Thank you so much for this. My LED cube worked with my Uno but did not work with my Leonardo. Once you explained the Analog pins as outputs difference, I was able to make the changes and it works fine. Thanks again!

Ayberk Özgür said...

I think another advantage of Leonardo is that you can hook it up with a micro USB cable which you can almost always find lying around vs. the B type USB cable you have to use for Uno which is harder to come across.

Unknown said...

Hi,

I bought your book "30 Arduino projects for the Evil Genius".

I have tried the "password typer" project, all works fine.

Was just wondering if there is a way of storing a password to EEPROM by reading an input from the keyboard on the PC?

Kind regards

Andy Clements