Saturday, May 16, 2015

How to make your first arduino project

How to start?

Hardware

The first thing you need is the hardware, the chip and some components to control.  I would suggest using adafruit since they have been reliable for me and they have a lot of cool components and projects.  I think that either the budget pack or starter kit is sufficient to start with.  Once you are comfortable with that kit you can start to buy additional components.



Software

Now we need the software to program the arduino.  Head over to the arduino download page and grab the copy that matches your operating system.  Once it is downloaded you are good to start making programs.

Hello world - Blink

Software

The first program we are going to create is a basic blinking example.  There are two parts required for all arduino programs, the setup and the loop.

In our program we only need to set up one thing, pin 13 as an output.  To do this we use a function called pinMode.  It takes two parameters, the pin to adjust and the mode to go into.

void setup() {
  pinMode(13, OUTPUT);
}

Now we need to tell the LED to flash on and off.  There are 2 functions we need to use, a delay function and a pin output modifier.  To block the program (delay it) we simply call delay with a time, in milliseconds as the parameter.  In our example we are going to make the LED take 1 second to get to the next state.  We also need to toggle the LED on and off.  To do this we can use digitalWrite to make the pin go HIGH or LOW.

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

Wiring

The circuit diagram is a simple setup.  The LED is connected to pin 13 and a resistor which is connected to ground (GND on the arduino).

When wiring the circuit make sure that you start with the arduino unplugged.  This helps ensure that there are not any accidental short circuits which could damage the LED or the arduino.

WARNING: WHEN USING A LED ALWAYS USE A RESISTOR!

If you do not use a resistor then the LED will fail and no longer work.  The exact order of the LED and resistor doesn't matter so either can go before the other.

LEDs are polarized, that means that it matters which way around you have them in a circuit.  Fortunately it is easy to remember which one goes to where; the longer lead goes to the positive end of the circuit.  In our circuit the positive end of the LED is connected to pin 13 and the negative end is connected to the resistor.

The resistor should be something between 100 and 500 ohms.  In the starter packs it is the resistor with the color code {Brown, Black, Black, Gold}.  For more information see what resistor to pick?

No comments:

Post a Comment