Arduino - 101 - Stuff you need to know

Setting up - an Arduino and a breadboard


The Sparkfun RedBoard (a clone of the Arduino UNO)


The board can be pared with a standard breadboard to facilitate the prototyping of circuits.


The circuits can be wired using the standard breadboard jumper cables. However, one can also make custom wires using solid core 22 gauge wire. 


The circuit shown here demonstrates a standard breadboard hookup. Power (+5V) and ground (Gnd) from the Arduino board is brought over to the bus rails on the breadboard. Note that red typically denotes positive voltage (+5 V) and black for ground (0 V).

The powered breadboard can then be used to supply power to components such as LED's. Here the long leg of the LED (the anode) is connected to the +5V supply and the short leg of the LED is connected to ground through a correctly sized resistor that acts to limit the current that can flow through the circuit - a current limiting resistor.

 

Creating Sketches - the setup() function


The anode of the LED can then be connected to one of the digital I/O pins on the Arduino - this means that it is connected to one of the 14 numbered pins on the right side of the Arduino board. These pins are numbered 0 - 13 and can be either be configured to supply a signal to a component that is connected to it - when it is configured in this manner the pin is in OUTPUT mode, or the pin can be configured to monitor signals applied to it and determine whether its input is HIGH (+5V) or LOW (Gnd), this is referred to INPUT mode

Thus the term digital I/O pin refers to a pin on a microcontroller that can set to either INPUT or OUTPUT modes. The LED in the following figure shows a situation where the anode of the LED has been connected to digital I/O pin 12. The state of the pin - either HIGH or LOW - can then be set via a program that is run on the Arduino.


This sketch shows how to set both the state of a digital pin and how set its state. This simple example accomplishes this task in the setup() function of the Arduino sketch.

***** Begin Sketch 1 *****

***** End Sketch 1 *****


The functionality of the circuit can be increased by adding more elements and configuring the setup such that each new element can be controlled via one of the Arduino digital I/O pins. This can be demonstrated by placing multiple LEDs on the breadboard - each with is own dedicated current limiting resistor - and connecting each LED to a digital I/O pin. The picture following shows four LEDs connected to digital I/O pins 12, 11, 10, 9. The figure to the right provides a diagram of the setup created using Fritzing (the potentiometer and button shown will be used later in the tutorial).

The program following is a variation of the previous sketch. Here multiple statements are used to set all the pins to OUTPUT mode and to set the state of the pins - pins 9 and 11 are set HIGH and pins 10 and 12 are set LOW.


***** Begin Sketch 2 *****

***** End Sketch 2 *****

The loop() function can now be used create statements that will be continually executed in sequence as long as the sketch is running. A simple example of this capability is demonstrated in the following sketch - each of the four LEDs are "blinked" in a sequence. This is sometimes referred to as racing lights or the night rider effect.

The basic set of statements is used to turn on an led, wait a bit, and then turn the LED off and wait a bit - in code this looks like:

pinMode(led9, HIGH);
delay(100);
pinMode(led9,LOW);
delay(50);

The following video shows what this looks like when used with four LEDs and the video is followed by the code.



***** Begin Sketch 3 *****

***** End Sketch 3 *****

Expanding the capabilities of a sketch using Arrays, For Statements, While Loops  and Functions

The following sketch is a modified version of the previous sketch, but here the code has been "improved" by adding the following:

  •  the LED variables are defined using an ledPins[]
  • a for statement is used to set through the array to set the pinModes
  • the statements used to create the racing lights effect has been placed into a function nightRider()
  • the function uses for statements to step through the array and set the state of the ledPins using digitalWrite
  • the function is called from the loop() function only ten times through the use of a while loop.

***** Begin Sketch 4 *****

***** End Sketch 4 *****

Analog Input and the Serial Monitor

The Arduino can also be used to gather data from sensors and other input devices. One class of these devices are referred to as analog devices and they typically output a voltage that is proportional to the value/quantity being measured. One of the easiest examples of this type of Analog Input is a potentiometer. 

A potentiometer is an adjustable voltage divider - the following figure provides a quick explanation as to how it works. The dial on the pot moves the "wiper" position that in turn changes the the ratio of R1 and R2. This allows the value of the voltage on the wiper (Vp) to vary from +5V to GND (0V).


The potentiometer can be used as an input device for the Arduino by connecting the wiper to one of its six analog input pins (A0 - A5). These pins are connected to the Arduino's analog to digital converter. This is a 10 bit device which means that it can read a 5V signal but it can only do so in defined increments (10 bit is 2^10 = 1024) so each voltage step is approximately .5 mV (5V/1023 = 0.0049V).

The following two images show how the pot is connected to the Arduino. 


The analog input pins are read using the analogRead() function. One important point to note is that there is no need to set the pinMode of the analog input pins because these pins only have a single mode - they can only be input. The code statement is written as:

int potVal = analogRead(A0);

This statement defines a variable potVal as an integer and assigns it a value that is between 0-1023 - where 0 >> 0.0 Volts and 1023 >> +5V.

It is sometimes desirable to change the range of values from 0-1023 to another set of values. For instance, one may want to use the pot to change the value of a variable between two specified values - a min and a max. This can be done using the built in linear mapping function called map(). The code statement is written as:

potVal = map(potVal, 0, 1023, minVal, maxVal);

In words this means...reset the value of the variable potVal from its initial value that is somewhere between 0 and 1023 to a value that is linearly proportional to it on the range between the values minVal and maxVal. This concept is shown in the following figure...


The following sketch is a slightly modified version of the example sketch AnalogReadSerial. Here the value of the pot is still read and echoed back to the serial monitor, but the value is also mapped from a minVal = 50 to a maxVal = 150 and this value is used to control the blink rate of an LED. Both the original and the mapped values are sent to the serial monitor. The action of the sketch is shown in the video below and the serial output is shown in the image...

 


***** Begin Sketch 5 *****

***** End Sketch 5 *****

Using IF statements

Sometimes you would like to be able to have your sketch do different actions for different input conditions. This type of control can be accomplished using the if statement.

An if statement is a function that checks to see if a specified condition is true or false. IF the condition (known as a conditional) evaluates to be TRUE, the function executes a series of statements, IF the condition evaluates to FALSE, the function ends and the sketch skips the statements that belong to the IF function and moves to the next item in the sequence.

Here is an example of a CONDITIONAL...

int x = 5;   // this is a statement that both defines and then sets the value of a variable 
x == 0;     // this is a conditional statement; the double equal sign is equivalent to asking the question
               // is the current value of the variable x equal to zero? The answer to this question is NO or
               // FALSE

the following is from the Arduino documentation pages and list some additional conditional statements.

 x == y (x is equal to y)
 x != y (x is not equal to y)
 x <  y (x is less than y)  
 x >  y (x is greater than y) 
 x <= y (x is less than or equal to y) 
 x >= y (x is greater than or equal to y) 

Conditional statements can be combined by including a logical AND (&&) or a logical OR ( || ), so the following statement can be used to define a range of values where a conditional will evaluate as TRUE.

x >= 10 && x <= 20   // True if x is equal to  10, 11, 12, 13, 14, 15, 16, 17, 18, 19 or 20

The if statement can now be used to turn on different light patterns depending on the value of the potentiometer. The following code is used to demonstrate this.

***** Begin Sketch 6 *****

***** End Sketch 6 *****

Using Buttons statements - Digital Input

The digital I/O pins on the Arduino (pins 0 to 14) are referred to as I/O because they can they be configured as digital outputs - such as they have been used in the preceeding examples - and they can be configured to detect digital inputs. This means they can be used to detect when a voltage signal switches from a HIGH level (+5V) to a LOW level (0 V).

The button shown on the figure below has one leg connected to the GND pin on the Arduino and the other leg is connected to digital I/O pin 2. This setup can be used to set the button to be configured with a "pull-up" resistor - the typical circuit representation of this is shown in the figure to the right of the Fritzing diagram.

The purpose of the pull-up resistor is to allow the digital input pin to "see" a voltage level of +5V by pulling it up to this voltage level by means of a large resistor (typically greater than 10 kOhms). As long as the button is not pressed, the digital input pin reads +5V, but when the button is pressed the pin is connected directly to the ground pin and the digital input pin registers a digital LOW.

The Arduino contains built-in 20 kOhm pull-up resistors that can be engaged using the pinMode() function, this removes the need of having to include physical pull-up resistors in the circuit set-up. The pull-up resistor is set using the following statement in the setup() function:

pinMode( pin, INPUT_PULLUP);

The state of the input pin (HIGH or LOW  or   1 to 0) is read using the digitalRead function

buttonState = digitalRead(pin);



          
The following sketch shows how to set up, read and display the state of the digital input pin. As long as the button is not pressed, the serial monitor will show that the state of the button is 1 (HIGH), when the button is pressed, the state of the button is switch to 0 (LOW).

***** Begin Sketch 7 *****

***** End Sketch 7 *****

The button input can now be used to increase the functionality of our test circuit. For instance, the behavior of Sketch 6 can be modified such that the user needs to hold down the button in order to adjust the number of LEDs that are on.

The following sketch only allows the user to update the number of lit LED's via the potentiometer when the button is pressed.

***** Begin Sketch 8 *****

***** End Sketch 8 *****