Digital I/O
Update time:2018-04-13 Views:5987
Operating Environment
Operating system : windows 7 Ultimate 64-bit
IDE : Arduino1.6.6
pinMode()
Description
Configures the specified pin to behave either as an input or an output.
Syntax
pinMode(pin,mode);
Parameters
pin : pin: the number of the pin whose mode you wish to set。(from 0~29)。(only for digital pins with only number names) Mode:OUTPUT,INPUT,INPUT_PULLUP,INPUT_PULLDOWN.
Returns
None
Example
int ledPin = 13; // LED connected to digital pin 13void setup(){ pinMode(ledPin, OUTPUT); // sets the digital pin as output}void loop(){ digitalWrite(ledPin, HIGH); // sets the LED on delay(1000); // waits for a second digitalWrite(ledPin, LOW); // sets the LED off delay(1000); // waits for a second }
Note
1.The digital pins cann't set pull direction arbitrary,as the pull direction has been set up,details see the table below。 2.all Digital I/O supports PWM output。 3.all Analog I/O cann't be used as Digital I/O
PIN | pull direction | PIN | pull direction | PIN | pull direction | PIN | pull direction |
---|---|---|---|---|---|---|---|
0 | pull up | 8 | pull down | 14 | pull up | 23 | pull up |
1 | pull up | 9 | pull up | 15 | pull up | 24 | pull up |
2 | pull down | 10 | pull up | 16 | pull up | 25 | pull up |
3 | pull up | 11 | pull up | 17 | pull up | 26 | pull up |
4 | pull down | 12 | pull up | 18 | pull up | 27 | pull down |
5 | pull down | 13 | pull up | 19 | pull up | ||
6 | pull down | 28 | pull up | 20 | pull up | ||
7 | pull down | 29 | pull up | 21 | pull up | ||
22 | pull up |
digitalWrite()
Description
Write a HIGH or a LOW value to a digital pin.
If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 3.3V for HIGH, 0V (ground) for LOW.
Syntax
digitalWrite(pin,value);
Parameters
pin: the pin number value: HIGH or LOW
Returns
None
Example
Sets pin 13 to HIGH, makes a one-second-long delay, and sets the pin back to LOW.
int ledPin = 13; // LED connected to digital pin 13void setup(){pinMode(ledPin, OUTPUT); // sets the digital pin as output}void loop(){digitalWrite(ledPin, HIGH); // sets the LED ondelay(1000); // waits for a seconddigitalWrite(ledPin, LOW); // sets the LED offdelay(1000); // waits for a second}
digitalRead()
Description
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
value = digitalRead(pin);
Parameters
pin: the number of the digital pin you want to read (int)
Returns
HIGH or LOW
example
int ledPin = 13; // LED connected to digital pin 13int inPin = 7; // pushbutton connected to digital pin 7int val = 0; // variable to store the read valuevoid setup(){pinMode(ledPin, OUTPUT); // sets the digital pin 13 as outputpinMode(inPin, INPUT); // sets the digital pin 7 as input}void loop(){val = digitalRead(inPin); // read the input pindigitalWrite(ledPin, val); // sets the LED to the button's value}