DS3:005–006 – Arduino Inputs & Outputs



What is electricity?

Ohm’s Law

Ohm’s law is an equation that can be used to calculate the values of the different properties in an electronic circuit.

  • R (resistance) = V (voltage) / I (current)
  • V=R * I
  • I=V / R

This video from Make explains the basics quite nicely:


The image above also visualizes the properties of voltage, current and resistance quite well. (Unfortunately, I do not know the original source of this illustration.)

Here is a good definition of the basic concepts and components you will run into by Tom Igoe http://www.tigoe.com/pcomp/code/circuits/understanding-electricity/


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. 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.

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.

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()


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:


Here is the same in schematic view:


And here is the code:

  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?


Digital Inputs and Outputs

  • The basic Arduino board has 14 digital pins that can be used either as inputs or outputs
  • 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

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.

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.

Logic Level and the Resolution of Inputs and Outputs.

digitalRead()LOW (0)HIGH (1)
digitalWrite()LOW (0)HIGH (1)

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).

How to Read Digital Signals [digitalRead()]

Here is how you would read the value from a switch.

// Read the input from a digital signal
int buttonState;
void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);
  pinMode(9, OUTPUT);

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(2);
  if(buttonState == HIGH){
      Serial.println("Button pressed");
      digitalWrite(9, HIGH);
      Serial.println("Button not pressed");
      digitalWrite(9, LOW);

The reed switch in your kit is another type of switch that you would read exactly the same way as the other switch.

How to Read Sensors [analogRead()]

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.


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



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

int lightSensor = 0;

void setup() {
  // put your setup code here, to run once:

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

Controlling the Brightness of an LED With the Light Sensor

int lightSensor = 0;
int b = 0;

void setup() {
  // put your setup code here, to run once:

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

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.