Input and Output


By Prof. Seungchul Lee
http://iai.postech.ac.kr/
Industrial AI Lab at POSTECH

Table of Contents

1. Pins


  • Pins are wires connected to the microcontroller
  • Pins are the interface of the microcontroller
  • Pin voltages are controlled by a sketch
  • Pin voltages can be read by a sketch

Input pins

  • Input pins are controlled by other components
  • Arduino reads the voltage on the pins
  • Allows it to respond to events and data

Output pins

  • Output pins are controlled by the Arduino
  • Voltage is determined by your sketch
  • Other components can be controlled through outputs

Digital vs Analog

  • Some pins are digital-only

    • Read digital input, write digital output
    • 0 volts or 5 volts
  • Some pins can be analog inputs

    • Can read analog voltages on the pin
    • Useful for analog sensors
  • Analog-only pins are clearly labeled

  • No pins can generate a true analog output

Input/Ouput (I/O) in Arduino

  • These functions allow access to the pins
void pinMode(pin, Mode)
  • Sets a pin to act as either an input or an output
  • pin is the number of the pin
    • 1 -13 for the digital pins
    • A0 - A5 for the analog input pins
  • Mode is the I/O mode the pin is set to
    • INPUT, OUTPUT

Digital Input

int digitalRead(pin)
  • Returns the state of an input pin
  • Returns either LOW (0 volts) or HIGH (5 volts)
int pinval;
pinval = digitalRead(3);
  • pinval is set to the state of digital pin3

Digital Output

void digitalWrite(pin, value)
  • Assigns the state of an output pin
  • Assigns either LOW (0 volts) or HIGH (5 volts)
digitalWrite(3, HIGH);
  • Digital pin 3 is set HIGH (5 volts)

Analog Input

int analogRead(pin)
  • Returns the state of an analog input pin
  • Returns an integer from 0 to 1023
  • 0 for 0 volts, 1023 for 5 volts
int pinval;
pinval = analogRead(A3);
  • Pin must be an analog pin

Analog Output

  • No pins can generate a true analog output
  • But PWM can be used

2. Digital Input and Output

http://www.allaboutcircuits.com/projects/learn-how-to-use-the-arduinos-digital-i-o/

2.1. Lab 01: Blinking LED using delay

  • the LED connects to digital I/O pin 8 of the Arduino through the 220 ohm resistor.

  • The resistor controls the current through the LED.

  • sets the digital I/O pin to HIGH for 1000 ms, then to LOW for another 1000 ms.

In [1]:
%%html
<iframe src="https://www.youtube.com/embed/Q8rPASi2Br4?list=PLBD_gON7g_m0grsluFTqWhl5bCSfmSWR5?autoplay=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe>
const int led  =  8;        // use digital I/O pin 8

void setup() {
  pinMode(led, OUTPUT);     // set pin 8 to be an output output
}

void loop() {
  delay(1000);              // delay 1000 milliseconds
  digitalWrite(led, HIGH);  // set pin 8 HIGH, turning on LED
  delay(1000);              // delay 1000 milliseconds
  digitalWrite(led, LOW);   // set pin 8 LOW, turning off LED
}

2.2. Lab 02: Blinking LED using Button

  • An LED is connected to pin 8, which is configured as an OUTPUT.

  • A pushbutton is connected to pin 9, which is configured as an INPUT.

  • When someone presses the pushbutton switch, pin 9 is set to HIGH, and the program will then set the output of pin 8 to HIGH and turning on the LED.

  • Releasing the pushbutton resets pin 9 to LOW, then sets pin 8 to LOW, which turns off the LED.

  • debouncing will be discussed later

In [2]:
%%html
<iframe src="https://www.youtube.com/embed/5APhIAi7Mzs?autoplay=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe>
const int led    =   8;       //name pin 8 as led
const int button =   9;       //name pin 9 as button

void setup() {
  pinMode(led, OUTPUT);       //set pin 8 as OUTPUT
  pinMode(button, INPUT)  ;   //set pin 9 as INPUT
}

void loop() {
  int reads = digitalRead(button);  //read the digital value on pin 9
  digitalWrite(led, reads);         //set the digital output value of pin 8 to that value
}

3. Analog Inputs and Output (or Pulse Width Modulation)

Analog Inputs

int analogRead(pin)

  • Returns the state of an analog input pin
  • Returns an integer from 0 to 1023 ($2^{10}$)
  • 0 for 0 volts, 1023 for 5 volts

Analog Outputs

  • No pins can generate a true analog output

Pulse Width Modulation

  • Can be used for analog output
  • Duty cycle is the percent of time the pulse is high
  • Increasing duty cycle increases perceived voltage

analogWrite()

  • Generates a square wave on a pin, 490 Hz
  • First argument is the pin number
  • Second argument is the pulse width
    • 0 is 0% duty cycle
    • 255 is 100% duty cycle ($2^8$)
  • Pin number must be a PWM pin
    • Marked on the Arduino with the ~ symbol
  • For example: analogWrite(9, 128)

3.1. Lab 03: Fade LED In and Out (PWM)

  • 220 ohm resistor
  • Note that pin 9 is used
In [7]:
%%html
<iframe src="https://www.youtube.com/embed/_HqUBLAbxSI?autoplay=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe>
int i = 0;
const int LED = 9;     //define the pin we use for LED

void setup() {
  pinMode(LED, OUTPUT); //set pin 9 as OUTPUT
}

void loop() {
  for (int i = 0; i < 255; i++) { //if i is less than 255 then increase i with 1
    analogWrite(LED, i);          //write the i value to pin 11
    delay(5);                     //wait 5 ms then do the for loop again
  }

  for (int i = 255; i > 0; i--) { //descrease i with 1
    analogWrite(LED, i);
    delay(5);
  }
}

4. Serial Communication


  • UART protocol used over the USB cable
  • One bit at a time
  • Initialize by using Serial.begin()
  • Serial.begin(speed)

    • speed is the baud rate
    • Serial.begin(9600)
  • Usually call Serial.begin() in the setup function

Serial.available()

  • Get the number of bytes (characters) available for reading from the serial port. This is data that's already arrived and stored in the serial receive buffer (which holds 64 bytes).

Serial.read()

  • Reads incoming serial data.
  • Returns the first byte of incoming serial data available (or -1 if no data is available)

Serial.print()

Serial.println(): new line


int incomingByte = 0;   // for incoming serial data

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte);
  }
}

Serial.write()

  • Writes binary data (ASCII) to the serial port. This data is sent as a byte or series of bytes
  • to send the characters representing the digits of a number use the Serial.print() function instead.
  • Serial.write(val)
    • val: a value to send as a single byte
  • Serial.write(str)
    • str: a string to send as a series of bytes

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.write(45); // send a byte with the value 45

  int bytesSent = Serial.write("hello"); //send the string “hello” and return the length of the string.
}

Debug via Serial Communication

  • Serial.print()
int x = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(x);
  if (x > 10) {
    x = 0;
  }
  else {
    x++;
  }
  delay(1000);
}
  • Serial.write()
int x = 48;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.write(x);
  Serial.println(' ');
  if (x > 56) {
    x = 48;
  }
  else {
    x++;
  }
  delay(1000);
}
In [4]:
%%html
<iframe src="https://www.youtube.com/embed/KYWCkdrCUKg" 
width="560" height="315" frameborder="0" allowfullscreen></iframe>

4.1. Lab 04: Serial Communication for Analog Inputs

In [5]:
%%html
<iframe src="https://www.youtube.com/embed/7Pq8BgJwo1w" 
width="560" height="315" frameborder="0" allowfullscreen></iframe>
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);

  // print out the value you read:
  Serial.println(voltage);
}
In [6]:
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')