006 – Arduino Basics

<<< Previous Lecture ––––––– Next Lecture >>>

Digital Inputs and Outputs

  • The basic Arduino board has 13 digital input/output pins.
  • 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.)
  • Each of those pins can be either an input or an output
  • 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 supportPWM (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.

Function 0V 5V
digitalRead() LOW HIGH
digitalWrite() LOW HIGH
analogRead() 0 1023
analogWrite() 0 255

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

Examples

Our first example was controlling the brightness of an LED.

Screen Shot 2013-10-10 at 01.46.06 Screen Shot 2013-10-10 at 01.46.13

int brightness = 0;

void setup(){
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop(){
   analogWrite(9, brightness);
   brightness = brightness + 1;
   if(brightness > 255){
     brightness = 0;
   }
   delay(100);
   Serial.println(brightness);
}

In the second example we turned on an LED using a button.

Screen Shot 2013-10-10 at 01.58.13Screen Shot 2013-10-10 at 01.54.15

int buttonPin = 2;
int ledPin = 9;
int buttonState = 0;

void setup(){
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  if(buttonState==HIGH){
     digitalWrite(ledPin, HIGH);
  }else{
    digitalWrite(ledPin, LOW);
  }
  delay(10);
}