loading

The following information is a single lesson in a larger project. Find more great projects here.

Project Overview:

This project covers some of the basics of working with the Arduino Uno. It covers digital input and output, analog input, and code structure.

Step 1: Introduction

It is common to make a circuit on a breadboard with a few LEDs. If you want to manually control the LEDs, you can add a switch button to each of them. It would be great if the LEDs could be controlled automatically. For example, to make a blinking light, or running lights, or whatever you can imagine.

This is where an Arduino comes in. It is a programmable device that can respond to sensors connected to its inputs, and control actuators connected to it’s outputs.

Sensors are devices that convert physical energy like motion, light, and sound, to electrical signals.

Actuators do just the opposite. They take electrical signals and convert them to real-world actions, like moving a motor, or making light or sound.

It's up to you to decide how the Arduino behaves. You do this by giving it instructions, called a program. The program defines what actions are taken, based on sensor input.

  1. Continue to the next step.

Step 2: Add an Arduino

There are a lot of different Arduino devices available. For this tutorial you will start with the most basic Arduino: the Arduino Uno.

This Arduino board consists of different parts, each with it’s own functionality, but all working together to form a great multi-functional system. The different parts are briefly explained in the next few steps.

  1. Click "Components" in the top right of the editor.
  2. Search for the Arduino uno component and click to add it to the circuit.
  3. Place it below the breadboard.
  4. Click "Components" again to hide the components selector.
  5. Continue to the next step.

Step 3: Parts of an Arduino - Power In

The input power jack is the black connector at the bottom left. This is the external power input.

In the editor this is not actually connected to anything. In the real world however, this is where the Arduino gets its power.

  1. Continue to the next step.

Step 4: Parts of an Arduino - Power Out

The bottom left header is for power to drive other circuits. This provides stable, 5 volt and 3.3 volt power.

You can think of this as a special kind of battery. For 5 volts, you can use the pin labelled "GND" as the negative (black) terminal, and the one named "5V" for the red (positive) terminal.

For 3.3 volts, you can use the pin labelled "GND" as the negative (black) terminal, and the one named "3.3V" for the red (positive) terminal.

  1. Continue to the next step.

Step 5: Parts of an Arduino - Analog In

The header at the bottom right is labelled "Analog In". The Arduino can measure analog voltages on these pins.

It is for connecting sensors that provide different voltages, based on what they sense. These are things like photodetectors, and temperature sensors.

  1. Continue to the next step.

Step 6: Parts of an Arduino - Digital In/out

The header at the top is for digital input and output. Digital signals have only two states: high (5V), or low (0V).

The pin headers numbered 0 - 13 can be used as both digital inputs or outputs.

  1. Continue to the next step.

Step 7: Parts of an Arduino - LEDs

There are four LEDs on the Arduino. These give some visual information about what's going on inside.

The one at the right is lit when the board has power.

The top left LED is connected to digital pin 13. It is lit whenever pin 13 is high.

The "TX" and "RX" LEDs indicate when serial communication is happening.

  1. Continue to the next step.

Step 8: Parts of an Arduino - Reset Button

The push button at the upper left is the reset button. Pushing this button reset the Arduino and your program restarts from the beginning.

  1. Continue to the next step.

Step 9: Parts of an Arduino - Microcontroller

The big black component in the middle is the microcontroller. It is the brain of the Arduino. It controls all of the pins and executes your program.

The Arduino you just added to your circuit comes with a default program. Find out what program it is.

  1. Click "Start Simulation" at the upper right of the editor.
  2. Continue to the next step.

Step 10: Default Program

When you simulate the Arduino without changing the program, you can see an onboard LED repeatedly turning on for one second, and off for one second.

To understand why this is happening you will have to explore the program that is running in the Arduino.

The program is the set of instructions for the Arduino that tells the microcontroller how to behave.

To make sure the microcontroller understand what you want it to do, you have to use a specific programming language. For the Arduino the programming language is one known as C++.

Take a look at the default program.

  1. Click "Code Editor" at the top right of the simulator to view the program.
  2. Continue to the next step.

Step 11: Program Code - Declarations

Inside the code editor you can see the program that is running.

For the default program, there are 3 main sections of code: declarations, setup, and the main loop.

The first 2 lines of code each start with a double slash (//). The double slash indicates that the rest of the line is a comment. Comments contain no instructions and are not sent to the Arduino. They are there to make the code more readable for you and other people.

At line 3 you can see a declaration of a variable. This is the first real program instruction. Here the variable called ‘led’ is declared as an integer (int) and set equal to the number ‘13’.

The semicolon at the end of the line terminates the instruction. All instructions end with a semicolon.

The variable is named ‘led’ since it is used to specify the pin to control the LED. It could have been named anything, but it is useful for reading a program if the names of things give clues about their use.

The "led" is assigned the value 13 because you want to control the onboard LED, which is connected to digital pin 13.

The rest of the code can refer to the variable "led" to refer to the number 13 instead of using the number 13 explicitly.

  1. Continue to the next step.

Step 12: Program Code - Setup

In the next section of code, you can find the setup function. This function is executed only once: when you start or reset the Arduino.

The statement at line 6 starts with the keyword ‘void’ which means that this function does not return a value when it is called.

After the function name ‘setup’, you can see the argument list between brackets. Arguments are information given to a function to tell it how to do its job. In this case the setup function does not expect any arguments, so the list is empty ().

Next, you can see a list of all of the instructions that will be performed when this function is called. This list is between curly braces {}.

  1. Continue to the next step.

Step 13: Program Code - Setup (2)

In this program the setup function executes a single statement (at line 8).

The Arduino digital pins can be used as inputs or outputs. When the Arduino starts, you can define how to use the pins. In this program, the goal is to control the LED connected to pin 13. So pin 13 is used as an output. To do so, the internal Arduino function ‘pinMode’ is called with the pin-number as a first argument and the pin-mode as a second argument.

The arguments are separated by commas.

  1. Continue to the next step.

Step 14: Program Code - Loop

The last block is the ‘loop’ function. This function runs continuously after the setup function is completed.

This function controls the actual behavior of the pins while running. In this case it makes the led turn on and off as you saw in the simulation.

The first instruction (line 13) turns the led on by the use of the ‘digitalWrite’ function. It writes a ‘HIGH’ value (5V) to ‘led’ pin 13.

The second instruction waits for one second. The ‘delay’ function pauses the Arduino for the specified number of milliseconds (ms). 1000 ms = 1 second.

The next instruction turns the LED off, by using the ‘digitalWrite’ function again.

This is followed by another 1 second delay.

When the end of the loop function is reached, it immediately starts again. This repeats the process of turning the LED on and off for as long as the Arduino continues to have power.

As a result, the LED continues blinking.

  1. Continue to the next step.

Step 15: Adding an Output - LED

Now you understand the program. It alternately sets a high and low voltage on digital pin 13, which makes the on board LED blink.

You can think of this as the Arduino switching on and off a battery, whose positive terminal (red) is at pin 13 and its negative terminal (black) is the ‘GND’ pin.

Now you will try to connect another LED to this pin. You can’t just connect a LED to a power source, you also need a resistor to limit the current through the LED.

  1. Open the component selector, search for the LED and add one somewhere above the Arduino.
  2. Search for the resistor, add it to the circuit and change it’s value to 220 Ω.
  3. Connect one side of the resistor to pin 13 and the other to the anode (longest pin) of the LED.
  4. Connect the other pin of the LED (the cathode) to the blue row at the bottom of the breadboard.
  5. Connect a wire between the blue row at the bottom of the breadboard and the ‘GND’ terminal of the Arduino.
  6. Start the simulation.
  7. Continue to the next step.

Step 16: Add More LEDs

Add a green LED and a blue LED to create a LED strip.

  1. Add 2 additional LEDs to the circuit and change the colors to green and blue.
  2. Connect a 220 \u03a9 resistor to the anode of each LED.
  3. Connect the resistors of the red, green and blue led to digital pins 13, 12 and 11, respectively.
  4. Connect the cathode of each LED to the ground rail (negative blue row) on the breadboard.
  5. Connect this ground rail to the "GND" terminal of the Arduino board.
  6. Continue to the next step.
  7. Stuck? HINT: The circuit is most clear when you put all of the components on a breadboard.

Step 17: Changing the Program

Now that different LEDs are connected to pins 11, 12 and 13, the program needs to be changed to control them all.

Try to modify the program to make all of the LEDs blink at the same time.

  1. Open the code editor.
  2. Change the variable ‘led’ to ‘ledRed’. Note: make sure you change the variable everywhere in the program.
  3. Add a declaration for the green and blue LEDs: int ledGreen = 12; int ledBlue = 11;
  4. Configure pins 11 and 12 as outputs in the ‘setup’ function: copy the instruction for the red led, paste it twice and change the variable names to ledGreen and ledBlue, and values to 12 and 11, respectively.
  5. Repeat the digitalWrite() function calls this in the ‘loop’ function to make sure all LED pins are set HIGH and, after the delay, set LOW.
  6. Start the simulation.
  7. Continue to the next step.

Step 18: Running Lights

Try to adjust the program so that the LEDs turn on one by one, resulting in a running light.

The red LED should turn on first. At the same time it turns off, the green LED should turn on. When the green LED turns off, the blue LED should turn on.

  1. Continue to the next step.
  2. Stuck? HINT: Your code in the loop function should look something like this: digitalWrite(ledRed, HIGH); delay(1000); digitalWrite(ledRed, LOW); digitalWrite(ledGreen, HIGH); delay(1000); digitalWrite(ledGreen, LOW); digitalWrite(ledBlue, HIGH); delay(1000); digitalWrite(ledBlue, LOW);

Step 19: Changing the Speed

Right now the LEDs change every second. You might find this a bit slow. Try to speed things up.

One way is to make the delays shorter. You could change the argument of all delay function calls from 1000 ms to 500 ms.

A better, more general way is to define an extra variable ‘delayTime’ to hold this value, and to change all of the delay function calls to use this value. That way, you can easily change the time of all of the delays in one place.

  1. Open the code editor.
  2. Declare a new variable in the declarations section (first part): int delayTime = 500;
  3. Change the argument of all of the delay function calls from ‘1000’ to ‘delayTime’
  4. Start the simulator.
  5. Continue to the next step.

Step 20: Adding a Sensor

Another feature you can add is to control the running speed with an external sensor. In this case, you will use a slide switch. When the slider is in the initial position the light will blink slowly. When the slide switch is in the other position, the LEDs will blink faster.

As was stated before, the digital pins of the Arduino can be either outputs or inputs. You will modify the circuit to supply either high (5V) or low (0V), depending on the position of the slide switch.

  1. Click "Components" to open the component selector.
  2. Search for and add a "Slideswitch" to the breadboard as in the picture.
  3. Connect the right terminal to the blue ground rail on the breadboard.
  4. Connect the left terminal to the red power rail of the breadboard.
  5. Connect the ‘5V’ terminal of the Arduino to the power rail of the breadboard.
  6. Connect the middle terminal of the switch to digital pin 0 of the Arduino.
  7. Continue to the next step.

Step 21: Reacting to the Switch

You will need to adjust the program to react to the switch.

First, you need to configure another pin "switchPin", but this time as an input.

The Arduino can read the state of this pin using the function ‘digitalRead’. This function needs the desired pin-number as an argument and returns the state of that pin.

When there is a high voltage on the pin, it will return the value 1 (HIGH), when there is no voltage at the pin, it will return the value 0 (LOW).

You can store this value in another variable. To summarize, you can use the following statement in your program: switchState = digitalRead(switchPin)

  1. Open the code editor.
  2. Add to the declaration section: int switchPin = 0; int switchState = LOW;
  3. Configure the pinMode in the setup function. Add: pinMode(switchPin, INPUT);
  4. Add a statement to read this pin value as the first line of the loop function: switchState = digitalRead(switchPin);
  5. Continue to the next step.

Step 22: Reacting to the Switch (2)

Now you have the switch to change the timing.

The Arduino can handle this using an 'if()' statement. The if statement can compare 2 values. Depending on whether the comparison is true or false, it can take different actions.

Here you can use the following code:

if (switchState == LOW) { delayTime = 500; } else { delayTime = 250; }

If the pin is read low, then this code assigns 500 (ms) to the variable ‘delayTime’. Otherwise (when the pin is high), it will assign 250 (ms).

Note: when comparing two values, you have to use two equal signs. If you use only one, you will assign a value instead of comparing.

  1. Paste the above code after the digitalRead statement in the loop function.
  2. Start the simulator.
  3. Change the position of the switch by clicking on it.
  4. Continue to the next step.

Step 23: Finishing Up

You have just made your first Arduino circuit. Congratulations!

Now it’s time to adapt this circuit as you want: for example:

- extend your LED strip with more LEDs. - add an additional switch - have the switch control the direction that the LEDs blink - etc…

Perhaps you have noticed that the effect of a switch toggle is only applied when the running light starts over again on the red LED. Try to figure out why this happens. How would you solve this problem?

  1. Good job, and continue learning!
  2. Stuck? HINT: Add the switch read and if statement after each delay statement in the loop function.

In the next lesson you will learn to read sensors and debug.

Next Lesson:Using Sensor Input and Serial Output

About This Instructable

192views

8favorites

License:

More by circuits:Introduction Arduino Project 15: Hacking Buttons Arduino Project 14: Tweak the Arduino Logo 
Add instructable to: