Friday, May 16, 2014

Getting started with Arduino - Simple Outputs

I'm going to start a small Arduino introduction series. I think it's important that people understand what's happening exactly when they create their first blinky program. The three things that I'm going to cover in this post is:
1. Turning an LED on
2. Blinking an LED on and off
3. Fading an LED for loop
We're also going to talk about how to use the IDE briefly, just so you can follow along better.

These three tasks are relatively simple, but understanding these will let you control thousands of other devices, not just LEDs!

To create a new sketch, all you do is open up the IDE, which can be downloaded at Arduino.cc. Once opened there should be nothing except for an empty area. Every Arduino program needs two functions to compile. Those functions are void setup() and void loop(). Those can be seen in the picture below.

1
2
3
4
5
6
7
8
9
void setup() {


}

void loop() {


}

The setup() function only runs once on the Arduino. It's often used to set pin modes as either inputs or outputs, and can be used to initialize variables at startup.

The loop() function runs forever, until your board loses power, or you program a stopping point. It runs very very fast, and a majority of your code goes in here. 

Now I want to explain what the pinMode() command does. pinMode() tells the Arduino what to set each pin as, either an input or output. pinMode has two parameters. The first parameter tells the Arduino which pin you're talking to, and the second parameter defines it as an input or output. Look at the picture below to see it being used. The pinMode() is also usually found in the setup() function.

1
2
3
4
5
6
7
8
9
void setup() {
  pinMode(3, OUTPUT); //Setting pin 3 as an output on the Arduino
  pinMode(8, INPUT); //Setting pin 8 as an input on the Arduino
}

void loop() {


}

digitalWrite() is how we set an OUTPUT pin high or low, meaning 5 volts or 0 volts. digitalWrite() has two parameters. The first one is which Arduino pin you're talking to, and the second is HIGH or LOW. To blink an LED at a steady pace it would be done like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
void setup() {
  pinMode(, OUTPUT); //Setting pin 3 as an output on the Arduino
}

void loop() {
 digitalWrite(3, HIGH);
 delay(500);
 digitalWrite(3, LOW);
 delay(500);
 
}

Above I use a delay() function. It only has one paramter, and it's in integer value in milliseconds. If you were to type
delay(500); 
The Arduino would pause what it was doing for 500 milliseconds and keep doing the command above it until the 500 milliseconds has passed.        

Another incredibly useful command would be analogWrite(). analogWrite() lets your vary the brightness of an LED or the speed of a motor. It uses something called pulse width modulation which does exactly what the name is. It varies the width of the HIGH LOW transitions to make it appear the voltage is changing, when in reality we're only changing the time that the pin is HIGH or LOW really fast. The parameters for analogWrite are the pin number, and a value between 0-255. 255 is a duty cycle of 100%, 127 would be 50%, and 0 would be 0%. It is very important to use a pin on the Arduino that is compatible with PWM. Look up the specs for your Arduino and use one of the pins that is useable with PWM.

To set an LED at half brightness it would look something like this:

1
2
3
4
5
6
7
void setup() {
  pinMode(3, OUTPUT);
}

void loop() {
  analogWrite(3, 127);
}


Now to fade an LED we would use a for loop. The for loop would need to control a variable to go from 0 - 255. When doing the the LED's brightness would ramp up, and with use of a second for loop, we can ramp it back down. The two loops would look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
void setup() {
  pinMode(3, OUTPUT); //Setting pin 3 as an output on the Arduino
}

void loop() {
 for(int i = 0; i <= 255; i++) {
   analogWrite(3, i);
   delay(10);
 }
 for(int j = 255; j >= 0; j--) {
   analogWrite(3, j);
   delay(10);
 }
}
The reason we use a 10 millisecond delay in the LED fading is so it doesn't happen so fast, and it looks better to the eyes!

In the for loop I have a counter 'i'. It starts at 0, and every 10 milliseconds it increments by 1 (that's what the i++ does). It then writes the value of 'i' to the second parameter of analogWrite and the LED then shows that brightness. Once the first for loop is completed, it moves on to the second one, which is just the opposite. It starts the LED off at full brightness, and brings it back down.

Another really cool and important topic is the use of variables. Variables make code much more readable and easier to debug when you're having problems. All you need to do is create a variable, set a value to it, and use it in your program instead of hardcoding in the pin numbers. An example is below. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
int led = 3;

void setup() {
  pinMode(led, OUTPUT); //Setting pin 3 as an output on the Arduino
}

void loop() {
 for(int i = 0; i <= 255; i++) {
   analogWrite(led, i);
   delay(10);
 }
 for(int j = 255; j >= 0; j--) {
   analogWrite(led, j);
   delay(10);
 }
}


Notice how I removed the '3' in the analogWrite functions and replaced them with led. It makes the program look a little bit nicer I think.

Thanks everyone for reading! Next time we'll go over input devices, like switches, potentiometers, and LDR's (light dependent resistors).