EFA:Arduino:Basics

This page explains the basic functionality of the Arduino in one place. See the sidebar menu for more detailed examples. Or explore the Arduino reference and tutorials.

Arduino

Arduino is an open-source microcontroller platform that is designed to make it easier to work with electronics. It is based on Wiring, a project that originally was heavily inspired by Processing and tried to do the same to hardware as Processing had done to software.

There have been many other boards that have tried to do similar things, but Arduino is the most popular in the art, design, and maker community.

If you have a couple of hours, I recommend digging into the history of Arduino and the soap-opera style drama surrounding the recent years.

All of this damaged Arduino quite significantly, but it also made all the alternatives stronger.

How To Get Started?

Download the Arduino software and follow the instructions on the Arduino website if you have problems setting things up. The Arduino boards that you have are Arduino Unos and do not require any driver installation for Mac or Linux. Windows will require a driver that comes with the installer package.

Getting Started With Arduino from the Arduino website.

Arduino Code Structure

The Arduino software is used to program the Arduino board. You will notice that it is very similar to Processing. The code structure is also very similar. All Arduino sketches need to have the following functions.

  • setup()
    • Runs once when the board starts or is reset
    • You use the setup() for things that you usually only need to do once (like opening the serial port, defining pin modes etc.)
  • loop()

screen-shot-2016-10-18-at-00-53-21

Blinking an LED

Our first program is the one that already comes with your Arduino (if it is a new one). You will see that the little LED on the board marked ‘L’ starts blinking when you power the board. Let’s connect an external LED to see it a little bit better. There is a good tutorial on the Arduino website for this basic example. We will go through the example to check that everything is working properly.

Hook up an LED on the breadboard like this:

screen-shot-2013-10-07-at-19-28-15

Here is the same in schematic view:

examplecircuit_sch

And here is the code:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// 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);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Upload your code to the board. Check that you have the correct Board and Serial Port selected from the Tools menu. Go through the getting started part again, if you have problems.

Once you make sure that everything works properly, you can start editing the code:

  • What happens if you change the values inside delay()?
  • Copy and paste more lines to create different light patterns
  • Can you blink SOS using Morse code?
  • Can you figure out the limits of your sight? What is the fastest speed where you can still see it blinking.

Inputs and Outputs

Let’s take a closer look at the inputs and outputs of the Arduino Uno board.

screen-shot-2016-10-18-at-00-53-21

  • The Arduino Uno board has 14 digital pins that can be used either as inputs or outputs. These are often called GPIO pins (General Purpose In/Out).
  • Digital inputs are used to read buttons/switches and other digital signals
  • Digital outputs are used to control other electric components or devices (LEDs, motors etc.)
  • You set the pin as INPUT or OUTPUT with the pinMode() function
  • The Arduino Uno board has 6 analog inputs that can be used to read voltages between 0V to 5V. (These analog input pins can actually also be used for digital in/out. Analog In 0 can also be called Digital In/Out 14, A0 is 15 etc.)
  • The Uno doesn’t have any true analog outputs (DACs, Digital to Analog Converters) but it has 6 pins that are able to output PWM signals that work kind of like analog outputs.

Everything on the Arduino Uno is based on a 5 volt logic level. Meaning that the maximum voltage you can have as input to any of the pins is 5 volts.

Keep in mind that some boards (especially newer ones) are using 3.3V instead of 5V as the logic level. Connecting 5V to a pin of a 3.3V device will break the board or at least destroy that specific pin. We use the Arduino Nano 33 IOT in this class. It is a board that works with 3.3V (hence the 33 in the name), so be careful when working with it.

Logic Level/Resolution of Inputs and Outputs

Come back to this table once you have gone through the rest of this page and the examples we build. It should make more sense after that.

Function0V5V
digitalRead()LOW (0)HIGH (1)
digitalWrite()LOW (0)HIGH (1)
analogRead()01023
analogWrite()0255

Digital Output

We have already used the digital outputs for blinking the LED in the previous Blink example. Each of the GPIO pins can be either an output or input so you need to use the pinMode() function to set it to the correct mode before using it.

Digital Input

Each of the GPIO pins can be either an output or input so you need to use the pinMode() function to set it to the correct mode before using it.

  • You set the pin as an input by using pinMode(pin, INPUT)
  • You can also set it as an input pin with the built-in pull-up resistor connected. See below for an explanation what this means. If you want to do this the command is pinMode(pin, INPUT_PULLUP)

Analog Output

The basic Arduino board does not have real analog output, but some of the pins support PWM (pulse-width modulation). PWM lets us to “fake” an analog signal with digital outputs. It can be used to fade lights, control the speed of motors etc.

Note that only certain pins are able to generate the PWM signal. On an Arduino Uno those pins are 3, 5, 6, 9, 10, and 11.

Analog Input

The Arduino Duemilanove/UNO/Leonardo boards have 6 analog inputs. They are 10-bit ADCs (analog-to-digital converters). The analog inputs are used to read analog sensors.


Serial Communication

The Arduino uses a serial port to send/receive data between the board and the computer (or any other device that supports serial communication).

On the Arduino Uno, you only have one hardware serial port. It is connected to the USB connector and is used to upload the code to your board as well as for sending data back and forth while your code is running.

It’s very important to note that this same serial port is also accessible on the GPIO pins 0 and 1 on your Arduino Uno. They are labelled RX (R for receive) and TX (T for transmit). Usually, you should not connect anything to these two pins since that will block your code from being uploaded. Use them only if you are connecting those pins to a device with serial input/output.


Examples

Basic Example #1 | Read the State of a Digital Input and Send Out Digital Output

Basic Example #2 | Read an Analog Input and Print Out the Results

Many sensors are just resistors that change their resistance value based on some external input (light, temperature, force etc.). The Arduino is not able to read the change in resistance directly, but we can convert that resistance change into a change in voltage using a voltage divider.

Voltage Divider

If you connect two resistors in series as in the image below, the voltage read from Vout depends on the values of the two resistors. Read the Wikipedia article if you want to learn how to calculate the values.

resistive_divider2-svg

This can be used to read the values from various sensors, such as the light sensor we are using.

screen-shot-2016-10-20-at-14-08-58

screen-shot-2016-10-20-at-14-12-40

// Read and display the value from the light sensor.

int lightSensor = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  lightSensor = analogRead(A0);
  Serial.print("light: ");
  Serial.println(lightSensor);
}

Basic Example #3 | Controlling the Brightness of an LED with the Light Sensor

screen-shot-2016-10-20-at-14-23-30
screen-shot-2016-10-20-at-14-25-34
int lightSensor = 0;
int b = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(9,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  lightSensor = analogRead(A0);
  Serial.print("light: ");
  Serial.print(lightSensor);
  
  b = map(lightSensor,450,850,255,0);
  b = constrain(b,0,255);
  
  Serial.print(" led: ");
  Serial.println(b);
  analogWrite(9,b);

Note that we use the map() command to scale the input values from the sensor to a range more suitable for the LED. The constrain() makes sure that we never send invalid values (under 0 or over 255) to the LED.