Input and Output
Table of Contents
Input pins
Output pins
Digital vs Analog
Some pins are digital-only
Some pins can be analog inputs
Analog-only pins are clearly labeled
No pins can generate a true analog output
void pinMode(pin, Mode)
pin
is the number of the pinMode
is the I/O mode the pin is set toINPUT
, OUTPUT
Digital Input
int digitalRead(pin)
LOW
(0 volts) or HIGH
(5 volts)int pinval;
pinval = digitalRead(3);
Digital Output
void digitalWrite(pin, value)
LOW
(0 volts) or HIGH
(5 volts)digitalWrite(3, HIGH);
HIGH
(5 volts)Analog Input
int analogRead(pin)
int pinval;
pinval = analogRead(A3);
Analog Output
http://www.allaboutcircuits.com/projects/learn-how-to-use-the-arduinos-digital-i-o/
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.
%%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
}
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
%%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
}
Analog Inputs
int analogRead(pin)
Analog Outputs
Pulse Width Modulation
analogWrite()
analogWrite(9, 128)
%%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);
}
}
Serial.begin()
Serial.begin(speed)
speed
is the baud rateSerial.begin(9600)
Usually call Serial.begin()
in the setup function
Serial.available()
Serial.read()
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()
Serial.print()
function instead.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.
}
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);
}
%%html
<iframe src="https://www.youtube.com/embed/KYWCkdrCUKg"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
%%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);
}
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')