Digital I/O
Please note that this is not basic programming. This is programming that involves knowledge of electronics. This is also the first step into robotics (for me, it is). No library files are required since these functions are built in. Let's first start with learning exactly where we will be using the Digital I/O on the Arduino board.
Can't see it clearly? I'.ll just tell you. The circled area is where the Digital ports go. On your Arduino board (assuming you have one), these ports are labeled DIGITAL. This is where you will be connecting motors, servos, Ping sensors, all kinds of stuff.
Now let's look at some code. Say I have an LED at digital port 6 (it is very important where you put them electronics). I want to turn that LED on. First, we need to activate that port. To do so, we must do the following command:
pinMode(6,OUTPUT);
The pinMode command allows us to activate a digital port on the Arduino. Here is the syntax:
pinMode(pin, mode);
The pin is the digital port we are using. In this case, it is 6. The mode is the type of electronic communication that we are using. In this case, we are in OUTPUT mode since we are sending electronic signals to the LED to light it up. There are three modes you can use:
INPUT
OUTPUT
INPUT_PULLUP
Setting a pin to INPUT will be useful in reading sensors. INPUT ports cannot be used to turn on an LED.
Setting a pin to OUTPUT will be useful in sending electric current to other devices. This is a bit different than voltage. Usually, what we mean by electric current means like a certain frequency to control a servo for example. OUTPUT ports cannot be used to read sensors.
Setting a pin to INPUT_PULLUP will activate the Arduino's built-in pull-up resistors (resistors that connect to power internally). Not really sure what that does really. I'll make a tutorial on that after some testing with it.
Now let's look at some code. Say I have an LED at digital port 6 (it is very important where you put them electronics). I want to turn that LED on. First, we need to activate that port. To do so, we must do the following command:
pinMode(6,OUTPUT);
The pinMode command allows us to activate a digital port on the Arduino. Here is the syntax:
pinMode(pin, mode);
The pin is the digital port we are using. In this case, it is 6. The mode is the type of electronic communication that we are using. In this case, we are in OUTPUT mode since we are sending electronic signals to the LED to light it up. There are three modes you can use:
INPUT
OUTPUT
INPUT_PULLUP
Setting a pin to INPUT will be useful in reading sensors. INPUT ports cannot be used to turn on an LED.
Setting a pin to OUTPUT will be useful in sending electric current to other devices. This is a bit different than voltage. Usually, what we mean by electric current means like a certain frequency to control a servo for example. OUTPUT ports cannot be used to read sensors.
Setting a pin to INPUT_PULLUP will activate the Arduino's built-in pull-up resistors (resistors that connect to power internally). Not really sure what that does really. I'll make a tutorial on that after some testing with it.