Thursday, 5 December 2013

Intro to Arduino at KMS

Adafruit Trinket
controlling an
RGB LED


Update: By popular demand I will be running this workshop again on April 14, 2014.

I volunteered to facilitate a workshop at Kingston MakerSpace next week to provide a very basic introduction to Arduino micro controllers. Here's the event advertising. There are many variants and imitators of Arduino. (Here's a Make article on which board?) Don't get hung up on picking one. Anything that is supported by the Arduino environment will give you a good learning platform to find out what you can do and what you need for each project.

There's an Arduino UNO hiding under there, matched up
with a breadboard and mounting plate that makes it
way easier to keep things together
That said, I would recommend the Arduino UNO board for beginners because it sets the standard and others may deviate a little from that standard and get confusing. It also has nice readable labels beside all the pin connections.

 You'll also need some way to connect the Arduino with components like LEDs and pushbuttons, and so on. A solderless breadboard makes that easy, and a mounting plate will hold them both together and help keep the wires from pulling out. (Bad connections are a very frustrating problem to debug.) I like the version in the Experimenter's Kit.

We'll get started step by step before you move on to other learning projects you choose yourself:

Install the Arduino integrated development environment (IDE) on your laptop in advance so you can bring it to the MakerSpace, or install it once you get there. (We have wireless and the password is on the wall.) I like the Adafruit Arduino 1.05 distribution. It is the latest standard version (1.05) from Arduino with  additional support for some of the Adafruit boards like Trinket and Flora that some of you will be using. (Any version of the Arduino IDE should be fine for this workshop if you are using the UNO.) The Mac install is seamless. The Windows install says it requires some fancy footwork with device drivers as detailed on the instruction pages, but the Win 7 machine I tested on automatically loaded the drivers for the UNO and worked. Same for the Arduino Micro. For windows you will have to unzip the distribution and copy it to your program files directory, then make a shortcut to run it from your desktop.

For the Trinket I had to do the detailed device driver install explained on the Adafruit pages, then set the programmer to USBTinyISP, and hook the Trinket to the USB through a hub (I had the same problem on a Macbook Pro). For a linux install you will probably have to edit the config files and add some stuff, but linux users are used to that ;-)

Connect your Arduino to a USB port and then create a program to say "Hello World". Open the Arduino program on your laptop and use the tools menu to pick the right Board and the Serial Port it's connected to. Type (or copy and paste) this code into the sketch window (a sketch is a little program).

void setup() {                
  Serial.begin(57600);   // get the serial port ready at 57600 baud
  pinMode(13,OUTPUT);    // set pin 13 to be in output mode
}

void loop() {
  digitalWrite(13,0);              // turn the LED off
  if(millis() % 1000 == 0){        // if we're at a round second
    Serial.print("Hello World! "); //   then say hello
    Serial.print("time is now ");
    Serial.print(millis());
    Serial.print(" ms from boot.\n");
    digitalWrite(13,1);            //   then turn the LED on
    delay(500);                    //   then wait half a second   
  }
}

Hit the right arrow icon to verify your sketch and upload it to the Arduino. Once it's running the LED on the board should start blinking on and off and the magnifying glass icon will let you see the serial port output from your sketch. (You may need to open the serial window and change the port speed to 57600 baud before anything good happens.) 

If you have a Trinket the sketch below from Adafruit will just flash the built-in LED on pin 1 for the Trinket, since it doesn't support Serial. (You'll need to push the button on the Trinket just before you upload.)


int led = 1; // blink 'digital' pin 1 - AKA the built in red LED

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);

}

// the loop routine runs over and over again forever:
void loop() {
    digitalWrite(led, HIGH); 
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
}
 
It will also work with the UNO and others if you switch the coded pin number to 13, where they have a built-in LED. Can you explain to your partner what each line of code does and how it works?

Try changing the code to have it say hello to you by name.

How often does the Arduino run through the loop() function? How can you find out?

Now we'll try to light up our own LED, or three. This will tell you All About LEDs, or we can cut to the chase:

Never connect an LED to power without a resistor of more than 100 ohms in series (pick one with a third colour band that's brown). With no resistor to limit the current your LED will either get hot and wear out faster, flash brightly and never work again, or maybe go BANG! None of these are good outcomes.

Make sure the long lead (the anode) on the LED is on the positive side of the circuit. Getting it backwards will give you no light, but won't break anything.

This UNO based RFID reader opens and
locks the door at KMS
Try connecting an LED and resistor between the 5V (5 volts) and GND (ground or 0 volts) pins on your Arduino. Using some wires and a breadboard will make this easier. If the LED doesn't light up, you probably have it backwards. If it still doesn't light up, somebody probably connected it with no resistor and it needs to be thrown away.

Now connect to pin 13 (or pin 1 on the Trinket) instead of 5V and the LED should blink in time with the built-in LED.

There's tons more complicated things you can do with LEDs but start with Blink (we already did) and Pulse (below) then move on to RGB LEDs that combine three LEDs in one package and can give you any colour you want.

int led = 1; // pulse 'digital' pin 1 - AKA the built in red LED

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  for (int i=0; i<256; i++) {
    analogWrite(led, i);  // PWM the LED from 0 to 255 (max)
    delay(5);
  }
  for (int i=255; i>=0; i--) {
    analogWrite(led, i);  // PWM the LED from 255 (max) to 0
    delay(5);
  }
}

This Pulse sketch is just a little different from the Blink sketch up above. What's the difference between digitalWrite() and analogWrite()? What does PWM stand for and does it work on all the pins?

Add a pushbutton (or two) to control what happens based on user input. Can you use a pin for input and for output at the same time? Get more detailed control with Analog Inputs and a potentiometer.

Try controlling a servo motor next for an experience that's easy to wire up and pretty gratifying (Trinket version). The door lock on the MakerSpace is mostly just a servo being controlled to open and close the deadbolt. Please don't try to reprogram the lock until you are sure you know what you're doing.

If we get anywhere near this far in a two hour workshop, then I will be impressed.