Arduino Pins

And how to use them

This post is the 2nd part of Arduino Programming.
If you have not read it, I recommend you do it before continuing.
If you have read it, then…

¡Let’s get started!

  • Pin’s declaration
  • Writing pins
  • Reading pins
  • Example

Now, I’m going to explain you the easiest way to start using Arduino’s pins.


Pin’s Declaration

As I mentioned in the last post, these pins are I/O pins. So, we need to declare which pins we will used and how.

In a generic way, it would be something like this:

  • pinMode(“number_of_pin”, INPUT);
  • pinMode(“number_of_pin”, OUTPUT);

The function pinMode it’s Arduino’s own.
This function need two values: the number of the pin (it’s on the board) and the type of pin tha we are using (INPUT - Lecture) or (OUTPUT - Write).

Example:
Using the same code that we used in the last post:

void setup() {
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, HIGH);   
  delay(1000);              
  digitalWrite(13, LOW);    
  delay(1000);
}

In line 2 pinMode(13, OUTPUT), there’s the pin 13 declaration as OUTPUT. In this case, the pin 13 is connected to a LED in the board by default. If you want to use the pin number 12, you should connect your own LED on it.

IMPORTANT! Declarations of pins are made in the function setup so that they only run once.


Writing of Pins.

After the declarations, we will have to indicate when the LED will turn on and off.
For this, we must send a pulse of 5V.
That is, if we want the LED to turn on we send it a high pulse:

digitalWrite("number_of_pin,HIGH)

if we want the LED to turn off we send a low pulse:

digitalWrite("number_of_pin,LOW)

Instead of writing LOW or HIGH you can write 0 or 1 respectively.


Reading Pins

We need to read a pin if we connect a button because when the button is pressed we will be sending a 1 or 0 (depending on how the pin is configured) and we will have to check its status in order to do anything.

Example: if we want to turn on a LED with a button, we should read the pin’s state and send a high or low signal to the LED.

To read a pin’s state, we will need to write the next line of code:

digitalRead("pin number");


Read and write pins example

The simplies example, as we have already said, is to turn on a LED by pressing a button.

You can find this example in: File - Exampls - 02.Digital - Button.

const int buttonPin = 2; 
const int ledPin =  13;
int buttonState = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

The setup function asigns pins. Pin 13 will be output, while pin 2 will be input (button).

The loop function will be in charge of reading the button state. Depending of what it reads, the LED will light or not.

If you press the button one time, the state will be HIGH, so the light will turn on.

If you have any question, just leave a comment and I will help you!


Leave a comment