---

Showing posts with label leonardo. Show all posts
Showing posts with label leonardo. Show all posts

Friday, March 22, 2013

Leostick Review and Mini-Project

The Leostick is an Arduino Leonardo compatible board from Freetronics.

There are a number of things that make this board a great deal more than a clone:
  • Its smaller than the Uno / Loenardo sized Arduinos. In fact about the size of a USB flash drive.
  • It has a USB plug built onto the PCB, so you can plug it straight into the side of your computer to program it. Or use an USB extension lead.
  • It has a built-in RGB LED.
  • Being a Leonardo type board means that you can have it emulate a keyboard and mouse!
  • Flip it over and it has a piezo sounder on the back.

This all means that you can actually have quite a lot of fun with the Leostick straight out of its anti-static bag. However, the fun does not end there. Oh no, there is even more. The Leostick comes with a set of header pins and yes, they also sell little mini proto-shields that you can fit onto the Leostick.



Mini Project

To try out the Leostick, I decided to have a go at a little project from the second edition of my book '30 Arduino Projects for the Evil Genius' - out May 2013. This project was originally designed for an Arduino Leonardo.

It has two buttons, and when you press one it generates an obscure password and stores it in EEPROM. When you press the other button, it emulates a keyboard and types the password for you, wherever your password happens to be.

This proved to be really easy to do. Here is the finished result on a Leoshield.

Step 1.
Solder the header sockets onto the Leostick and the header pins onto the Leoshield.

It is easier to solder the header pins onto the shield if you first push them into the sockets on the Leostick.

Step 2. 
Solder the click switches and wires.


Make sure you get the switches the right way around. Look carefully at the diagram above. One way around there will be two unused holes between pins (left to right) the other way (vertically) there will be just one.

The bare wire at the bottom connects GND to one side of both switches. The other sides of the switches are connected to D7 and D6.

Here is the sketch, ignore the bit of paper that comes with the Leostick saying you need to download something, you don't, just use it like you would an Arduino Leonardo.



#include <EEPROM.h>

int greenPin = 9;
int soundPin = 11;
int typeButton = 7;
int generateButton = 6;
int passwordLength = 10;

char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

void setup()
{
  pinMode(greenPin, OUTPUT);
  pinMode(soundPin, OUTPUT);
  pinMode(typeButton, INPUT_PULLUP);
  pinMode(generateButton, INPUT_PULLUP);
  Keyboard.begin();
}

void loop()
{
  if (digitalRead(typeButton) == LOW)
  {
    notify(1);
    typePassword();
  }
  if (digitalRead(generateButton) == LOW)
  {
    notify(3);
    generatePassword();
  }
  delay(300);
}

void typePassword()
{
  for (int i = 0; i < passwordLength; i++)
  {
    Keyboard.write(EEPROM.read(i));
  }
  Keyboard.write('\n');
}

void generatePassword()
{
  randomSeed(millis() * analogRead(A0));
  for (int i = 0; i < passwordLength; i++)
  {
    EEPROM.write(i, randomLetter());
  }
}

char randomLetter()
{
 int n = strlen(letters);
 int i = random(n);
 return letters[i]; 
}

void notify(int n)
{
  for (int i = 0; i < n; i++)
  {
    tone(soundPin, 330);
    digitalWrite(greenPin, HIGH);
    delay(200);
    noTone(soundPin);
    digitalWrite(greenPin, LOW);
    delay(200);
  }
}


For good measure the sketch uses both the built-in RGB LED and the buzzer to provide feedback when you press a button.

To test it, just put your cursor in a blank document in an editor or wordprocessor. Pressing the button furthest away from the USB plug will generate a new password and beep three times, while flashing the LED green.

Pressing the other button will write out the password. So, now you can carry this everywhere with you and use it to type your passwords for you.

Summary

Pros

  • What's not to like - see everything I have written so far
  • Gold plated everything - Freetronics know how to make a good PCB
  • Pin usage labelled on the board and a jumper to break if you don't want sound.

Cons

  • A very minor, I2C SDA and SCL would be the icing on the cake. But probably not worth spoiling the pinout for.

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





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.